Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b01130d

Browse files
committed
feature #49705 [FrameworkBundle] Add support to easily clear all cache pools (bobvandevijver)
This PR was squashed before being merged into the 6.3 branch. Discussion ---------- [FrameworkBundle] Add support to easily clear all cache pools | Q | A | ------------- | --- | Branch? | 6.3 | Bug fix? | no | New feature? | yes <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tickets | Fix #22047<!-- prefix each issue number with "Fix #", no need to create an issue if none exists, explain below instead --> | License | MIT | Doc PR | symfony/symfony-docs#18072 <!-- required for new features --> <!-- Replace this notice by a short README for your feature/bugfix. This will help reviewers and should be a good start for the documentation. Additionally (see https://symfony.com/releases): - Always add tests and ensure they pass. - Bug fixes must be submitted against the lowest maintained branch where they apply (lowest branches are regularly merged to upper ones so they get the fixes too). - Features and deprecations must be submitted against the latest branch. - For new features, provide some code snippets to help understand usage. - Changelog entry should follow https://symfony.com/doc/current/contributing/code/conventions.html#writing-a-changelog-entry - Never break backward compatibility (see https://symfony.com/bc). --> As suggested in #22047, I've now added a `--all` option to clear all cache pools by just providing that argument to `cache:pool:clear`. I believe this is more convenient than remembering the `cache.global_clearer` clearer alias and more descriptive for the consumer. Commits ------- 3a50d2d [FrameworkBundle] Add support to easily clear all cache pools
2 parents 2e2eaad + 3a50d2d commit b01130d

File tree

3 files changed

+47
-4
lines changed

3 files changed

+47
-4
lines changed

src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ CHANGELOG
2121
* Add autowiring aliases for `Http\Client\HttpAsyncClient`
2222
* Deprecate the `Http\Client\HttpClient` service, use `Psr\Http\Client\ClientInterface` instead
2323
* Add `stop_worker_on_signals` configuration option to `messenger` to define signals which would stop a worker
24+
* Add support for `--all` option to clear all cache pools with `cache:pool:clear` command
2425

2526
6.2
2627
---

src/Symfony/Bundle/FrameworkBundle/Command/CachePoolClearCommand.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Symfony\Component\Console\Exception\InvalidArgumentException;
2020
use Symfony\Component\Console\Input\InputArgument;
2121
use Symfony\Component\Console\Input\InputInterface;
22+
use Symfony\Component\Console\Input\InputOption;
2223
use Symfony\Component\Console\Output\OutputInterface;
2324
use Symfony\Component\Console\Style\SymfonyStyle;
2425
use Symfony\Component\HttpKernel\CacheClearer\Psr6CacheClearer;
@@ -49,8 +50,9 @@ protected function configure(): void
4950
{
5051
$this
5152
->setDefinition([
52-
new InputArgument('pools', InputArgument::IS_ARRAY | InputArgument::REQUIRED, 'A list of cache pools or cache pool clearers'),
53+
new InputArgument('pools', InputArgument::IS_ARRAY | InputArgument::OPTIONAL, 'A list of cache pools or cache pool clearers'),
5354
])
55+
->addOption('all', null, InputOption::VALUE_NONE, 'Clear all cache pools')
5456
->setHelp(<<<'EOF'
5557
The <info>%command.name%</info> command clears the given cache pools or cache pool clearers.
5658
@@ -67,7 +69,19 @@ protected function execute(InputInterface $input, OutputInterface $output): int
6769
$pools = [];
6870
$clearers = [];
6971

70-
foreach ($input->getArgument('pools') as $id) {
72+
$poolNames = $input->getArgument('pools');
73+
if ($input->getOption('all')) {
74+
if (!$this->poolNames) {
75+
throw new InvalidArgumentException('Could not clear all cache pools, try specifying a specific pool or cache clearer.');
76+
}
77+
78+
$io->comment('Clearing all cache pools...');
79+
$poolNames = $this->poolNames;
80+
} elseif (!$poolNames) {
81+
throw new InvalidArgumentException('Either specify at least one pool name, or provide the --all option to clear all pools.');
82+
}
83+
84+
foreach ($poolNames as $id) {
7185
if ($this->poolClearer->hasPool($id)) {
7286
$pools[$id] = $id;
7387
} else {

src/Symfony/Bundle/FrameworkBundle/Tests/Functional/CachePoolClearCommandTest.php

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Bundle\FrameworkBundle\Command\CachePoolClearCommand;
1515
use Symfony\Bundle\FrameworkBundle\Console\Application;
1616
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
17+
use Symfony\Component\Console\Exception\InvalidArgumentException;
1718
use Symfony\Component\Console\Tester\CommandTester;
1819
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
1920
use Symfony\Component\Finder\SplFileInfo;
@@ -76,6 +77,33 @@ public function testClearUnexistingPool()
7677
->execute(['pools' => ['unknown_pool']], ['decorated' => false]);
7778
}
7879

80+
public function testClearAll()
81+
{
82+
$tester = $this->createCommandTester(['cache.app_clearer']);
83+
$tester->execute(['--all' => true], ['decorated' => false]);
84+
85+
$tester->assertCommandIsSuccessful('cache:pool:clear exits with 0 in case of success');
86+
$this->assertStringContainsString('Clearing all cache pools...', $tester->getDisplay());
87+
$this->assertStringContainsString('Calling cache clearer: cache.app_clearer', $tester->getDisplay());
88+
$this->assertStringContainsString('[OK] Cache was successfully cleared.', $tester->getDisplay());
89+
}
90+
91+
public function testClearWithoutPoolNames()
92+
{
93+
$this->expectException(InvalidArgumentException::class);
94+
$this->expectExceptionMessage('Could not clear all cache pools, try specifying a specific pool or cache clearer.');
95+
96+
$this->createCommandTester()->execute(['--all' => true], ['decorated' => false]);
97+
}
98+
99+
public function testClearNoOptions()
100+
{
101+
$this->expectException(InvalidArgumentException::class);
102+
$this->expectExceptionMessage('Either specify at least one pool name, or provide the --all option to clear all pools.');
103+
104+
$this->createCommandTester()->execute([], ['decorated' => false]);
105+
}
106+
79107
public function testClearFailed()
80108
{
81109
$tester = $this->createCommandTester();
@@ -104,10 +132,10 @@ public function testClearFailed()
104132
$this->assertStringContainsString('[WARNING] Cache pool "cache.public_pool" could not be cleared.', $tester->getDisplay());
105133
}
106134

107-
private function createCommandTester()
135+
private function createCommandTester(array $poolNames = null)
108136
{
109137
$application = new Application(static::$kernel);
110-
$application->add(new CachePoolClearCommand(static::getContainer()->get('cache.global_clearer')));
138+
$application->add(new CachePoolClearCommand(static::getContainer()->get('cache.global_clearer'), $poolNames));
111139

112140
return new CommandTester($application->find('cache:pool:clear'));
113141
}

0 commit comments

Comments
 (0)