-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
src/Symfony/Bundle/FrameworkBundle/Command/CachePoolInvalidateTagsCommand.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\FrameworkBundle\Command; | ||
|
||
use Symfony\Component\Console\Attribute\AsCommand; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Completion\CompletionInput; | ||
use Symfony\Component\Console\Completion\CompletionSuggestions; | ||
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\DependencyInjection\Exception\ServiceNotFoundException; | ||
use Symfony\Contracts\Cache\TagAwareCacheInterface; | ||
use Symfony\Contracts\Service\ServiceProviderInterface; | ||
|
||
/** | ||
* @author Kevin Bond <kevinbond@gmail.com> | ||
*/ | ||
#[AsCommand(name: 'cache:pool:invalidate-tags', description: 'Invalidate cache tags for all or a specific pool')] | ||
final class CachePoolInvalidateTagsCommand extends Command | ||
{ | ||
private ServiceProviderInterface $pools; | ||
private array $poolNames; | ||
|
||
public function __construct(ServiceProviderInterface $pools) | ||
{ | ||
parent::__construct(); | ||
|
||
$this->pools = $pools; | ||
$this->poolNames = array_keys($pools->getProvidedServices()); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function configure(): void | ||
{ | ||
$this | ||
->addArgument('tags', InputArgument::IS_ARRAY | InputArgument::REQUIRED, 'The tags to invalidate') | ||
->addOption('pool', 'p', InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'The pools to invalidate on') | ||
->setHelp(<<<'EOF' | ||
The <info>%command.name%</info> command invalidates tags from taggable pools. By default, all pools | ||
have the passed tags invalidated. Pass <info>--pool=my_pool</info> to invalidate tags on a specific pool. | ||
|
||
php %command.full_name% tag1 tag2 | ||
php %command.full_name% tag1 tag2 --pool=cache2 --pool=cache1 | ||
EOF) | ||
; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
$io = new SymfonyStyle($input, $output); | ||
$pools = $input->getOption('pool') ?: $this->poolNames; | ||
$tags = $input->getArgument('tags'); | ||
$tagList = implode(', ', $tags); | ||
$errors = false; | ||
|
||
foreach ($pools as $name) { | ||
$io->comment(sprintf('Invalidating tag(s): <info>%s</info> from pool <comment>%s</comment>.', $tagList, $name)); | ||
|
||
try { | ||
$pool = $this->pools->get($name); | ||
} catch (ServiceNotFoundException) { | ||
$io->error(sprintf('Pool "%s" not found.', $name)); | ||
$errors = true; | ||
|
||
continue; | ||
} | ||
|
||
if (!$pool instanceof TagAwareCacheInterface) { | ||
$io->error(sprintf('Pool "%s" is not taggable.', $name)); | ||
$errors = true; | ||
|
||
continue; | ||
} | ||
|
||
if (!$pool->invalidateTags($tags)) { | ||
$io->error(sprintf('Cache tag(s) "%s" could not be invalidated for pool "%s".', $tagList, $name)); | ||
$errors = true; | ||
} | ||
} | ||
|
||
if ($errors) { | ||
$io->error('Done but with errors.'); | ||
|
||
return self::FAILURE; | ||
} | ||
|
||
$io->success('Successfully invalidated cache tags.'); | ||
|
||
return self::SUCCESS; | ||
} | ||
|
||
public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void | ||
{ | ||
if ($input->mustSuggestOptionValuesFor('pool')) { | ||
$suggestions->suggestValues($this->poolNames); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
138 changes: 138 additions & 0 deletions
138
src/Symfony/Bundle/FrameworkBundle/Tests/Command/CachePoolInvalidateTagsCommandTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\FrameworkBundle\Tests\Command; | ||
|
||
use Symfony\Bundle\FrameworkBundle\Command\CachePoolInvalidateTagsCommand; | ||
use Symfony\Bundle\FrameworkBundle\Tests\TestCase; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Tester\CommandCompletionTester; | ||
use Symfony\Component\Console\Tester\CommandTester; | ||
use Symfony\Component\DependencyInjection\ServiceLocator; | ||
use Symfony\Contracts\Cache\TagAwareCacheInterface; | ||
|
||
class CachePoolInvalidateTagsCommandTest extends TestCase | ||
{ | ||
public function testComplete() | ||
{ | ||
$tester = new CommandCompletionTester($this->createCommand(['foo' => null, 'bar' => null])); | ||
|
||
$suggestions = $tester->complete(['--pool=']); | ||
|
||
$this->assertSame(['foo', 'bar'], $suggestions); | ||
} | ||
|
||
public function testInvalidatesTagsForAllPoolsByDefault() | ||
{ | ||
$tagsToInvalidate = ['tag1', 'tag2']; | ||
|
||
$foo = $this->createMock(TagAwareCacheInterface::class); | ||
$foo->expects($this->once())->method('invalidateTags')->with($tagsToInvalidate)->willReturn(true); | ||
|
||
$bar = $this->createMock(TagAwareCacheInterface::class); | ||
$bar->expects($this->once())->method('invalidateTags')->with($tagsToInvalidate)->willReturn(true); | ||
|
||
$tester = new CommandTester($this->createCommand([ | ||
'foo' => $foo, | ||
'bar' => $bar, | ||
])); | ||
|
||
$ret = $tester->execute(['tags' => $tagsToInvalidate]); | ||
|
||
$this->assertSame(Command::SUCCESS, $ret); | ||
} | ||
|
||
public function testCanInvalidateSpecificPools() | ||
{ | ||
$tagsToInvalidate = ['tag1', 'tag2']; | ||
|
||
$foo = $this->createMock(TagAwareCacheInterface::class); | ||
$foo->expects($this->once())->method('invalidateTags')->with($tagsToInvalidate)->willReturn(true); | ||
|
||
$bar = $this->createMock(TagAwareCacheInterface::class); | ||
$bar->expects($this->never())->method('invalidateTags'); | ||
|
||
$tester = new CommandTester($this->createCommand([ | ||
'foo' => $foo, | ||
'bar' => $bar, | ||
])); | ||
|
||
$ret = $tester->execute(['tags' => $tagsToInvalidate, '--pool' => ['foo']]); | ||
|
||
$this->assertSame(Command::SUCCESS, $ret); | ||
} | ||
|
||
public function testCommandFailsIfPoolNotFound() | ||
{ | ||
$tagsToInvalidate = ['tag1', 'tag2']; | ||
|
||
$foo = $this->createMock(TagAwareCacheInterface::class); | ||
$foo->expects($this->once())->method('invalidateTags')->with($tagsToInvalidate)->willReturn(true); | ||
|
||
$bar = $this->createMock(TagAwareCacheInterface::class); | ||
$bar->expects($this->never())->method('invalidateTags'); | ||
|
||
$tester = new CommandTester($this->createCommand([ | ||
'foo' => $foo, | ||
'bar' => $bar, | ||
])); | ||
|
||
$ret = $tester->execute(['tags' => $tagsToInvalidate, '--pool' => ['invalid', 'foo']]); | ||
|
||
$this->assertSame(Command::FAILURE, $ret); | ||
} | ||
|
||
public function testCommandFailsIfPoolNotTaggable() | ||
{ | ||
$tagsToInvalidate = ['tag1', 'tag2']; | ||
|
||
$foo = new \stdClass(); | ||
|
||
$bar = $this->createMock(TagAwareCacheInterface::class); | ||
$bar->expects($this->once())->method('invalidateTags')->with($tagsToInvalidate)->willReturn(true); | ||
|
||
$tester = new CommandTester($this->createCommand([ | ||
'foo' => $foo, | ||
'bar' => $bar, | ||
])); | ||
|
||
$ret = $tester->execute(['tags' => $tagsToInvalidate]); | ||
|
||
$this->assertSame(Command::FAILURE, $ret); | ||
} | ||
|
||
public function testCommandFailsIfInvalidatingTagsFails() | ||
{ | ||
$tagsToInvalidate = ['tag1', 'tag2']; | ||
|
||
$foo = $this->createMock(TagAwareCacheInterface::class); | ||
$foo->expects($this->once())->method('invalidateTags')->with($tagsToInvalidate)->willReturn(false); | ||
|
||
$bar = $this->createMock(TagAwareCacheInterface::class); | ||
$bar->expects($this->once())->method('invalidateTags')->with($tagsToInvalidate)->willReturn(true); | ||
|
||
$tester = new CommandTester($this->createCommand([ | ||
'foo' => $foo, | ||
'bar' => $bar, | ||
])); | ||
|
||
$ret = $tester->execute(['tags' => $tagsToInvalidate]); | ||
|
||
$this->assertSame(Command::FAILURE, $ret); | ||
} | ||
|
||
private function createCommand(array $services): CachePoolInvalidateTagsCommand | ||
{ | ||
return new CachePoolInvalidateTagsCommand( | ||
new ServiceLocator(array_map(fn ($service) => fn () => $service, $services)) | ||
); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
[Cache][FrameworkBundle] add
cache:pool:invalidate-tags
command #44692New 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
Uh oh!
There was an error while loading. Please reload this page.
[Cache][FrameworkBundle] add
cache:pool:invalidate-tags
command #44692Changes from all commits
0657f14
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.