-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[FrameworkBundle] Add cache:pool:clear command #19891
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<?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 Psr\Cache\CacheItemPoolInterface; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Console\Style\SymfonyStyle; | ||
use Symfony\Component\HttpKernel\CacheClearer\Psr6CacheClearer; | ||
|
||
/** | ||
* Clear cache pools. | ||
* | ||
* @author Nicolas Grekas <p@tchwork.com> | ||
*/ | ||
final class CachePoolClearCommand extends ContainerAwareCommand | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function configure() | ||
{ | ||
$this | ||
->setName('cache:pool:clear') | ||
->setDefinition(array( | ||
new InputArgument('pools', InputArgument::IS_ARRAY, 'A list of cache pools or cache pool clearers'), | ||
)) | ||
->setDescription('Clears cache pools') | ||
->setHelp(<<<'EOF' | ||
The <info>%command.name%</info> command clears the given cache pools or cache pool clearers. | ||
|
||
%command.full_name% <cache pool or clearer 1> [...<cache pool or clearer N>] | ||
EOF | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You need to add an example here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. example added |
||
) | ||
; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function execute(InputInterface $input, OutputInterface $output) | ||
{ | ||
$io = new SymfonyStyle($input, $output); | ||
$pools = array(); | ||
$clearers = array(); | ||
$container = $this->getContainer(); | ||
$cacheDir = $container->getParameter('kernel.cache_dir'); | ||
|
||
foreach ($input->getArgument('pools') as $id) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If there is no pools, what about clearing them all? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. too risky IMHO: there can be important operational data in the cache There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fair enough |
||
$pool = $container->get($id); | ||
|
||
if ($pool instanceof CacheItemPoolInterface) { | ||
$pools[$id] = $pool; | ||
} elseif ($pool instanceof Psr6CacheClearer) { | ||
$clearers[$id] = $pool; | ||
} else { | ||
throw new \InvalidArgumentException(sprintf('"%s" is not a cache pool nor a cache clearer.', $id)); | ||
} | ||
} | ||
|
||
foreach ($clearers as $id => $clearer) { | ||
$io->comment(sprintf('Calling cache clearer: <info>%s</info>', $id)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the clearer/pool specific messages should be displayed only if the output is verbose and maybe a global comment should be added before e.g. "Clearing the cache for pools ...", wdyt? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't know :) Doesn't look verbose to me. (Not as an exception trace e.g.). So I'll follow any advises. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, it's not very verbose until you have a lot of pools, I just refer to the clear cache command which hide details by default |
||
$clearer->clear($cacheDir); | ||
} | ||
|
||
foreach ($pools as $id => $pool) { | ||
$io->comment(sprintf('Clearing cache pool: <info>%s</info>', $id)); | ||
$pool->clear(); | ||
} | ||
|
||
$io->success('Cache was successfully cleared.'); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just interesting - why not use
addArgument
here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All the other commands use
setDefinition
, that's the reason :)