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

Skip to content

Commit 84ad40d

Browse files
committed
added cache clear hook
1 parent fd12796 commit 84ad40d

File tree

7 files changed

+195
-0
lines changed

7 files changed

+195
-0
lines changed

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

Lines changed: 3 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 {
@@ -168,3 +170,4 @@ protected function getContainerClass()
168170
return new $class($parent->getEnvironment(), $parent->isDebug());
169171
}
170172
}
173+
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
}
42+

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: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
}
28+
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
}
56+
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
}
59+

0 commit comments

Comments
 (0)