vendor/symfony/framework-bundle/Command/CachePoolClearCommand.php line 40

Open in your IDE?
  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\Bundle\FrameworkBundle\Command;
  11. use Psr\Cache\CacheItemPoolInterface;
  12. use Symfony\Component\Console\Attribute\AsCommand;
  13. use Symfony\Component\Console\Command\Command;
  14. use Symfony\Component\Console\Completion\CompletionInput;
  15. use Symfony\Component\Console\Completion\CompletionSuggestions;
  16. use Symfony\Component\Console\Exception\InvalidArgumentException;
  17. use Symfony\Component\Console\Input\InputArgument;
  18. use Symfony\Component\Console\Input\InputInterface;
  19. use Symfony\Component\Console\Output\OutputInterface;
  20. use Symfony\Component\Console\Style\SymfonyStyle;
  21. use Symfony\Component\HttpKernel\CacheClearer\Psr6CacheClearer;
  22. /**
  23.  * Clear cache pools.
  24.  *
  25.  * @author Nicolas Grekas <p@tchwork.com>
  26.  */
  27. #[AsCommand(name'cache:pool:clear'description'Clear cache pools')]
  28. final class CachePoolClearCommand extends Command
  29. {
  30.     private Psr6CacheClearer $poolClearer;
  31.     private ?array $poolNames;
  32.     /**
  33.      * @param string[]|null $poolNames
  34.      */
  35.     public function __construct(Psr6CacheClearer $poolClearer, array $poolNames null)
  36.     {
  37.         parent::__construct();
  38.         $this->poolClearer $poolClearer;
  39.         $this->poolNames $poolNames;
  40.     }
  41.     /**
  42.      * {@inheritdoc}
  43.      */
  44.     protected function configure()
  45.     {
  46.         $this
  47.             ->setDefinition([
  48.                 new InputArgument('pools'InputArgument::IS_ARRAY InputArgument::REQUIRED'A list of cache pools or cache pool clearers'),
  49.             ])
  50.             ->setHelp(<<<'EOF'
  51. The <info>%command.name%</info> command clears the given cache pools or cache pool clearers.
  52.     %command.full_name% <cache pool or clearer 1> [...<cache pool or clearer N>]
  53. EOF
  54.             )
  55.         ;
  56.     }
  57.     /**
  58.      * {@inheritdoc}
  59.      */
  60.     protected function execute(InputInterface $inputOutputInterface $output): int
  61.     {
  62.         $io = new SymfonyStyle($input$output);
  63.         $kernel $this->getApplication()->getKernel();
  64.         $pools = [];
  65.         $clearers = [];
  66.         foreach ($input->getArgument('pools') as $id) {
  67.             if ($this->poolClearer->hasPool($id)) {
  68.                 $pools[$id] = $id;
  69.             } else {
  70.                 $pool $kernel->getContainer()->get($id);
  71.                 if ($pool instanceof CacheItemPoolInterface) {
  72.                     $pools[$id] = $pool;
  73.                 } elseif ($pool instanceof Psr6CacheClearer) {
  74.                     $clearers[$id] = $pool;
  75.                 } else {
  76.                     throw new InvalidArgumentException(sprintf('"%s" is not a cache pool nor a cache clearer.'$id));
  77.                 }
  78.             }
  79.         }
  80.         foreach ($clearers as $id => $clearer) {
  81.             $io->comment(sprintf('Calling cache clearer: <info>%s</info>'$id));
  82.             $clearer->clear($kernel->getContainer()->getParameter('kernel.cache_dir'));
  83.         }
  84.         $failure false;
  85.         foreach ($pools as $id => $pool) {
  86.             $io->comment(sprintf('Clearing cache pool: <info>%s</info>'$id));
  87.             if ($pool instanceof CacheItemPoolInterface) {
  88.                 if (!$pool->clear()) {
  89.                     $io->warning(sprintf('Cache pool "%s" could not be cleared.'$pool));
  90.                     $failure true;
  91.                 }
  92.             } else {
  93.                 if (false === $this->poolClearer->clearPool($id)) {
  94.                     $io->warning(sprintf('Cache pool "%s" could not be cleared.'$pool));
  95.                     $failure true;
  96.                 }
  97.             }
  98.         }
  99.         if ($failure) {
  100.             return 1;
  101.         }
  102.         $io->success('Cache was successfully cleared.');
  103.         return 0;
  104.     }
  105.     public function complete(CompletionInput $inputCompletionSuggestions $suggestions): void
  106.     {
  107.         if (\is_array($this->poolNames) && $input->mustSuggestArgumentValuesFor('pools')) {
  108.             $suggestions->suggestValues($this->poolNames);
  109.         }
  110.     }
  111. }