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

Skip to content

Commit 05285e4

Browse files
committed
merged branch dustin10/cache-clearers (PR #2857)
Commits ------- 3ae976c fixed CS 84ad40d added cache clear hook Discussion ---------- [Cache][2.1] Added cache clear hook Allows bundles to hook into the `cache:clear` command by using the `kernel.cache_clearer` tag instead of using the `event_dispatcher` service. See #1884 Bug fix: No Feature addition: Yes Backwards compatibility break: No Symfony2 tests pass: Yes Fixes the following tickets: #1884 References the following tickets: #1884 --------------------------------------------------------------------------- by dustin10 at 2011/12/16 11:03:54 -0800 Rebased to squash all commits into one. --------------------------------------------------------------------------- by lsmith77 at 2011/12/17 05:27:29 -0800 @fabpot: we figured that priorities wouldn't be needed for cleaning .. haven't tested the PR, but conceptually it looks good to me and aside from the priority stuff its modeled after the cache warners. --------------------------------------------------------------------------- by dustin10 at 2011/12/19 09:46:26 -0800 @fabpot Updated to pass cache dir to `clear` method. --------------------------------------------------------------------------- by dustin10 at 2011/12/19 10:02:21 -0800 @stof and @fabpot Another thought I just had. Should the `$this->getContainer()->get('cache_clearer')->clear($realCacheDir);` call in the `CacheClearCommand` be done before the warming? --------------------------------------------------------------------------- by stof at 2011/12/19 10:03:59 -0800 indeed. the clearing should be done before the warming. --------------------------------------------------------------------------- by dustin10 at 2011/12/19 10:19:28 -0800 Squashed all commits into one. Let me know if there is anything else. --------------------------------------------------------------------------- by dustin10 at 2011/12/19 10:31:50 -0800 Fixed extra lines.
2 parents ce41b8e + 3ae976c commit 05285e4

File tree

7 files changed

+190
-0
lines changed

7 files changed

+190
-0
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
6464
$kernel = $this->getContainer()->get('kernel');
6565
$output->writeln(sprintf('Clearing the cache for the <info>%s</info> environment with debug <info>%s</info>', $kernel->getEnvironment(), var_export($kernel->isDebug(), true)));
6666

67+
$this->getContainer()->get('cache_clearer')->clear($realCacheDir);
68+
6769
if ($input->getOption('no-warmup')) {
6870
rename($realCacheDir, $oldCacheDir);
6971
} else {
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler;
4+
5+
use Symfony\Component\DependencyInjection\ContainerBuilder;
6+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
7+
use Symfony\Component\DependencyInjection\Reference;
8+
9+
/*
10+
* This file is part of the Symfony framework.
11+
*
12+
* (c) Fabien Potencier <[email protected]>
13+
*
14+
* This source file is subject to the MIT license that is bundled
15+
* with this source code in the file LICENSE.
16+
*/
17+
18+
/**
19+
* Registers the cache clearers.
20+
*
21+
* @author Dustin Dobervich <[email protected]>
22+
*/
23+
class AddCacheClearerPass implements CompilerPassInterface
24+
{
25+
/**
26+
* {@inheritDoc}
27+
*/
28+
public function process(ContainerBuilder $container)
29+
{
30+
if (!$container->hasDefinition('cache_clearer')) {
31+
return;
32+
}
33+
34+
$clearers = array();
35+
foreach ($container->findTaggedServiceIds('kernel.cache_clearer') as $id => $attributes) {
36+
$clearers[] = new Reference($id);
37+
}
38+
39+
$container->getDefinition('cache_clearer')->replaceArgument(0, $clearers);
40+
}
41+
}

src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\ProfilerPass;
2121
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\TranslatorPass;
2222
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddCacheWarmerPass;
23+
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddCacheClearerPass;
2324
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\ContainerBuilderDebugDumpPass;
2425
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\CompilerDebugDumpPass;
2526
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\TranslationExtractorPass;
@@ -59,6 +60,7 @@ public function build(ContainerBuilder $container)
5960
$container->addCompilerPass(new FormPass());
6061
$container->addCompilerPass(new TranslatorPass());
6162
$container->addCompilerPass(new AddCacheWarmerPass());
63+
$container->addCompilerPass(new AddCacheClearerPass());
6264
$container->addCompilerPass(new TranslationExtractorPass());
6365
$container->addCompilerPass(new TranslationDumperPass());
6466

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
<parameter key="http_kernel.class">Symfony\Bundle\FrameworkBundle\HttpKernel</parameter>
1010
<parameter key="filesystem.class">Symfony\Component\HttpKernel\Util\Filesystem</parameter>
1111
<parameter key="cache_warmer.class">Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerAggregate</parameter>
12+
<parameter key="cache_clearer.class">Symfony\Component\HttpKernel\CacheClearer\ChainCacheClearer</parameter>
1213
<parameter key="file_locator.class">Symfony\Component\HttpKernel\Config\FileLocator</parameter>
1314
</parameters>
1415

@@ -27,6 +28,10 @@
2728
<argument type="collection" />
2829
</service>
2930

31+
<service id="cache_clearer" class="%cache_clearer.class%">
32+
<argument type="collection" />
33+
</service>
34+
3035
<!--
3136
If you want to change the Request class, modify the code in
3237
your front controller (app.php) so that it passes an instance of
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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\Component\HttpKernel\CacheClearer;
13+
14+
/**
15+
* CacheClearerInterface.
16+
*
17+
* @author Dustin Dobervich <[email protected]>
18+
*/
19+
interface CacheClearerInterface
20+
{
21+
/**
22+
* Clears any caches necessary.
23+
*
24+
* @param string $cacheDir The cache directory.
25+
*/
26+
function clear($cacheDir);
27+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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\Component\HttpKernel\CacheClearer;
13+
14+
/**
15+
* ChainCacheClearer.
16+
*
17+
* @author Dustin Dobervich <[email protected]>
18+
*/
19+
class ChainCacheClearer implements CacheClearerInterface
20+
{
21+
/**
22+
* @var array $clearers
23+
*/
24+
protected $clearers;
25+
26+
/**
27+
* Constructs a new instance of ChainCacheClearer.
28+
*
29+
* @param array $clearers The initial clearers.
30+
*/
31+
public function __construct(array $clearers = array())
32+
{
33+
$this->clearers = $clearers;
34+
}
35+
36+
/**
37+
* {@inheritDoc}
38+
*/
39+
public function clear($cacheDir)
40+
{
41+
foreach ($this->clearers as $clearer) {
42+
$clearer->clear($cacheDir);
43+
}
44+
}
45+
46+
/**
47+
* Adds a cache clearer to the aggregate.
48+
*
49+
* @param CacheClearerInterface $clearer
50+
*/
51+
public function add(CacheClearerInterface $clearer)
52+
{
53+
$this->clearers[] = $clearer;
54+
}
55+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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\Tests\Component\HttpKernel\CacheClearer;
13+
14+
use Symfony\Component\HttpKernel\CacheClearer\CacheClearerInterface;
15+
use Symfony\Component\HttpKernel\CacheClearer\ChainCacheClearer;
16+
17+
class ChainCacheClearerTest extends \PHPUnit_Framework_TestCase
18+
{
19+
protected static $cacheDir;
20+
21+
public static function setUpBeforeClass()
22+
{
23+
self::$cacheDir = tempnam(sys_get_temp_dir(), 'sf2_cache_clearer_dir');
24+
}
25+
26+
public static function tearDownAfterClass()
27+
{
28+
@unlink(self::$cacheDir);
29+
}
30+
31+
public function testInjectClearersInConstructor()
32+
{
33+
$clearer = $this->getMockClearer();
34+
$clearer
35+
->expects($this->once())
36+
->method('clear');
37+
38+
$chainClearer = new ChainCacheClearer(array($clearer));
39+
$chainClearer->clear(self::$cacheDir);
40+
}
41+
42+
public function testInjectClearerUsingAdd()
43+
{
44+
$clearer = $this->getMockClearer();
45+
$clearer
46+
->expects($this->once())
47+
->method('clear');
48+
49+
$chainClearer = new ChainCacheClearer();
50+
$chainClearer->add($clearer);
51+
$chainClearer->clear(self::$cacheDir);
52+
}
53+
54+
protected function getMockClearer()
55+
{
56+
return $this->getMock('Symfony\Component\HttpKernel\CacheClearer\CacheClearerInterface');
57+
}
58+
}

0 commit comments

Comments
 (0)