From 0ed7fb26a7cd453eed76392de6e8f93174d1d9f7 Mon Sep 17 00:00:00 2001 From: Laurent VOULLEMIER Date: Wed, 22 Apr 2020 21:54:02 +0200 Subject: [PATCH] Allow invalidateTags calls to be traced by data collector --- .../CacheCollectorPass.php | 48 ++++++++++++------- .../CacheCollectorPassTest.php | 14 ++++++ 2 files changed, 44 insertions(+), 18 deletions(-) diff --git a/src/Symfony/Component/Cache/DependencyInjection/CacheCollectorPass.php b/src/Symfony/Component/Cache/DependencyInjection/CacheCollectorPass.php index 68671dc23830a..fcc51a00f3915 100644 --- a/src/Symfony/Component/Cache/DependencyInjection/CacheCollectorPass.php +++ b/src/Symfony/Component/Cache/DependencyInjection/CacheCollectorPass.php @@ -46,29 +46,41 @@ public function process(ContainerBuilder $container) return; } - $collectorDefinition = $container->getDefinition($this->dataCollectorCacheId); foreach ($container->findTaggedServiceIds($this->cachePoolTag) as $id => $attributes) { - $definition = $container->getDefinition($id); - if ($definition->isAbstract()) { - continue; - } + $this->addToCollector($id, $container); - $recorder = new Definition(is_subclass_of($definition->getClass(), TagAwareAdapterInterface::class) ? TraceableTagAwareAdapter::class : TraceableAdapter::class); - $recorder->setTags($definition->getTags()); - if (!$definition->isPublic() || !$definition->isPrivate()) { - $recorder->setPublic($definition->isPublic()); + foreach ($attributes as $attribute) { + // A TagAwareAdapter decorates the pool + if (isset($attribute['name']) && $attribute['name'] !== $id) { + $this->addToCollector($attribute['name'], $container); + } } - $recorder->setArguments([new Reference($innerId = $id.$this->cachePoolRecorderInnerSuffix)]); - - $definition->setTags([]); - $definition->setPublic(false); + } + } - $container->setDefinition($innerId, $definition); - $container->setDefinition($id, $recorder); + private function addToCollector(string $id, ContainerBuilder $container) + { + $definition = $container->getDefinition($id); + if ($definition->isAbstract()) { + return; + } - // Tell the collector to add the new instance - $collectorDefinition->addMethodCall('addInstance', [$id, new Reference($id)]); - $collectorDefinition->setPublic(false); + $collectorDefinition = $container->getDefinition($this->dataCollectorCacheId); + $recorder = new Definition(is_subclass_of($definition->getClass(), TagAwareAdapterInterface::class) ? TraceableTagAwareAdapter::class : TraceableAdapter::class); + $recorder->setTags($definition->getTags()); + if (!$definition->isPublic() || !$definition->isPrivate()) { + $recorder->setPublic($definition->isPublic()); } + $recorder->setArguments([new Reference($innerId = $id.$this->cachePoolRecorderInnerSuffix)]); + + $definition->setTags([]); + $definition->setPublic(false); + + $container->setDefinition($innerId, $definition); + $container->setDefinition($id, $recorder); + + // Tell the collector to add the new instance + $collectorDefinition->addMethodCall('addInstance', [$id, new Reference($id)]); + $collectorDefinition->setPublic(false); } } diff --git a/src/Symfony/Component/Cache/Tests/DependencyInjection/CacheCollectorPassTest.php b/src/Symfony/Component/Cache/Tests/DependencyInjection/CacheCollectorPassTest.php index 7e77491de867b..8aff19fc3e14b 100644 --- a/src/Symfony/Component/Cache/Tests/DependencyInjection/CacheCollectorPassTest.php +++ b/src/Symfony/Component/Cache/Tests/DependencyInjection/CacheCollectorPassTest.php @@ -13,6 +13,7 @@ use PHPUnit\Framework\TestCase; use Symfony\Component\Cache\Adapter\FilesystemAdapter; +use Symfony\Component\Cache\Adapter\PhpArrayAdapter; use Symfony\Component\Cache\Adapter\TagAwareAdapter; use Symfony\Component\Cache\Adapter\TraceableAdapter; use Symfony\Component\Cache\Adapter\TraceableTagAwareAdapter; @@ -34,16 +35,29 @@ public function testProcess() ->addArgument(new Reference('fs')) ->addTag('cache.pool'); + $container + ->register('.php.inner', PhpArrayAdapter::class) + ->addTag('cache.pool', ['name' => 'php']); + $container + ->register('php', TagAwareAdapter::class) + ->addArgument(new Reference('.php.inner')); + $collector = $container->register('data_collector.cache', CacheDataCollector::class); (new CacheCollectorPass())->process($container); $this->assertEquals([ ['addInstance', ['fs', new Reference('fs')]], ['addInstance', ['tagged_fs', new Reference('tagged_fs')]], + ['addInstance', ['.php.inner', new Reference('.php.inner')]], + ['addInstance', ['php', new Reference('php')]], ], $collector->getMethodCalls()); $this->assertSame(TraceableAdapter::class, $container->findDefinition('fs')->getClass()); $this->assertSame(TraceableTagAwareAdapter::class, $container->getDefinition('tagged_fs')->getClass()); + + $this->assertSame(TraceableAdapter::class, $container->findDefinition('.php.inner')->getClass()); + $this->assertSame(TraceableTagAwareAdapter::class, $container->getDefinition('php')->getClass()); + $this->assertFalse($collector->isPublic(), 'The "data_collector.cache" should be private after processing'); } }