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

Skip to content

Commit c09deb5

Browse files
[FrameworkBundle] Add CachePoolClearerPass for weak cache pool refs in cache clearers
1 parent 1f9f87b commit c09deb5

File tree

5 files changed

+97
-3
lines changed

5 files changed

+97
-3
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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\DependencyInjection\Compiler;
13+
14+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
15+
use Symfony\Component\DependencyInjection\ContainerBuilder;
16+
use Symfony\Component\DependencyInjection\Reference;
17+
18+
/**
19+
* @author Nicolas Grekas <[email protected]>
20+
*/
21+
class CachePoolClearerPass implements CompilerPassInterface
22+
{
23+
/**
24+
* {@inheritdoc}
25+
*/
26+
public function process(ContainerBuilder $container)
27+
{
28+
foreach ($container->findTaggedServiceIds('cache.pool') as $id => $tags) {
29+
foreach ($tags as $attr) {
30+
}
31+
if (isset($attr['clearer'])) {
32+
$clearer = $container->getDefinition($attr['clearer']);
33+
$clearer->addMethodCall('addPool', array(new Reference($id)));
34+
}
35+
}
36+
}
37+
}

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/CachePoolPass.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public function process(ContainerBuilder $container)
3737
}
3838
}
3939

40+
$aliases = $container->getAliases();
4041
$attributes = array(
4142
'provider',
4243
'namespace',
@@ -57,7 +58,10 @@ public function process(ContainerBuilder $container)
5758
$tags[0]['namespace'] = $this->getNamespace($namespaceSuffix, $id);
5859
}
5960
if (isset($tags[0]['clearer'])) {
60-
$clearer = $container->getDefinition($tags[0]['clearer']);
61+
$clearer = strtolower($tags[0]['clearer']);
62+
while (isset($aliases[$clearer])) {
63+
$clearer = (string) $aliases[$clearer];
64+
}
6165
} else {
6266
$clearer = null;
6367
}
@@ -78,7 +82,7 @@ public function process(ContainerBuilder $container)
7882
}
7983

8084
if (null !== $clearer) {
81-
$clearer->addMethodCall('addPool', array(new Reference($id)));
85+
$pool->addTag('cache.pool', array('clearer' => $clearer));
8286
}
8387
}
8488
}

src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddValidatorInitializersPass;
1616
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddConsoleCommandPass;
1717
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\CachePoolPass;
18+
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\CachePoolClearerPass;
1819
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\ControllerArgumentValueResolverPass;
1920
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\FormPass;
2021
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\PropertyInfoPass;
@@ -91,6 +92,7 @@ public function build(ContainerBuilder $container)
9192
$container->addCompilerPass(new PropertyInfoPass());
9293
$container->addCompilerPass(new ControllerArgumentValueResolverPass());
9394
$container->addCompilerPass(new CachePoolPass());
95+
$container->addCompilerPass(new CachePoolClearerPass(), PassConfig::TYPE_AFTER_REMOVING);
9496

9597
if ($container->getParameter('kernel.debug')) {
9698
$container->addCompilerPass(new UnusedTagsPass(), PassConfig::TYPE_AFTER_REMOVING);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@
8888
</call>
8989
</service>
9090

91-
<service id="cache.default_clearer" class="Symfony\Component\HttpKernel\CacheClearer\Psr6CacheClearer" public="false">
91+
<service id="cache.default_clearer" class="Symfony\Component\HttpKernel\CacheClearer\Psr6CacheClearer">
9292
<tag name="kernel.cache_clearer" />
9393
</service>
9494

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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\Tests\DependencyInjection\Compiler;
13+
14+
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\CachePoolClearerPass;
15+
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\CachePoolPass;
16+
use Symfony\Component\DependencyInjection\Compiler\RemoveUnusedDefinitionsPass;
17+
use Symfony\Component\DependencyInjection\Compiler\RepeatedPass;
18+
use Symfony\Component\DependencyInjection\ContainerBuilder;
19+
use Symfony\Component\DependencyInjection\Definition;
20+
use Symfony\Component\DependencyInjection\Reference;
21+
22+
class CachePoolClearerPassTest extends \PHPUnit_Framework_TestCase
23+
{
24+
public function testPoolRefsAreWeak()
25+
{
26+
$container = new ContainerBuilder();
27+
28+
$publicPool = new Definition();
29+
$publicPool->addArgument('namespace');
30+
$publicPool->addTag('cache.pool', array('clearer' => 'clearer_alias'));
31+
$container->setDefinition('public.pool', $publicPool);
32+
33+
$privatePool = new Definition();
34+
$privatePool->setPublic(false);
35+
$privatePool->addArgument('namespace');
36+
$privatePool->addTag('cache.pool', array('clearer' => 'clearer_alias'));
37+
$container->setDefinition('private.pool', $privatePool);
38+
39+
$clearer = new Definition();
40+
$container->setDefinition('clearer', $clearer);
41+
$container->setAlias('clearer_alias', 'clearer');
42+
43+
$pass = new RemoveUnusedDefinitionsPass();
44+
$pass->setRepeatedPass(new RepeatedPass(array($pass)));
45+
foreach (array(new CachePoolPass(), $pass, new CachePoolClearerPass()) as $pass) {
46+
$pass->process($container);
47+
}
48+
49+
$this->assertEquals(array(array('addPool', array(new Reference('public.pool')))), $clearer->getMethodCalls());
50+
}
51+
}

0 commit comments

Comments
 (0)