|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Bundle\FrameworkBundle\Command; |
| 13 | + |
| 14 | +use Psr\Cache\CacheItemPoolInterface; |
| 15 | +use Symfony\Component\Console\Input\InputInterface; |
| 16 | +use Symfony\Component\Console\Input\InputArgument; |
| 17 | +use Symfony\Component\Console\Output\OutputInterface; |
| 18 | +use Symfony\Component\Console\Style\SymfonyStyle; |
| 19 | +use Symfony\Component\HttpKernel\CacheClearer\Psr6CacheClearer; |
| 20 | + |
| 21 | +/** |
| 22 | + * Clear cache pools. |
| 23 | + * |
| 24 | + * @author Nicolas Grekas <[email protected]> |
| 25 | + */ |
| 26 | +class CachePoolClearCommand extends ContainerAwareCommand |
| 27 | +{ |
| 28 | + /** |
| 29 | + * {@inheritdoc} |
| 30 | + */ |
| 31 | + protected function configure() |
| 32 | + { |
| 33 | + $this |
| 34 | + ->setName('cache:pool:clear') |
| 35 | + ->setDefinition(array( |
| 36 | + new InputArgument('pools', InputArgument::IS_ARRAY, 'A list of cache pools or cache pool clearers'), |
| 37 | + )) |
| 38 | + ->setDescription('Clears cache pools') |
| 39 | + ->setHelp(<<<'EOF' |
| 40 | +The <info>%command.name%</info> command clears the given cache pools or cache pool clearers. |
| 41 | +EOF |
| 42 | + ) |
| 43 | + ; |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * {@inheritdoc} |
| 48 | + */ |
| 49 | + protected function execute(InputInterface $input, OutputInterface $output) |
| 50 | + { |
| 51 | + $io = new SymfonyStyle($input, $output); |
| 52 | + $pools = array(); |
| 53 | + $clearers = array(); |
| 54 | + $container = $this->getContainer(); |
| 55 | + $cacheDir = $container->getParameter('kernel.cache_dir'); |
| 56 | + |
| 57 | + foreach ($input->getArgument('pools') as $id) { |
| 58 | + $pool = $container->get($id); |
| 59 | + |
| 60 | + if ($pool instanceof CacheItemPoolInterface) { |
| 61 | + $pools[$id] = $pool; |
| 62 | + } elseif ($pool instanceof Psr6CacheClearer) { |
| 63 | + $clearers[$id] = $pool; |
| 64 | + } else { |
| 65 | + throw new \InvalidArgumentException(sprintf('"%s" is not a cache pool nor a cache clearer.', $id)); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + foreach ($clearers as $id => $clearer) { |
| 70 | + $io->comment(sprintf('Calling cache clearer: <info>%s</info>', $id)); |
| 71 | + $clearer->clear($cacheDir); |
| 72 | + } |
| 73 | + |
| 74 | + foreach ($pools as $id => $pool) { |
| 75 | + $io->comment(sprintf('Clearing cache pool: <info>%s</info>', $id)); |
| 76 | + $pool->clear(); |
| 77 | + } |
| 78 | + |
| 79 | + $io->success('Cache was successfully cleared.'); |
| 80 | + } |
| 81 | +} |
0 commit comments