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

Skip to content

Commit 3f96928

Browse files
[FrameworkBundle] Add cache:pool:clear command
1 parent 1f9f87b commit 3f96928

File tree

2 files changed

+84
-1
lines changed

2 files changed

+84
-1
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\FrameworkBundle\Command;
13+
14+
use Psr\Cache\CacheItemPoolInterface;
15+
use Symfony\Component\Console\Input\InputInterface;
16+
use Symfony\Component\Console\Input\InputArgument;
17+
use Symfony\Component\Console\Output\OutputInterface;
18+
use Symfony\Component\Console\Style\SymfonyStyle;
19+
use Symfony\Component\HttpKernel\CacheClearer\Psr6CacheClearer;
20+
21+
/**
22+
* Clear cache pools.
23+
*
24+
* @author Nicolas Grekas <[email protected]>
25+
*/
26+
class CachePoolClearCommand extends ContainerAwareCommand
27+
{
28+
/**
29+
* {@inheritdoc}
30+
*/
31+
protected function configure()
32+
{
33+
$this
34+
->setName('cache:pool:clear')
35+
->setDefinition(array(
36+
new InputArgument('pools', InputArgument::IS_ARRAY, 'A list of cache pools or cache pool clearers'),
37+
))
38+
->setDescription('Clears cache pools')
39+
->setHelp(<<<'EOF'
40+
The <info>%command.name%</info> command clears the given cache pools or cache pool clearers.
41+
EOF
42+
)
43+
;
44+
}
45+
46+
/**
47+
* {@inheritdoc}
48+
*/
49+
protected function execute(InputInterface $input, OutputInterface $output)
50+
{
51+
$io = new SymfonyStyle($input, $output);
52+
$pools = array();
53+
$clearers = array();
54+
$container = $this->getContainer();
55+
$cacheDir = $container->getParameter('kernel.cache_dir');
56+
57+
foreach ($input->getArgument('pools') as $id) {
58+
$pool = $container->get($id);
59+
60+
if ($pool instanceof CacheItemPoolInterface) {
61+
$pools[$id] = $pool;
62+
} elseif ($pool instanceof Psr6CacheClearer) {
63+
$clearers[$id] = $pool;
64+
} else {
65+
throw new \InvalidArgumentException(sprintf('"%s" is not a cache pool nor a cache clearer.', $id));
66+
}
67+
}
68+
69+
foreach ($clearers as $id => $clearer) {
70+
$io->comment(sprintf('Calling cache clearer: <info>%s</info>', $id));
71+
$clearer->clear($cacheDir);
72+
}
73+
74+
foreach ($pools as $id => $pool) {
75+
$io->comment(sprintf('Clearing cache pool: <info>%s</info>', $id));
76+
$pool->clear();
77+
}
78+
79+
$io->success('Cache was successfully cleared.');
80+
}
81+
}

src/Symfony/Bundle/FrameworkBundle/Resources/config/cache.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<services>
88

99
<service id="cache.app" parent="cache.adapter.filesystem">
10-
<tag name="cache.pool" />
10+
<tag name="cache.pool" clearer="cache.app_clearer" />
1111
</service>
1212

1313
<service id="cache.system" parent="cache.adapter.system">
@@ -92,5 +92,7 @@
9292
<tag name="kernel.cache_clearer" />
9393
</service>
9494

95+
<service id="cache.app_clearer" alias="cache.default_clearer" />
96+
9597
</services>
9698
</container>

0 commit comments

Comments
 (0)