added cache clear hook

This commit is contained in:
Dustin Dobervich 2011-12-12 16:39:17 -06:00
parent fd12796673
commit 84ad40dcc8
7 changed files with 195 additions and 0 deletions

View File

@ -64,6 +64,8 @@ EOF
$kernel = $this->getContainer()->get('kernel'); $kernel = $this->getContainer()->get('kernel');
$output->writeln(sprintf('Clearing the cache for the <info>%s</info> environment with debug <info>%s</info>', $kernel->getEnvironment(), var_export($kernel->isDebug(), true))); $output->writeln(sprintf('Clearing the cache for the <info>%s</info> environment with debug <info>%s</info>', $kernel->getEnvironment(), var_export($kernel->isDebug(), true)));
$this->getContainer()->get('cache_clearer')->clear($realCacheDir);
if ($input->getOption('no-warmup')) { if ($input->getOption('no-warmup')) {
rename($realCacheDir, $oldCacheDir); rename($realCacheDir, $oldCacheDir);
} else { } else {
@ -168,3 +170,4 @@ EOF;
return new $class($parent->getEnvironment(), $parent->isDebug()); return new $class($parent->getEnvironment(), $parent->isDebug());
} }
} }

View File

@ -0,0 +1,42 @@
<?php
namespace Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\Reference;
/*
* This file is part of the Symfony framework.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
/**
* Registers the cache clearers.
*
* @author Dustin Dobervich <ddobervich@gmail.com>
*/
class AddCacheClearerPass implements CompilerPassInterface
{
/**
* {@inheritDoc}
*/
public function process(ContainerBuilder $container)
{
if (!$container->hasDefinition('cache_clearer')) {
return;
}
$clearers = array();
foreach ($container->findTaggedServiceIds('kernel.cache_clearer') as $id => $attributes) {
$clearers[] = new Reference($id);
}
$container->getDefinition('cache_clearer')->replaceArgument(0, $clearers);
}
}

View File

@ -20,6 +20,7 @@ use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\RoutingResolverP
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\ProfilerPass; use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\ProfilerPass;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\TranslatorPass; use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\TranslatorPass;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddCacheWarmerPass; use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddCacheWarmerPass;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddCacheClearerPass;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\ContainerBuilderDebugDumpPass; use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\ContainerBuilderDebugDumpPass;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\CompilerDebugDumpPass; use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\CompilerDebugDumpPass;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\TranslationExtractorPass; use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\TranslationExtractorPass;
@ -59,6 +60,7 @@ class FrameworkBundle extends Bundle
$container->addCompilerPass(new FormPass()); $container->addCompilerPass(new FormPass());
$container->addCompilerPass(new TranslatorPass()); $container->addCompilerPass(new TranslatorPass());
$container->addCompilerPass(new AddCacheWarmerPass()); $container->addCompilerPass(new AddCacheWarmerPass());
$container->addCompilerPass(new AddCacheClearerPass());
$container->addCompilerPass(new TranslationExtractorPass()); $container->addCompilerPass(new TranslationExtractorPass());
$container->addCompilerPass(new TranslationDumperPass()); $container->addCompilerPass(new TranslationDumperPass());

View File

@ -9,6 +9,7 @@
<parameter key="http_kernel.class">Symfony\Bundle\FrameworkBundle\HttpKernel</parameter> <parameter key="http_kernel.class">Symfony\Bundle\FrameworkBundle\HttpKernel</parameter>
<parameter key="filesystem.class">Symfony\Component\HttpKernel\Util\Filesystem</parameter> <parameter key="filesystem.class">Symfony\Component\HttpKernel\Util\Filesystem</parameter>
<parameter key="cache_warmer.class">Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerAggregate</parameter> <parameter key="cache_warmer.class">Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerAggregate</parameter>
<parameter key="cache_clearer.class">Symfony\Component\HttpKernel\CacheClearer\ChainCacheClearer</parameter>
<parameter key="file_locator.class">Symfony\Component\HttpKernel\Config\FileLocator</parameter> <parameter key="file_locator.class">Symfony\Component\HttpKernel\Config\FileLocator</parameter>
</parameters> </parameters>
@ -27,6 +28,10 @@
<argument type="collection" /> <argument type="collection" />
</service> </service>
<service id="cache_clearer" class="%cache_clearer.class%">
<argument type="collection" />
</service>
<!-- <!--
If you want to change the Request class, modify the code in If you want to change the Request class, modify the code in
your front controller (app.php) so that it passes an instance of your front controller (app.php) so that it passes an instance of

View File

@ -0,0 +1,28 @@
<?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\Component\HttpKernel\CacheClearer;
/**
* CacheClearerInterface.
*
* @author Dustin Dobervich <ddobervich@gmail.com>
*/
interface CacheClearerInterface
{
/**
* Clears any caches necessary.
*
* @param string $cacheDir The cache directory.
*/
function clear($cacheDir);
}

View File

@ -0,0 +1,56 @@
<?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\Component\HttpKernel\CacheClearer;
/**
* ChainCacheClearer.
*
* @author Dustin Dobervich <ddobervich@gmail.com>
*/
class ChainCacheClearer implements CacheClearerInterface
{
/**
* @var array $clearers
*/
protected $clearers;
/**
* Constructs a new instance of ChainCacheClearer.
*
* @param array $clearers The initial clearers.
*/
public function __construct(array $clearers = array())
{
$this->clearers = $clearers;
}
/**
* {@inheritDoc}
*/
public function clear($cacheDir)
{
foreach ($this->clearers as $clearer) {
$clearer->clear($cacheDir);
}
}
/**
* Adds a cache clearer to the aggregate.
*
* @param CacheClearerInterface $clearer
*/
public function add(CacheClearerInterface $clearer)
{
$this->clearers[] = $clearer;
}
}

View File

@ -0,0 +1,59 @@
<?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\Tests\Component\HttpKernel\CacheClearer;
use Symfony\Component\HttpKernel\CacheClearer\CacheClearerInterface;
use Symfony\Component\HttpKernel\CacheClearer\ChainCacheClearer;
class ChainCacheClearerTest extends \PHPUnit_Framework_TestCase
{
protected static $cacheDir;
public static function setUpBeforeClass()
{
self::$cacheDir = tempnam(sys_get_temp_dir(), 'sf2_cache_clearer_dir');
}
public static function tearDownAfterClass()
{
@unlink(self::$cacheDir);
}
public function testInjectClearersInConstructor()
{
$clearer = $this->getMockClearer();
$clearer
->expects($this->once())
->method('clear');
$chainClearer = new ChainCacheClearer(array($clearer));
$chainClearer->clear(self::$cacheDir);
}
public function testInjectClearerUsingAdd()
{
$clearer = $this->getMockClearer();
$clearer
->expects($this->once())
->method('clear');
$chainClearer = new ChainCacheClearer();
$chainClearer->add($clearer);
$chainClearer->clear(self::$cacheDir);
}
protected function getMockClearer()
{
return $this->getMock('Symfony\Component\HttpKernel\CacheClearer\CacheClearerInterface');
}
}