CachePoolPrunerPass.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Cache\DependencyInjection;
  11. use Symfony\Component\Cache\PruneableInterface;
  12. use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
  13. use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
  14. use Symfony\Component\DependencyInjection\ContainerBuilder;
  15. use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
  16. use Symfony\Component\DependencyInjection\Reference;
  17. /**
  18. * @author Rob Frawley 2nd <rmf@src.run>
  19. */
  20. class CachePoolPrunerPass implements CompilerPassInterface
  21. {
  22. private $cacheCommandServiceId;
  23. private $cachePoolTag;
  24. public function __construct(string $cacheCommandServiceId = 'console.command.cache_pool_prune', string $cachePoolTag = 'cache.pool')
  25. {
  26. if (0 < \func_num_args()) {
  27. trigger_deprecation('symfony/cache', '5.3', 'Configuring "%s" is deprecated.', __CLASS__);
  28. }
  29. $this->cacheCommandServiceId = $cacheCommandServiceId;
  30. $this->cachePoolTag = $cachePoolTag;
  31. }
  32. /**
  33. * {@inheritdoc}
  34. */
  35. public function process(ContainerBuilder $container)
  36. {
  37. if (!$container->hasDefinition($this->cacheCommandServiceId)) {
  38. return;
  39. }
  40. $services = [];
  41. foreach ($container->findTaggedServiceIds($this->cachePoolTag) as $id => $tags) {
  42. $class = $container->getParameterBag()->resolveValue($container->getDefinition($id)->getClass());
  43. if (!$reflection = $container->getReflectionClass($class)) {
  44. throw new InvalidArgumentException(sprintf('Class "%s" used for service "%s" cannot be found.', $class, $id));
  45. }
  46. if ($reflection->implementsInterface(PruneableInterface::class)) {
  47. $services[$id] = new Reference($id);
  48. }
  49. }
  50. $container->getDefinition($this->cacheCommandServiceId)->replaceArgument(0, new IteratorArgument($services));
  51. }
  52. }