CachePoolPrunerPass.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. public function process(ContainerBuilder $container): void
  23. {
  24. if (!$container->hasDefinition('console.command.cache_pool_prune')) {
  25. return;
  26. }
  27. $services = [];
  28. foreach ($container->findTaggedServiceIds('cache.pool') as $id => $tags) {
  29. $class = $container->getParameterBag()->resolveValue($container->getDefinition($id)->getClass());
  30. if (!$reflection = $container->getReflectionClass($class)) {
  31. throw new InvalidArgumentException(\sprintf('Class "%s" used for service "%s" cannot be found.', $class, $id));
  32. }
  33. if ($reflection->implementsInterface(PruneableInterface::class)) {
  34. $services[$id] = new Reference($id);
  35. }
  36. }
  37. $container->getDefinition('console.command.cache_pool_prune')->replaceArgument(0, new IteratorArgument($services));
  38. }
  39. }