|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/* |
| 6 | + * This file is part of the Micro framework package. |
| 7 | + * |
| 8 | + * (c) Stanislau Komar <[email protected]> |
| 9 | + * |
| 10 | + * For the full copyright and license information, please view the LICENSE |
| 11 | + * file that was distributed with this source code. |
| 12 | + */ |
| 13 | + |
| 14 | +namespace Micro\Plugin\Cache\Communication\Cli; |
| 15 | + |
| 16 | +use Micro\Plugin\Cache\Facade\CacheFacadeInterface; |
| 17 | +use Symfony\Component\Console\Command\Command; |
| 18 | +use Symfony\Component\Console\Input\InputArgument; |
| 19 | +use Symfony\Component\Console\Input\InputInterface; |
| 20 | +use Symfony\Component\Console\Output\OutputInterface; |
| 21 | +use Symfony\Component\Console\Style\SymfonyStyle; |
| 22 | + |
| 23 | +class ClearCacheCommand extends Command |
| 24 | +{ |
| 25 | + public function __construct( |
| 26 | + private readonly CacheFacadeInterface $cacheFacade |
| 27 | + ) { |
| 28 | + parent::__construct('cache:clear'); |
| 29 | + } |
| 30 | + |
| 31 | + public function configure(): void |
| 32 | + { |
| 33 | + $this |
| 34 | + ->addArgument('pools', InputArgument::REQUIRED | InputArgument::IS_ARRAY, 'A list of cache pools or cache pool clearer\'s') |
| 35 | + ->setHelp( |
| 36 | + <<<'EOF' |
| 37 | +The <info>%command.name%</info> command clears the given cache pools or cache pool clearer\'s. |
| 38 | +
|
| 39 | + %command.full_name% <cache pool or clearer 1> [...<cache pool or clearer N>] |
| 40 | +EOF |
| 41 | + ) |
| 42 | + ->setDescription('Clear cache pools'); |
| 43 | + } |
| 44 | + |
| 45 | + public function execute(InputInterface $input, OutputInterface $output) |
| 46 | + { |
| 47 | + $io = new SymfonyStyle($input, $output); |
| 48 | + |
| 49 | + foreach ($input->getArgument('pools') as $poolName) { |
| 50 | + $io->comment(sprintf('Clearing cache pool: <info>%s</info>', $poolName)); |
| 51 | + $pool = $this->cacheFacade->getCachePsr6($poolName); |
| 52 | + $pool->clear(); |
| 53 | + } |
| 54 | + |
| 55 | + $io->success('Cache was successfully cleared.'); |
| 56 | + |
| 57 | + return self::SUCCESS; |
| 58 | + } |
| 59 | +} |
0 commit comments