Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Cache] Allow invalidateTags calls to be traced by data collector #36536

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -46,29 +46,38 @@ 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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't we want to collect only the decorator when there is one?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, If we only collect the TagAwareAdapter, we lose the psr6 calls since the Adapter decorated by TagAwareAdapter calls these methods on itself.


$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());
if (($attributes[0]['name'] ?? $id) !== $id) {
$this->addToCollector($attributes[0]['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);
}
}
Expand Up @@ -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;
Expand All @@ -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');
}
}