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

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\Attribute\AsCommand;
  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\Input\InputArgument;
  16. use Symfony\Component\Console\Input\InputInterface;
  17. use Symfony\Component\Console\Output\OutputInterface;
  18. use Symfony\Component\Console\Style\SymfonyStyle;
  19. use Symfony\Component\HttpKernel\CacheClearer\Psr6CacheClearer;
  20. /**
  21.  * Delete an item from a cache pool.
  22.  *
  23.  * @author Pierre du Plessis <pdples@gmail.com>
  24.  */
  25. #[AsCommand(name'cache:pool:delete'description'Delete an item from a cache pool')]
  26. final class CachePoolDeleteCommand extends Command
  27. {
  28.     private Psr6CacheClearer $poolClearer;
  29.     private ?array $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.             ->setHelp(<<<'EOF'
  50. The <info>%command.name%</info> deletes an item from a given cache pool.
  51.     %command.full_name% <pool> <key>
  52. EOF
  53.             )
  54.         ;
  55.     }
  56.     /**
  57.      * {@inheritdoc}
  58.      */
  59.     protected function execute(InputInterface $inputOutputInterface $output): int
  60.     {
  61.         $io = new SymfonyStyle($input$output);
  62.         $pool $input->getArgument('pool');
  63.         $key $input->getArgument('key');
  64.         $cachePool $this->poolClearer->getPool($pool);
  65.         if (!$cachePool->hasItem($key)) {
  66.             $io->note(sprintf('Cache item "%s" does not exist in cache pool "%s".'$key$pool));
  67.             return 0;
  68.         }
  69.         if (!$cachePool->deleteItem($key)) {
  70.             throw new \Exception(sprintf('Cache item "%s" could not be deleted.'$key));
  71.         }
  72.         $io->success(sprintf('Cache item "%s" was successfully deleted.'$key));
  73.         return 0;
  74.     }
  75.     public function complete(CompletionInput $inputCompletionSuggestions $suggestions): void
  76.     {
  77.         if (\is_array($this->poolNames) && $input->mustSuggestArgumentValuesFor('pool')) {
  78.             $suggestions->suggestValues($this->poolNames);
  79.         }
  80.     }
  81. }