Skip to content

Commit e6be2bf

Browse files
[FrameworkBundle] Allow configuring taggable cache pools
1 parent 2ae7ad9 commit e6be2bf

File tree

10 files changed

+125
-11
lines changed

10 files changed

+125
-11
lines changed

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ public function process(ContainerBuilder $container)
3131
foreach ($container->findTaggedServiceIds('cache.pool.clearer') as $id => $attr) {
3232
$clearer = $container->getDefinition($id);
3333
$pools = array();
34-
foreach ($clearer->getArgument(0) as $id => $ref) {
35-
if ($container->hasDefinition($id)) {
36-
$pools[$id] = new Reference($id);
34+
foreach ($clearer->getArgument(0) as $name => $ref) {
35+
if ($container->hasDefinition($ref)) {
36+
$pools[$name] = new Reference($ref);
3737
}
3838
}
3939
$clearer->replaceArgument(0, $pools);

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

+7-5
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public function process(ContainerBuilder $container)
4141
$clearers = array();
4242
$attributes = array(
4343
'provider',
44+
'name',
4445
'namespace',
4546
'default_lifetime',
4647
'reset',
@@ -56,8 +57,9 @@ public function process(ContainerBuilder $container)
5657
$tags[0] += $t[0];
5758
}
5859
}
60+
$name = $tags[0]['name'] ?? $id;
5961
if (!isset($tags[0]['namespace'])) {
60-
$tags[0]['namespace'] = $this->getNamespace($seed, $id);
62+
$tags[0]['namespace'] = $this->getNamespace($seed, $name);
6163
}
6264
if (isset($tags[0]['clearer'])) {
6365
$clearer = $tags[0]['clearer'];
@@ -67,7 +69,7 @@ public function process(ContainerBuilder $container)
6769
} else {
6870
$clearer = null;
6971
}
70-
unset($tags[0]['clearer']);
72+
unset($tags[0]['clearer'], $tags[0]['name']);
7173

7274
if (isset($tags[0]['provider'])) {
7375
$tags[0]['provider'] = new Reference(static::getServiceProvider($container, $tags[0]['provider']));
@@ -86,14 +88,14 @@ public function process(ContainerBuilder $container)
8688
unset($tags[0][$attr]);
8789
}
8890
if (!empty($tags[0])) {
89-
throw new InvalidArgumentException(sprintf('Invalid "cache.pool" tag for service "%s": accepted attributes are "clearer", "provider", "namespace", "default_lifetime" and "reset", found "%s".', $id, implode('", "', array_keys($tags[0]))));
91+
throw new InvalidArgumentException(sprintf('Invalid "cache.pool" tag for service "%s": accepted attributes are "clearer", "provider", "name", "namespace", "default_lifetime" and "reset", found "%s".', $id, implode('", "', array_keys($tags[0]))));
9092
}
9193

9294
if (null !== $clearer) {
93-
$clearers[$clearer][$id] = new Reference($id, $container::IGNORE_ON_UNINITIALIZED_REFERENCE);
95+
$clearers[$clearer][$name] = new Reference($id, $container::IGNORE_ON_UNINITIALIZED_REFERENCE);
9496
}
9597

96-
$pools[$id] = new Reference($id, $container::IGNORE_ON_UNINITIALIZED_REFERENCE);
98+
$pools[$name] = new Reference($id, $container::IGNORE_ON_UNINITIALIZED_REFERENCE);
9799
}
98100

99101
$clearer = 'cache.global_clearer';

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

+1
Original file line numberDiff line numberDiff line change
@@ -861,6 +861,7 @@ private function addCacheSection(ArrayNodeDefinition $rootNode)
861861
->prototype('array')
862862
->children()
863863
->scalarNode('adapter')->defaultValue('cache.app')->end()
864+
->scalarNode('tags')->defaultNull()->end()
864865
->booleanNode('public')->defaultFalse()->end()
865866
->integerNode('default_lifetime')->end()
866867
->scalarNode('provider')

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

+21-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use Symfony\Component\Cache\Adapter\AbstractAdapter;
2323
use Symfony\Component\Cache\Adapter\AdapterInterface;
2424
use Symfony\Component\Cache\Adapter\ArrayAdapter;
25+
use Symfony\Component\Cache\Adapter\TagAwareAdapter;
2526
use Symfony\Component\Cache\ResettableInterface;
2627
use Symfony\Component\Config\FileLocator;
2728
use Symfony\Component\Config\Loader\LoaderInterface;
@@ -1543,12 +1544,31 @@ private function registerCacheConfiguration(array $config, ContainerBuilder $con
15431544
$config['pools']['cache.'.$name] = array(
15441545
'adapter' => $config[$name],
15451546
'public' => true,
1547+
'tags' => false,
15461548
);
15471549
}
15481550
foreach ($config['pools'] as $name => $pool) {
1551+
if ($config['pools'][$pool['adapter']]['tags'] ?? false) {
1552+
$pool['adapter'] = '.'.$pool['adapter'].'.inner';
1553+
}
15491554
$definition = new ChildDefinition($pool['adapter']);
1555+
1556+
if ($pool['tags']) {
1557+
if ($config['pools'][$pool['tags']]['tags'] ?? false) {
1558+
$pool['tags'] = '.'.$pool['tags'].'.inner';
1559+
}
1560+
$container->register($name, TagAwareAdapter::class)
1561+
->addArgument(new Reference('.'.$name.'.inner'))
1562+
->addArgument(true !== $pool['tags'] ? new Reference($pool['tags']) : null)
1563+
->setPublic($pool['public'])
1564+
;
1565+
1566+
$pool['name'] = $name;
1567+
$pool['public'] = false;
1568+
$name = '.'.$name.'.inner';
1569+
}
15501570
$definition->setPublic($pool['public']);
1551-
unset($pool['adapter'], $pool['public']);
1571+
unset($pool['adapter'], $pool['public'], $pool['tags']);
15521572

15531573
$definition->addTag('cache.pool', $pool);
15541574
$container->setDefinition($name, $definition);

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/CachePoolClearerPassTest.php

+11-2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ public function testPoolRefsAreWeak()
3939
$publicPool->addTag('cache.pool', array('clearer' => 'clearer_alias'));
4040
$container->setDefinition('public.pool', $publicPool);
4141

42+
$publicPool = new Definition();
43+
$publicPool->addArgument('namespace');
44+
$publicPool->addTag('cache.pool', array('clearer' => 'clearer_alias', 'name' => 'pool2'));
45+
$container->setDefinition('public.pool2', $publicPool);
46+
4247
$privatePool = new Definition();
4348
$privatePool->setPublic(false);
4449
$privatePool->addArgument('namespace');
@@ -55,7 +60,11 @@ public function testPoolRefsAreWeak()
5560
$pass->process($container);
5661
}
5762

58-
$this->assertEquals(array(array('public.pool' => new Reference('public.pool'))), $clearer->getArguments());
59-
$this->assertEquals(array(array('public.pool' => new Reference('public.pool'))), $globalClearer->getArguments());
63+
$expected = array(array(
64+
'public.pool' => new Reference('public.pool'),
65+
'pool2' => new Reference('public.pool2'),
66+
));
67+
$this->assertEquals($expected, $clearer->getArguments());
68+
$this->assertEquals($expected, $globalClearer->getArguments());
6069
}
6170
}

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

+22
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,28 @@ public function testArgsAreReplaced()
9393
$this->assertSame(3, $cachePool->getArgument(2));
9494
}
9595

96+
public function testWithNameAttribute()
97+
{
98+
$container = new ContainerBuilder();
99+
$container->setParameter('kernel.debug', false);
100+
$container->setParameter('kernel.name', 'app');
101+
$container->setParameter('kernel.environment', 'prod');
102+
$container->setParameter('cache.prefix.seed', 'foo');
103+
$cachePool = new Definition();
104+
$cachePool->addTag('cache.pool', array(
105+
'name' => 'foobar',
106+
'provider' => 'foobar',
107+
));
108+
$cachePool->addArgument(null);
109+
$cachePool->addArgument(null);
110+
$cachePool->addArgument(null);
111+
$container->setDefinition('app.cache_pool', $cachePool);
112+
113+
$this->cachePoolPass->process($container);
114+
115+
$this->assertSame('9HvPgAayyh', $cachePool->getArgument(1));
116+
}
117+
96118
/**
97119
* @expectedException \InvalidArgumentException
98120
* @expectedExceptionMessage Invalid "cache.pool" tag for service "app.cache_pool": accepted attributes are

src/Symfony/Bundle/FrameworkBundle/Tests/Functional/CachePoolsTest.php

+20
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\Cache\Adapter\AdapterInterface;
1515
use Symfony\Component\Cache\Adapter\RedisAdapter;
16+
use Symfony\Component\Cache\Adapter\TagAwareAdapter;
1617
use Symfony\Component\Cache\Exception\InvalidArgumentException;
1718

1819
class CachePoolsTest extends WebTestCase
@@ -94,6 +95,25 @@ private function doTestCachePools($options, $adapterClass)
9495

9596
$item = $pool2->getItem($key);
9697
$this->assertTrue($item->isHit());
98+
99+
$prefix = "\0".TagAwareAdapter::class."\0";
100+
$pool4 = $container->get('cache.pool4');
101+
$this->assertInstanceof(TagAwareAdapter::class, $pool4);
102+
$pool4 = (array) $pool4;
103+
$this->assertSame($pool4[$prefix.'pool'], $pool4[$prefix.'tags']);
104+
105+
$pool5 = $container->get('cache.pool5');
106+
$this->assertInstanceof(TagAwareAdapter::class, $pool5);
107+
$pool5 = (array) $pool5;
108+
$this->assertSame($pool2, $pool5[$prefix.'tags']);
109+
110+
$pool6 = $container->get('cache.pool6');
111+
$this->assertInstanceof(TagAwareAdapter::class, $pool6);
112+
$pool6 = (array) $pool6;
113+
$this->assertSame($pool4[$prefix.'pool'], $pool6[$prefix.'tags']);
114+
115+
$pool7 = $container->get('cache.pool7');
116+
$this->assertNotInstanceof(TagAwareAdapter::class, $pool7);
97117
}
98118

99119
protected static function createKernel(array $options = array())

src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/CachePools/config.yml

+12
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,15 @@ framework:
1212
adapter: cache.pool3
1313
cache.pool3:
1414
clearer: ~
15+
cache.pool4:
16+
tags: true
17+
public: true
18+
cache.pool5:
19+
tags: cache.pool2
20+
public: true
21+
cache.pool6:
22+
tags: cache.pool4
23+
public: true
24+
cache.pool7:
25+
adapter: cache.pool4
26+
public: true

src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/CachePools/redis_config.yml

+14
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,17 @@ framework:
1515
cache.pool2:
1616
public: true
1717
clearer: ~
18+
cache.pool3:
19+
clearer: ~
20+
cache.pool4:
21+
tags: true
22+
public: true
23+
cache.pool5:
24+
tags: cache.pool2
25+
public: true
26+
cache.pool6:
27+
tags: cache.pool4
28+
public: true
29+
cache.pool7:
30+
adapter: cache.pool4
31+
public: true

src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/CachePools/redis_custom_config.yml

+14
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,17 @@ framework:
2626
cache.pool2:
2727
public: true
2828
clearer: ~
29+
cache.pool3:
30+
clearer: ~
31+
cache.pool4:
32+
tags: true
33+
public: true
34+
cache.pool5:
35+
tags: cache.pool2
36+
public: true
37+
cache.pool6:
38+
tags: cache.pool4
39+
public: true
40+
cache.pool7:
41+
adapter: cache.pool4
42+
public: true

0 commit comments

Comments
 (0)