Skip to content

[FrameworkBundle] Add CachePoolClearerPass for weak cache pool refs in cache clearers #19900

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 14, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand logic behind this code (array_reverse + break). Do you want to emulate overriding of the tag? Does it mean that a service cannot have 2 different clearers?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cannot have 2 different clearers

True: neither in 3.1 nor with this PR. Could be worth it, I'll give it a try.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But not in this PR, that's another topic :)

break;
}
}
}
}
}
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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());
}
}