CachePoolPrunerPass.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. $this->cacheCommandServiceId = $cacheCommandServiceId;
  27. $this->cachePoolTag = $cachePoolTag;
  28. }
  29. /**
  30. * {@inheritdoc}
  31. */
  32. public function process(ContainerBuilder $container)
  33. {
  34. if (!$container->hasDefinition($this->cacheCommandServiceId)) {
  35. return;
  36. }
  37. $services = [];
  38. foreach ($container->findTaggedServiceIds($this->cachePoolTag) as $id => $tags) {
  39. $class = $container->getParameterBag()->resolveValue($container->getDefinition($id)->getClass());
  40. if (!$reflection = $container->getReflectionClass($class)) {
  41. throw new InvalidArgumentException(sprintf('Class "%s" used for service "%s" cannot be found.', $class, $id));
  42. }
  43. if ($reflection->implementsInterface(PruneableInterface::class)) {
  44. $services[$id] = new Reference($id);
  45. }
  46. }
  47. $container->getDefinition($this->cacheCommandServiceId)->replaceArgument(0, new IteratorArgument($services));
  48. }
  49. }