forked from symfony/symfony
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FrameworkBundle] Add CachePoolClearerPass for weak cache pool refs i…
…n cache clearers
- Loading branch information
1 parent
1f9f87b
commit c4b9f7d
Showing
5 changed files
with
100 additions
and
3 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/CachePoolClearerPass.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) { | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
...ny/Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/CachePoolClearerPassTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |