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

  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.     protected function configure()
  42.     {
  43.         $this
  44.             ->setDefinition([
  45.                 new InputArgument('pools'InputArgument::IS_ARRAY InputArgument::REQUIRED'A list of cache pools or cache pool clearers'),
  46.             ])
  47.             ->setHelp(<<<'EOF'
  48. The <info>%command.name%</info> command clears the given cache pools or cache pool clearers.
  49.     %command.full_name% <cache pool or clearer 1> [...<cache pool or clearer N>]
  50. EOF
  51.             )
  52.         ;
  53.     }
  54.     protected function execute(InputInterface $inputOutputInterface $output): int
  55.     {
  56.         $io = new SymfonyStyle($input$output);
  57.         $kernel $this->getApplication()->getKernel();
  58.         $pools = [];
  59.         $clearers = [];
  60.         foreach ($input->getArgument('pools') as $id) {
  61.             if ($this->poolClearer->hasPool($id)) {
  62.                 $pools[$id] = $id;
  63.             } else {
  64.                 $pool $kernel->getContainer()->get($id);
  65.                 if ($pool instanceof CacheItemPoolInterface) {
  66.                     $pools[$id] = $pool;
  67.                 } elseif ($pool instanceof Psr6CacheClearer) {
  68.                     $clearers[$id] = $pool;
  69.                 } else {
  70.                     throw new InvalidArgumentException(sprintf('"%s" is not a cache pool nor a cache clearer.'$id));
  71.                 }
  72.             }
  73.         }
  74.         foreach ($clearers as $id => $clearer) {
  75.             $io->comment(sprintf('Calling cache clearer: <info>%s</info>'$id));
  76.             $clearer->clear($kernel->getContainer()->getParameter('kernel.cache_dir'));
  77.         }
  78.         $failure false;
  79.         foreach ($pools as $id => $pool) {
  80.             $io->comment(sprintf('Clearing cache pool: <info>%s</info>'$id));
  81.             if ($pool instanceof CacheItemPoolInterface) {
  82.                 if (!$pool->clear()) {
  83.                     $io->warning(sprintf('Cache pool "%s" could not be cleared.'$pool));
  84.                     $failure true;
  85.                 }
  86.             } else {
  87.                 if (false === $this->poolClearer->clearPool($id)) {
  88.                     $io->warning(sprintf('Cache pool "%s" could not be cleared.'$pool));
  89.                     $failure true;
  90.                 }
  91.             }
  92.         }
  93.         if ($failure) {
  94.             return 1;
  95.         }
  96.         $io->success('Cache was successfully cleared.');
  97.         return 0;
  98.     }
  99.     public function complete(CompletionInput $inputCompletionSuggestions $suggestions): void
  100.     {
  101.         if (\is_array($this->poolNames) && $input->mustSuggestArgumentValuesFor('pools')) {
  102.             $suggestions->suggestValues($this->poolNames);
  103.         }
  104.     }
  105. }