vendor/symfony/framework-bundle/Command/CachePoolDeleteCommand.php line 39

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 Symfony\Component\Console\Command\Command;
  12. use Symfony\Component\Console\Completion\CompletionInput;
  13. use Symfony\Component\Console\Completion\CompletionSuggestions;
  14. use Symfony\Component\Console\Input\InputArgument;
  15. use Symfony\Component\Console\Input\InputInterface;
  16. use Symfony\Component\Console\Output\OutputInterface;
  17. use Symfony\Component\Console\Style\SymfonyStyle;
  18. use Symfony\Component\HttpKernel\CacheClearer\Psr6CacheClearer;
  19. /**
  20.  * Delete an item from a cache pool.
  21.  *
  22.  * @author Pierre du Plessis <pdples@gmail.com>
  23.  */
  24. final class CachePoolDeleteCommand extends Command
  25. {
  26.     protected static $defaultName 'cache:pool:delete';
  27.     protected static $defaultDescription 'Delete an item from a cache pool';
  28.     private $poolClearer;
  29.     private $poolNames;
  30.     /**
  31.      * @param string[]|null $poolNames
  32.      */
  33.     public function __construct(Psr6CacheClearer $poolClearer, array $poolNames null)
  34.     {
  35.         parent::__construct();
  36.         $this->poolClearer $poolClearer;
  37.         $this->poolNames $poolNames;
  38.     }
  39.     /**
  40.      * {@inheritdoc}
  41.      */
  42.     protected function configure()
  43.     {
  44.         $this
  45.             ->setDefinition([
  46.                 new InputArgument('pool'InputArgument::REQUIRED'The cache pool from which to delete an item'),
  47.                 new InputArgument('key'InputArgument::REQUIRED'The cache key to delete from the pool'),
  48.             ])
  49.             ->setDescription(self::$defaultDescription)
  50.             ->setHelp(<<<'EOF'
  51. The <info>%command.name%</info> deletes an item from a given cache pool.
  52.     %command.full_name% <pool> <key>
  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.         $pool $input->getArgument('pool');
  64.         $key $input->getArgument('key');
  65.         $cachePool $this->poolClearer->getPool($pool);
  66.         if (!$cachePool->hasItem($key)) {
  67.             $io->note(sprintf('Cache item "%s" does not exist in cache pool "%s".'$key$pool));
  68.             return 0;
  69.         }
  70.         if (!$cachePool->deleteItem($key)) {
  71.             throw new \Exception(sprintf('Cache item "%s" could not be deleted.'$key));
  72.         }
  73.         $io->success(sprintf('Cache item "%s" was successfully deleted.'$key));
  74.         return 0;
  75.     }
  76.     public function complete(CompletionInput $inputCompletionSuggestions $suggestions): void
  77.     {
  78.         if (\is_array($this->poolNames) && $input->mustSuggestArgumentValuesFor('pool')) {
  79.             $suggestions->suggestValues($this->poolNames);
  80.         }
  81.     }
  82. }