8000 [FrameworkBundle] Add support to easily clear all cache pools by bobvandevijver · Pull Request #49705 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[FrameworkBundle] Add support to easily clear all cache pools #49705

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ CHANGELOG
* Add autowiring aliases for `Http\Client\HttpAsyncClient`
* Deprecate the `Http\Client\HttpClient` service, use `Psr\Http\Client\ClientInterface` instead
* Add `stop_worker_on_signals` configuration option to `messenger` to define signals which would stop a worker
* Add support for `--all` option to clear all cache pools with `cache:pool:clear` command

6.2
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Symfony\Component\Console\Exception\InvalidArgumentException;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\HttpKernel\CacheClearer\Psr6CacheClearer;
Expand Down Expand Up @@ -49,8 +50,9 @@ protected function configure(): void
{
$this
->setDefinition([
new InputArgument('pools', InputArgument::IS_ARRAY | InputArgument::REQUIRED, 'A list of cache pools or cache pool clearers'),
new InputArgument('pools', InputArgument::IS_ARRAY | InputArgument::OPTIONAL, 'A list of cache pools or cache pool clearers'),
])
->addOption('all', null, InputOption::VALUE_NONE, 'Clear all cache pools')
->setHelp(<<<'EOF'
The <info>%command.name%</info> command clears the given cache pools or cache pool clearers.

Expand All @@ -67,7 +69,19 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$pools = [];
$clearers = [];

foreach ($input->getArgument('pools') as $id) {
$poolNames = $input->getArgument('pools');
if ($ 8000 input->getOption('all')) {
if (!$this->poolNames) {
throw new InvalidArgumentException('Could not clear all cache pools, try specifying a specific pool or cache clearer.');
}

$io->comment('Clearing all cache pools...');
$poolNames = $this->poolNames;
} elseif (!$poolNames) {
throw new InvalidArgumentException('Either specify at least one pool name, or provide the --all option to clear all pools.');
}

foreach ($poolNames as $id) {
if ($this->poolClearer->hasPool($id)) {
$pools[$id] = $id;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Symfony\Bundle\FrameworkBundle\Command\CachePoolClearCommand;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
use Symfony\Component\Console\Exception\InvalidArgumentException;
use Symfony\Component\Console\Tester\CommandTester;
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
use Symfony\Component\Finder\SplFileInfo;
Expand Down Expand Up @@ -76,6 +77,33 @@ public function testClearUnexistingPool()
->execute(['pools' => ['unknown_pool']], ['decorated' => false]);
}

public function testClearAll()
{
$tester = $this->createCommandTester(['cache.app_clearer']);
$tester->execute(['--all' => true], ['decorated' => false]);

$tester->assertCommandIsSuccessful('cache:pool:clear exits with 0 in case of success');
$this->assertStringContainsString('Clearing all cache pools...', $tester->getDisplay());
$this->assertStringContainsString('Calling cache clearer: cache.app_clearer', $tester->getDisplay());
$this->assertStringContainsString('[OK] Cache was successfully cleared.', $tester->getDisplay());
}

public function testClearWithoutPoolNames()
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Could not clear all cache pools, try specifying a specific pool or cache clearer.');

$this->createCommandTester()->execute(['--all' => true], ['decorated' => false]);
}

public function testClearNoOptions()
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Either specify at least one pool name, or provide the --all option to clear all pools.');

$this->createCommandTester()->execute([], ['decorated' => false]);
}

public function testClearFailed()
{
$tester = $this->createCommandTester();
Expand Down Expand Up @@ -104,10 +132,10 @@ public function testClearFailed()
$this->assertStringContainsString('[WARNING] Cache pool "cache.public_pool" could not be cleared.', $tester->getDisplay());
}

private function createCommandTester()
private function createCommandTester(array $poolNames = null)
{
$application = new Application(static::$kernel);
$application->add(new CachePoolClearCommand(static::getContainer()->get('cache.global_clearer')));
$application->add(new CachePoolClearCommand(static::getContainer()->get('cache.global_clearer'), $poolNames));

return new CommandTester($application->find('cache:pool:clear'));
}
Expand Down
0