8000 [FrameworkBundle] Fix and add tests for cache pool wiring · symfony/symfony@92b1a20 · GitHub
[go: up one dir, main page]

Skip to content

Commit 92b1a20

Browse files
xabbuhnicolas-grekas
authored andcommitted
[FrameworkBundle] Fix and add tests for cache pool wiring
1 parent e44bfdc commit 92b1a20

File tree

10 files changed

+163
-162
lines changed

10 files changed

+163
-162
lines changed

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
1515
use Symfony\Component\DependencyInjection\ContainerBuilder;
1616
use Symfony\Component\DependencyInjection\DefinitionDecorator;
17-
use Symfony\Component\DependencyInjection\Reference;
1817

1918
/**
2019
* @author Nicolas Grekas <p@tchwork.com>
@@ -28,7 +27,6 @@ public function process(ContainerBuilder $container)
2827
{
2928
foreach ($container->findTaggedServiceIds('cache.pool') as $id => $tags) {
3029
$pool = $container->getDefinition($id);
31-
$namespaceArgIndex = isset($tags[0]['namespace_arg_index']) ? $tags[0]['namespace_arg_index'] : -1;
3230

3331
if (!$pool instanceof DefinitionDecorator) {
3432
throw new \InvalidArgumentException(sprintf('Services tagged with "cache.pool" must have a parent service but "%s" has none.', $id));
@@ -39,7 +37,11 @@ public function process(ContainerBuilder $container)
3937
do {
4038
$adapterId = $adapter->getParent();
4139
$adapter = $container->getDefinition($adapterId);
42-
} while ($adapter instanceof DefinitionDecorator && !$adapter->getTag('cache.adapter'));
40+
} while ($adapter instanceof DefinitionDecorator && !$adapter->hasTag('cache.adapter'));
41+
42+
if (!$adapter->hasTag('cache.adapter')) {
43+
throw new \InvalidArgumentException(sprintf('Services tagged with "cache.pool" must have a parent service tagged with "cache.adapter" but "%s" has none.', $id));
44+
}
4345

4446
$tags = $adapter->getTag('cache.adapter');
4547

@@ -51,14 +53,14 @@ public function process(ContainerBuilder $container)
5153
throw new \InvalidArgumentException(sprintf('Services tagged as "cache.adapter" must be abstract: "%s" is not.', $adapterId));
5254
}
5355

54-
if (0 <= $namespaceArgIndex) {
56+
if (0 <= $namespaceArgIndex = $tags[0]['namespace_arg_index']) {
5557
$pool->replaceArgument($namespaceArgIndex, $this->getNamespace($id));
5658
}
5759
}
5860
}
5961

6062
private function getNamespace($id)
6163
{
62-
return substr(str_replace('/', '-', base64_encode(md5('symfony.'.$id, true)), 0, 10));
64+
return substr(str_replace('/', '-', base64_encode(md5('symfony.'.$id, true))), 0, 10);
6365
}
6466
}

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -557,18 +557,16 @@ private function addCacheSection(ArrayNodeDefinition $rootNode)
557557
->info('Cache configuration')
558558
->fixXmlConfig('pool')
559559
->children()
560-
->arrayNode('pool')
560+
->arrayNode('pools')
561561
->useAttributeAsKey('name')
562562
->prototype('array')
563-
->beforeNormalization()
564-
->end()
565563
->children()
566564
->enumNode('type')
567565
->info('The cache pool type (one of "apcu", "doctrine", "psr6" or "filesystem")')
568566
->isRequired()
569567
->values(array('apcu', 'doctrine', 'psr6', 'filesystem'))
570568
->end()
571-
->integerNode('default_lifetime')->default(0)->end()
569+
->integerNode('default_lifetime')->defaultValue(0)->end()
572570
->scalarNode('cache_provider_service')->defaultNull()->end()
573571
->scalarNode('directory')->defaultNull()->end()
574572
->end()

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ public function load(array $configs, ContainerBuilder $container)
139139
}
140140

141141
if (isset($config['cache'])) {
142-
$this->registerCacheConfiguration($config['cache'], $container);
142+
$this->registerCacheConfiguration($config['cache'], $container, $loader);
143143
}
144144

145145
$loader->load('debug_prod.xml');
@@ -1022,11 +1022,11 @@ private function registerPropertyInfoConfiguration(array $config, ContainerBuild
10221022

10231023
private function registerCacheConfiguration(array $config, ContainerBuilder $container, XmlFileLoader $loader)
10241024
{
1025-
if (!empty($config['pool'])) {
1025+
if (!empty($config['pools'])) {
10261026
$loader->load('cache_adapters.xml');
10271027
}
10281028

1029-
foreach ($config['pool'] as $name => $poolConfig) {
1029+
foreach ($config['pools'] as $name => $poolConfig) {
10301030
$poolDefinition = new DefinitionDecorator('cache.adapter.'.$poolConfig['type']);
10311031
$poolDefinition->replaceArgument(1, $poolConfig['default_lifetime']);
10321032

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,27 @@
77
<services>
88

99
<service id="cache.adapter.apcu" class="Symfony\Component\Cache\Adapter\ApcuAdapter" abstract="true">
10-
<tag name="cache.adapter" namespace-arg-index="0"></tag>
10+
<tag name="cache.adapter" namespace-arg-index="0" />
1111
<argument /> <!-- namespace -->
1212
<argument /> <!-- default lifetime -->
1313
</service>
1414

1515
<service id="cache.adapter.doctrine" class="Symfony\Component\Cache\Adapter\DoctrineAdapter" abstract="true">
16-
<tag name="cache.adapter" namespace-arg-index="2"></tag>
16+
<tag name="cache.adapter" namespace-arg-index="2" />
1717
<argument /> <!-- doctrine provider service -->
1818
<argument /> <!-- default lifetime -->
1919
<argument /> <!-- namespace -->
2020
</service>
2121

2222
<service id="cache.adapter.psr6" class="Symfony\Component\Cache\Adapter\ProxyAdapter" abstract="true">
23-
<tag name="cache.adapter" namespace-arg-index="2"></tag>
23+
<tag name="cache.adapter" namespace-arg-index="2" />
2424
<argument /> <!-- PSR-6 provider service -->
2525
<argument /> <!-- default lifetime -->
2626
<argument /> <!-- namespace -->
2727
</service>
2828

2929
<service id="cache.adapter.filesystem" class="Symfony\Component\Cache\Adapter\FilesystemAdapter" abstract="true">
30-
<tag name="cache.adapter" namespace-arg-index="2"></tag>
30+
<tag name="cache.adapter" namespace-arg-index="2" />
3131
<argument>%kernel.cache_dir%</argument>
3232
<argument /> <!-- default lifetime -->
3333
<argument /> <!-- namespace -->

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/CachePoolPassTest.php

Lines changed: 0 additions & 109 deletions
This file was deleted.
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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\CachePoolPass;
15+
use Symfony\Component\DependencyInjection\ContainerBuilder;
16+
use Symfony\Component\DependencyInjection\Definition;
17+
use Symfony\Component\DependencyInjection\DefinitionDecorator;
18+
19+
class CachePoolPassTest extends \PHPUnit_Framework_TestCase
20+
{
21+
private $cachePoolPass;
22+
23+
protected function setUp()
24+
{
25+
$this->cachePoolPass = new CachePoolPass();
26+
}
F438
27+
28+
public function testNamespaceArgumentIsReplaced()
29+
{
30+
$container = new ContainerBuilder();
31+
$adapter = new Definition();
32+
$adapter->setAbstract(true);
33+
$adapter->addTag('cache.adapter', array('namespace_arg_index' => 0));
34+
$container->setDefinition('app.cache_adapter', $adapter);
35+
$cachePool = new DefinitionDecorator('app.cache_adapter');
36+
$cachePool->addArgument(null);
37+
$cachePool->addTag('cache.pool');
38+
$container->setDefinition('app.cache_pool', $cachePool);
39+
40+
$this->cachePoolPass->process($container);
41+
42+
$this->assertSame('yRnzIIVLvL', $cachePool->getArgument(0));
43+
}
44+
45+
/**
46+
* @expectedException \InvalidArgumentException
47+
* @expectedExceptionMessage Services tagged with "cache.pool" must have a parent service but "app.cache_pool" has none.
48+
*/
49+
public function testThrowsExceptionWhenCachePoolHasNoParentDefinition()
50+
{
51+
$container = new ContainerBuilder();
52+
$cachePool = new Definition();
53+
$cachePool->addTag('cache.pool');
54+
$container->setDefinition('app.cache_pool', $cachePool);
55+
56+
$this->cachePoolPass->process($container);
57+
}
58+
59+
/**
60+
* @expectedException \InvalidArgumentException
61+
* @expectedExceptionMessage Services tagged with "cache.pool" must have a parent service tagged with "cache.adapter" but "app.cache_pool" has none.
62+
*/
63+
public function testThrowsExceptionWhenCachePoolIsNotBasedOnAdapter()
64+
{
65+
$container = new ContainerBuilder();
66+
$container->register('app.cache_adapter');
67+
$cachePool = new DefinitionDecorator('app.cache_adapter');
68+
$cachePool->addTag('cache.pool');
69+
$container->setDefinition('app.cache_pool', $cachePool);
70+
71+
$this->cachePoolPass->process($container);
72+
}
73+
74+
/**
75+
* @expectedException \InvalidArgumentException
76+
* @expectedExceptionMessage Invalid "cache.adapter" tag for service "app.cache_adapter": attribute "namespace_arg_index" is missing.
77+
*/
78+
public function testThrowsExceptionWhenCacheAdapterDefinesNoNamespaceArgument()
79+
{
80+
$container = new ContainerBuilder();
81+
$adapter = new Definition();
82+
$adapter->setAbstract(true);
83+
$adapter->addTag('cache.adapter');
84+
$container->setDefinition('app.cache_adapter', $adapter);
85+
$cachePool = new DefinitionDecorator('app.cache_adapter');
86+
$cachePool->addTag('cache.pool');
87+
$container->setDefinition('app.cache_pool', $cachePool);
88+
89+
$this->cachePoolPass->process($container);
90+
}
91+
92+
/**
93+
* @expectedException \InvalidArgumentException
94+
* @expectedExceptionMessage Services tagged as "cache.adapter" must be abstract: "app.cache_adapter" is not.
95+
*/
96+
public function testThrowsExceptionWhenCacheAdapterIsNotAbstract()
97+
{
98+
$container = new ContainerBuilder();
99+
$adapter = new Definition();
100+
$adapter->addTag('cache.adapter', array('namespace_arg_index' => 0));
101+
$container->setDefinition('app.cache_adapter', $adapter);
102+
$cachePool = new DefinitionDecorator('app.cache_adapter');
103+
$cachePool->addTag('cache.pool');
104+
$container->setDefinition('app.cache_pool', $cachePool);
105+
106+
$this->cachePoolPass->process($container);
107+
}
108+
}

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/cache.php

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,25 @@
22

33
$container->loadFromExtension('framework', array(
44
'cache' => array(
5-
'adapters' => array(
5+
'pools' => array(
66
'foo' => array(
77
'type' => 'apcu',
8-
'options' => array(
9-
'default_lifetime' => 30,
10-
),
8+
'default_lifetime' => 30,
119
),
1210
'bar' => array(
1311
'type' => 'doctrine',
14-
'options' => array(
15-
'default_lifetime' => 5,
16-
'cache_provider_service' => 'app.doctrine_cache_provider',
17-
),
12+
'default_lifetime' => 5,
13+
'cache_provider_service' => 'app.doctrine_cache_provider',
1814
),
1915
'baz' => array(
2016
'type' => 'filesystem',
21-
'options' => array(
22-
'default_lifetime' => 7,
23-
'directory' => 'app/cache/psr',
24-
),
17+
'default_lifetime' => 7,
18+
'directory' => 'app/cache/psr',
19+
),
20+
'foobar' => array(
21+
'type' => 'psr6',
22+
'default_lifetime' => 10,
23+
'cache_provider_service' => 'app.cache_pool',
2524
),
2625
),
2726
),

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/cache.xml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77

88
<framework:config>
99
<framework:cache>
10-
<framework:adapter name="foo" type="apcu" default-lifetime="30" />
11-
<framework:adapter name="bar" type="doctrine" default-lifetime="5" cache-provider-service="app.doctrine_cache_provider" />
12-
<framework:adapter name="baz" type="filesystem" default-lifetime="7" directory="app/cache/psr" />
10+
<framework:pool name="foo" type="apcu" default-lifetime="30" />
11+
<framework:pool name="bar" type="doctrine" default-lifetime="5" cache-provider-service="app.doctrine_cache_provider" />
12+
<framework:pool name="baz" type="filesystem" default-lifetime="7" directory="app/cache/psr" />
13+
<framework:pool name="foobar" type="psr6" default-lifetime="10" cache-provider-service="app.cache_pool" />
1314
</framework:cache>
1415
</framework:config>
1516
</container>

0 commit comments

Comments
 (0)
{"resolvedServerColorMode":"day"}
0