Skip to content

Commit

Permalink
feature #51058 [FrameworkBundle] Add --exclude option to the `cache…
Browse files Browse the repository at this point in the history
…:pool:clear` command (MatTheCat)

This PR was merged into the 6.4 branch.

Discussion
----------

[FrameworkBundle] Add `--exclude` option to the `cache:pool:clear` command

| Q             | A
| ------------- | ---
| Branch?       | 6.4
| Bug fix?      | no
| New feature?  | yes
| Deprecations? | no
| Tickets       | Fix #51023
| License       | MIT
| Doc PR        | symfony/symfony-docs#18595

For now this PR just ignores excluded pools/clearers when they don’t exist or wouldn’t be cleared/run anyways. Not sure what the best DX would be 🤔

Commits
-------

21d0348 [FrameworkBundle] Add `--exclude` option to the `cache:pool:clear` command
  • Loading branch information
fabpot committed Oct 11, 2023
2 parents fc12885 + 21d0348 commit 2101962
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ CHANGELOG
* Add support for relative URLs in BrowserKit's redirect assertion
* Change BrowserKitAssertionsTrait::getClient() to be protected
* Deprecate the `framework.asset_mapper.provider` config option
* Add `--exclude` option to the `cache:pool:clear` command

6.3
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ protected function configure(): void
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')
->addOption('exclude', null, InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, 'A list of cache pools or cache pool clearers to exclude')
->setHelp(<<<'EOF'
The <info>%command.name%</info> command clears the given cache pools or cache pool clearers.
Expand All @@ -70,17 +71,23 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$clearers = [];

$poolNames = $input->getArgument('pools');
$excludedPoolNames = $input->getOption('exclude');
if ($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...');
if (!$excludedPoolNames) {
$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.');
}

$poolNames = array_diff($poolNames, $excludedPoolNames);

foreach ($poolNames as $id) {
if ($this->poolClearer->hasPool($id)) {
$pools[$id] = $id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,17 @@ public function testClearFailed()
$this->assertStringContainsString('[WARNING] Cache pool "cache.public_pool" could not be cleared.', $tester->getDisplay());
}

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

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

private function createCommandTester(array $poolNames = null)
{
$application = new Application(static::$kernel);
Expand Down

0 comments on commit 2101962

Please sign in to comment.