CachePoolClearCommand.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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\Exception\InvalidArgumentException;
  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. * Clear cache pools.
  21. *
  22. * @author Nicolas Grekas <p@tchwork.com>
  23. */
  24. final class CachePoolClearCommand extends Command
  25. {
  26. protected static $defaultName = 'cache:pool:clear';
  27. private $poolClearer;
  28. public function __construct(Psr6CacheClearer $poolClearer)
  29. {
  30. parent::__construct();
  31. $this->poolClearer = $poolClearer;
  32. }
  33. /**
  34. * {@inheritdoc}
  35. */
  36. protected function configure()
  37. {
  38. $this
  39. ->setDefinition([
  40. new InputArgument('pools', InputArgument::IS_ARRAY | InputArgument::REQUIRED, 'A list of cache pools or cache pool clearers'),
  41. ])
  42. ->setDescription('Clear cache pools')
  43. ->setHelp(<<<'EOF'
  44. The <info>%command.name%</info> command clears the given cache pools or cache pool clearers.
  45. %command.full_name% <cache pool or clearer 1> [...<cache pool or clearer N>]
  46. EOF
  47. )
  48. ;
  49. }
  50. /**
  51. * {@inheritdoc}
  52. */
  53. protected function execute(InputInterface $input, OutputInterface $output): int
  54. {
  55. $io = new SymfonyStyle($input, $output);
  56. $kernel = $this->getApplication()->getKernel();
  57. $pools = [];
  58. $clearers = [];
  59. foreach ($input->getArgument('pools') as $id) {
  60. if ($this->poolClearer->hasPool($id)) {
  61. $pools[$id] = $id;
  62. } else {
  63. $pool = $kernel->getContainer()->get($id);
  64. if ($pool instanceof CacheItemPoolInterface) {
  65. $pools[$id] = $pool;
  66. } elseif ($pool instanceof Psr6CacheClearer) {
  67. $clearers[$id] = $pool;
  68. } else {
  69. throw new InvalidArgumentException(sprintf('"%s" is not a cache pool nor a cache clearer.', $id));
  70. }
  71. }
  72. }
  73. foreach ($clearers as $id => $clearer) {
  74. $io->comment(sprintf('Calling cache clearer: <info>%s</info>', $id));
  75. $clearer->clear($kernel->getContainer()->getParameter('kernel.cache_dir'));
  76. }
  77. foreach ($pools as $id => $pool) {
  78. $io->comment(sprintf('Clearing cache pool: <info>%s</info>', $id));
  79. if ($pool instanceof CacheItemPoolInterface) {
  80. $pool->clear();
  81. } else {
  82. $this->poolClearer->clearPool($id);
  83. }
  84. }
  85. $io->success('Cache was successfully cleared.');
  86. return 0;
  87. }
  88. }