CachePoolPrunerPass.php 1.7 KB

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