Skip to content

Commit 9734427

Browse files
committed
Fix CacheCollectorPass with decorated cache pools
1 parent 4482fcf commit 9734427

File tree

2 files changed

+44
-9
lines changed

2 files changed

+44
-9
lines changed

src/Symfony/Component/Cache/DependencyInjection/CacheCollectorPass.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,13 @@ public function process(ContainerBuilder $container)
4747
}
4848

4949
foreach ($container->findTaggedServiceIds($this->cachePoolTag) as $id => $attributes) {
50-
$this->addToCollector($id, $container);
50+
$poolName = $attributes[0]['name'] ?? $id;
5151

52-
if (($attributes[0]['name'] ?? $id) !== $id) {
53-
$this->addToCollector($attributes[0]['name'], $container);
54-
}
52+
$this->addToCollector($id, $poolName, $container);
5553
}
5654
}
5755

58-
private function addToCollector(string $id, ContainerBuilder $container)
56+
private function addToCollector(string $id, string $name, ContainerBuilder $container)
5957
{
6058
$definition = $container->getDefinition($id);
6159
if ($definition->isAbstract()) {
@@ -77,7 +75,7 @@ private function addToCollector(string $id, ContainerBuilder $container)
7775
$container->setDefinition($id, $recorder);
7876

7977
// Tell the collector to add the new instance
80-
$collectorDefinition->addMethodCall('addInstance', [$id, new Reference($id)]);
78+
$collectorDefinition->addMethodCall('addInstance', [$name, new Reference($id)]);
8179
$collectorDefinition->setPublic(false);
8280
}
8381
}

src/Symfony/Component/Cache/Tests/DependencyInjection/CacheCollectorPassTest.php

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
use Symfony\Component\Cache\Adapter\TraceableTagAwareAdapter;
2020
use Symfony\Component\Cache\DataCollector\CacheDataCollector;
2121
use Symfony\Component\Cache\DependencyInjection\CacheCollectorPass;
22+
use Symfony\Component\Cache\Tests\Fixtures\ArrayCache;
23+
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
2224
use Symfony\Component\DependencyInjection\ContainerBuilder;
2325
use Symfony\Component\DependencyInjection\Reference;
2426

@@ -48,16 +50,51 @@ public function testProcess()
4850
$this->assertEquals([
4951
['addInstance', ['fs', new Reference('fs')]],
5052
['addInstance', ['tagged_fs', new Reference('tagged_fs')]],
51-
['addInstance', ['.php.inner', new Reference('.php.inner')]],
52-
['addInstance', ['php', new Reference('php')]],
53+
['addInstance', ['php', new Reference('.php.inner')]],
5354
], $collector->getMethodCalls());
5455

5556
$this->assertSame(TraceableAdapter::class, $container->findDefinition('fs')->getClass());
5657
$this->assertSame(TraceableTagAwareAdapter::class, $container->getDefinition('tagged_fs')->getClass());
5758

5859
$this->assertSame(TraceableAdapter::class, $container->findDefinition('.php.inner')->getClass());
59-
$this->assertSame(TraceableTagAwareAdapter::class, $container->getDefinition('php')->getClass());
60+
$this->assertSame(TagAwareAdapter::class, $container->getDefinition('php')->getClass());
6061

6162
$this->assertFalse($collector->isPublic(), 'The "data_collector.cache" should be private after processing');
6263
}
64+
65+
public function testProcessCacheObjectsAreDecorated()
66+
{
67+
$container = new ContainerBuilder();
68+
$collector = $container->register('data_collector.cache', CacheDataCollector::class);
69+
70+
$container
71+
->register('cache.object', ArrayCache::class)
72+
->addTag('cache.pool', ['name' => 'cache.object']);
73+
74+
$container
75+
->register('something_is_decorating_cache_object', TagAwareAdapter::class)
76+
->setPublic(true)
77+
->setDecoratedService('cache.object');
78+
79+
$container->register('some_service_using_cache_object', TraceableAdapter::class)
80+
->setPublic(true)
81+
->addArgument(new Reference('cache.object'));
82+
83+
$container->addCompilerPass(new CacheCollectorPass(), PassConfig::TYPE_BEFORE_REMOVING);
84+
85+
$container->compile();
86+
$this->assertCount(1, $collector->getMethodCalls());
87+
$this->assertEquals(
88+
[
89+
[
90+
'addInstance',
91+
[
92+
'cache.object',
93+
new Reference('something_is_decorating_cache_object'),
94+
],
95+
],
96+
],
97+
$collector->getMethodCalls()
98+
);
99+
}
63100
}

0 commit comments

Comments
 (0)