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

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