8000 bug #36536 [Cache] Allow invalidateTags calls to be traced by data co… · symfony/symfony@1bc3ee7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1bc3ee7

Browse files
bug #36536 [Cache] Allow invalidateTags calls to be traced by data collector (l-vo)
This PR was merged into the 4.4 branch. Discussion ---------- [Cache] Allow invalidateTags calls to be traced by data collector | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | #34810 | License | MIT | Doc PR | `TraceableTagAwareAdapter` is not used in the fullstack framework since tag aware pools don't have the `cache.pool` tag (it's the decorated adapter that has it). This PR aims to use `TraceableTagAwareAdapter` when a pool is configured with `tags: true` Commits ------- 28fdb3a Allow invalidateTags calls to be traced by data collector
2 parents 2d7b0b8 + 28fdb3a commit 1bc3ee7

File tree

2 files changed

+41
-18
lines changed

2 files changed

+41
-18
lines changed

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

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -46,29 +46,38 @@ public function process(ContainerBuilder $container)
4646
return;
4747
}
4848

49-
$collectorDefinition = $container->getDefinition($this->dataCollectorCacheId);
5049
foreach ($container->findTaggedServiceIds($this->cachePoolTag) as $id => $attributes) {
51-
$definition = $container->getDefinition($id);
52-
if ($definition->isAbstract()) {
53-
continue;
54-
}
50+
$this->addToCollector($id, $container);
5551

56-
$recorder = new Definition(is_subclass_of($definition->getClass(), TagAwareAdapterInterface::class) ? TraceableTagAwareAdapter::class : TraceableAdapter::class);
57-
$recorder->setTags($definition->getTags());
58-
if (!$definition->isPublic() || !$definition->isPrivate()) {
59-
$recorder->setPublic($definition->isPublic());
52+
if (($attributes[0]['name'] ?? $id) !== $id) {
53+
$this->addToCollector($attributes[0]['name'], $container);
6054
}
61-
$recorder->setArguments([new Reference($innerId = $id.$this->cachePoolRecorderInnerSuffix)]);
62-
63-
$definition->setTags([]);
64-
$definition->setPublic(false);
55+
}
56+
}
6557

66-
$container->setDefinition($innerId, $definition);
67-
$container->setDefinition($id, $recorder);
58+
private function addToCollector(string $id, ContainerBuilder $container)
59+
{
60+
$definition = $container->getDefinition($id);
61+
if ($definition->isAbstract()) {
62+
return;
63+
}
6864

69-
// Tell the collector to add the new instance
70-
$collectorDefinition->addMethodCall('addInstance', [$id, new Reference($id)]);
71-
$collectorDefinition->setPublic(false);
65+
$collectorDefinition = $container->getDefinition($this->dataCollectorCacheId);
66+
$recorder = new Definition(is_subclass_of($definition->getClass(), TagAwareAdapterInterface::class) ? TraceableTagAwareAdapter::class : TraceableAdapter::class);
67+
$recorder->setTags($definition->getTags());
68+
if (!$definition->isPublic() || !$definition->isPrivate()) {
69+
$recorder->setPublic($definition->isPublic());
7270
}
71+
$recorder->setArguments([new Reference($innerId = $id.$this->cachePoolRecorderInnerSuffix)]);
72+
73+
$definition->setTags([]);
74+
$definition->setPublic(false);
75+
76+
$container->setDefinition($innerId, $definition);
77+
$container->setDefinition($id, $recorder);
78+
79+
// Tell the collector to add the new instance
80+
$collectorDefinition->addMethodCall('addInstance', [$id, new Reference($id)]);
81+
$collectorDefinition->setPublic(false);
7382
}
7483
}

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
16+
use Symfony\Component\Cache\Adapter\PhpArrayAdapter;
1617
use Symfony\Component\Cache\Adapter\TagAwareAdapter;
1718
use Symfony\Component\Cache\Adapter\TraceableAdapter;
1819
use Symfony\Component\Cache\Adapter\TraceableTagAwareAdapter;
@@ -34,16 +35,29 @@ public function testProcess()
3435
->addArgument(new Reference('fs'))
3536
->addTag('cache.pool');
3637

38+
$container
39+
->register('.php.inner', PhpArrayAdapter::class)
40+
->addTag('cache.pool', ['name' => 'php']);
41+
$container
42+
->register('php', TagAwareAdapter::class)
43+
->addArgument(new Reference('.php.inner'));
44+
3745
$collector = $container->register('data_collector.cache', CacheDataCollector::class);
3846
(new CacheCollectorPass())->process($container);
3947

4048
$this->assertEquals([
4149
['addInstance', ['fs', new Reference('fs')]],
4250
['addInstance', ['tagged_fs', new Reference('tagged_fs')]],
51+
['addInstance', ['.php.inner', new Reference('.php.inner')]],
52+
['addInstance', ['php', new Reference('php')]],
4353
], $collector->getMethodCalls());
4454

4555
$this->assertSame(TraceableAdapter::class, $container->findDefinition('fs')->getClass());
4656
$this->assertSame(TraceableTagAwareAdapter::class, $container->getDefinition('tagged_fs')->getClass());
57+
58+
$this->assertSame(TraceableAdapter::class, $container->findDefinition('.php.inner')->getClass());
59+
$this->assertSame(TraceableTagAwareAdapter::class, $container->getDefinition('php')->getClass());
60+
4761
$this->assertFalse($collector->isPublic(), 'The "data_collector.cache" should be private after processing');
4862
}
4963
}

0 commit comments

Comments
 (0)
0