Skip to content

Commit

Permalink
[FrameworkBundle] Add CachePoolClearerPass for weak cache pool refs i…
Browse files Browse the repository at this point in the history
…n cache clearers
  • Loading branch information
nicolas-grekas committed Sep 11, 2016
1 parent 1f9f87b commit c4b9f7d
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 3 deletions.
@@ -0,0 +1,40 @@
<?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\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;

/**
* @author Nicolas Grekas <p@tchwork.com>
*/
final class CachePoolClearerPass implements CompilerPassInterface
{
/**
* {@inheritdoc}
*/
public function process(ContainerBuilder $container)
{
foreach ($container->findTaggedServiceIds('cache.pool') as $id => $attributes) {
foreach (array_reverse($attributes) as $attr) {
if (isset($attr['clearer'])) {
$clearer = $container->getDefinition($attr['clearer']);
$clearer->addMethodCall('addPool', array(new Reference($id)));
}
if (array_key_exists('clearer', $attr)) {
break;
}
}
}
}
}
Expand Up @@ -37,6 +37,7 @@ public function process(ContainerBuilder $container)
}
}

$aliases = $container->getAliases();
$attributes = array(
'provider',
'namespace',
Expand All @@ -57,7 +58,10 @@ public function process(ContainerBuilder $container)
$tags[0]['namespace'] = $this->getNamespace($namespaceSuffix, $id);
}
if (isset($tags[0]['clearer'])) {
$clearer = $container->getDefinition($tags[0]['clearer']);
$clearer = strtolower($tags[0]['clearer']);
while (isset($aliases[$clearer])) {
$clearer = (string) $aliases[$clearer];
}
} else {
$clearer = null;
}
Expand All @@ -78,7 +82,7 @@ public function process(ContainerBuilder $container)
}

if (null !== $clearer) {
$clearer->addMethodCall('addPool', array(new Reference($id)));
$pool->addTag('cache.pool', array('clearer' => $clearer));
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php
Expand Up @@ -15,6 +15,7 @@
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddValidatorInitializersPass;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddConsoleCommandPass;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\CachePoolPass;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\CachePoolClearerPass;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\ControllerArgumentValueResolverPass;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\FormPass;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\PropertyInfoPass;
Expand Down Expand Up @@ -91,6 +92,7 @@ public function build(ContainerBuilder $container)
$container->addCompilerPass(new PropertyInfoPass());
$container->addCompilerPass(new ControllerArgumentValueResolverPass());
$container->addCompilerPass(new CachePoolPass());
$container->addCompilerPass(new CachePoolClearerPass(), PassConfig::TYPE_AFTER_REMOVING);

if ($container->getParameter('kernel.debug')) {
$container->addCompilerPass(new UnusedTagsPass(), PassConfig::TYPE_AFTER_REMOVING);
Expand Down
Expand Up @@ -88,7 +88,7 @@
</call>
</service>

<service id="cache.default_clearer" class="Symfony\Component\HttpKernel\CacheClearer\Psr6CacheClearer" public="false">
<service id="cache.default_clearer" class="Symfony\Component\HttpKernel\CacheClearer\Psr6CacheClearer">
<tag name="kernel.cache_clearer" />
</service>

Expand Down
@@ -0,0 +1,51 @@
<?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\Tests\DependencyInjection\Compiler;

use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\CachePoolClearerPass;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\CachePoolPass;
use Symfony\Component\DependencyInjection\Compiler\RemoveUnusedDefinitionsPass;
use Symfony\Component\DependencyInjection\Compiler\RepeatedPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;

class CachePoolClearerPassTest extends \PHPUnit_Framework_TestCase
{
public function testPoolRefsAreWeak()
{
$container = new ContainerBuilder();

$publicPool = new Definition();
$publicPool->addArgument('namespace');
$publicPool->addTag('cache.pool', array('clearer' => 'clearer_alias'));
$container->setDefinition('public.pool', $publicPool);

$privatePool = new Definition();
$privatePool->setPublic(false);
$privatePool->addArgument('namespace');
$privatePool->addTag('cache.pool', array('clearer' => 'clearer_alias'));
$container->setDefinition('private.pool', $privatePool);

$clearer = new Definition();
$container->setDefinition('clearer', $clearer);
$container->setAlias('clearer_alias', 'clearer');

$pass = new RemoveUnusedDefinitionsPass();
$pass->setRepeatedPass(new RepeatedPass(array($pass)));
foreach (array(new CachePoolPass(), $pass, new CachePoolClearerPass()) as $pass) {
$pass->process($container);
}

$this->assertEquals(array(array('addPool', array(new Reference('public.pool')))), $clearer->getMethodCalls());
}
}

0 comments on commit c4b9f7d

Please sign in to comment.