CachePoolDeleteCommand.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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\Input\InputArgument;
  13. use Symfony\Component\Console\Input\InputInterface;
  14. use Symfony\Component\Console\Output\OutputInterface;
  15. use Symfony\Component\Console\Style\SymfonyStyle;
  16. use Symfony\Component\HttpKernel\CacheClearer\Psr6CacheClearer;
  17. /**
  18. * Delete an item from a cache pool.
  19. *
  20. * @author Pierre du Plessis <pdples@gmail.com>
  21. */
  22. final class CachePoolDeleteCommand extends Command
  23. {
  24. protected static $defaultName = 'cache:pool:delete';
  25. private $poolClearer;
  26. public function __construct(Psr6CacheClearer $poolClearer)
  27. {
  28. parent::__construct();
  29. $this->poolClearer = $poolClearer;
  30. }
  31. /**
  32. * {@inheritdoc}
  33. */
  34. protected function configure()
  35. {
  36. $this
  37. ->setDefinition([
  38. new InputArgument('pool', InputArgument::REQUIRED, 'The cache pool from which to delete an item'),
  39. new InputArgument('key', InputArgument::REQUIRED, 'The cache key to delete from the pool'),
  40. ])
  41. ->setDescription('Delete an item from a cache pool')
  42. ->setHelp(<<<'EOF'
  43. The <info>%command.name%</info> deletes an item from a given cache pool.
  44. %command.full_name% <pool> <key>
  45. EOF
  46. )
  47. ;
  48. }
  49. /**
  50. * {@inheritdoc}
  51. */
  52. protected function execute(InputInterface $input, OutputInterface $output): int
  53. {
  54. $io = new SymfonyStyle($input, $output);
  55. $pool = $input->getArgument('pool');
  56. $key = $input->getArgument('key');
  57. $cachePool = $this->poolClearer->getPool($pool);
  58. if (!$cachePool->hasItem($key)) {
  59. $io->note(sprintf('Cache item "%s" does not exist in cache pool "%s".', $key, $pool));
  60. return 0;
  61. }
  62. if (!$cachePool->deleteItem($key)) {
  63. throw new \Exception(sprintf('Cache item "%s" could not be deleted.', $key));
  64. }
  65. $io->success(sprintf('Cache item "%s" was successfully deleted.', $key));
  66. return 0;
  67. }
  68. }