8000 bug #38059 [Cache] Fix CacheCollectorPass with decorated cache pools … · symfony/symfony@da73be8 · GitHub
[go: up one dir, main page]

Skip to content

Commit da73be8

Browse files
committed
bug #38059 [Cache] Fix CacheCollectorPass with decorated cache pools (shyim)
This PR was merged into the 4.4 branch. Discussion ---------- [Cache] Fix CacheCollectorPass with decorated cache pools | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix #38055 | License | MIT | Doc PR | The `getDefinition` call in `addToCollector` does not work when the service is decorated. Also instances will be added with the decoration name. This looks weird in the profiler. I have changed it to prefer the attribute name. Before: ![image](https://user-images.githubusercontent.com/6224096/92225243-3be27c00-eea3-11ea-8bd5-2ae69212e658.png) After: (`\Shopware\Storefront\Framework\Cache\CacheDecorator` did go into `cache.object`) ![image](https://user-images.githubusercontent.com/6224096/92225284-47ce3e00-eea3-11ea-885f-cdba95b89302.png) Commits ------- 9734427 Fix CacheCollectorPass with decorated cache pools
2 parents a19f91a + 9734427 commit da73be8

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)
0