Fix CacheCollectorPass with decorated cache pools

This commit is contained in:
Soner Sayakci 2020-09-04 11:35:31 +02:00
parent 4482fcf28d
commit 973442759c
No known key found for this signature in database
GPG Key ID: 807EA42309EC39F3
2 changed files with 44 additions and 9 deletions

View File

@ -47,15 +47,13 @@ class CacheCollectorPass implements CompilerPassInterface
}
foreach ($container->findTaggedServiceIds($this->cachePoolTag) as $id => $attributes) {
$this->addToCollector($id, $container);
$poolName = $attributes[0]['name'] ?? $id;
if (($attributes[0]['name'] ?? $id) !== $id) {
$this->addToCollector($attributes[0]['name'], $container);
}
$this->addToCollector($id, $poolName, $container);
}
}
private function addToCollector(string $id, ContainerBuilder $container)
private function addToCollector(string $id, string $name, ContainerBuilder $container)
{
$definition = $container->getDefinition($id);
if ($definition->isAbstract()) {
@ -77,7 +75,7 @@ class CacheCollectorPass implements CompilerPassInterface
$container->setDefinition($id, $recorder);
// Tell the collector to add the new instance
$collectorDefinition->addMethodCall('addInstance', [$id, new Reference($id)]);
$collectorDefinition->addMethodCall('addInstance', [$name, new Reference($id)]);
$collectorDefinition->setPublic(false);
}
}

View File

@ -19,6 +19,8 @@ use Symfony\Component\Cache\Adapter\TraceableAdapter;
use Symfony\Component\Cache\Adapter\TraceableTagAwareAdapter;
use Symfony\Component\Cache\DataCollector\CacheDataCollector;
use Symfony\Component\Cache\DependencyInjection\CacheCollectorPass;
use Symfony\Component\Cache\Tests\Fixtures\ArrayCache;
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
@ -48,16 +50,51 @@ class CacheCollectorPassTest extends TestCase
$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')]],
['addInstance', ['php', new Reference('.php.inner')]],
], $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->assertSame(TagAwareAdapter::class, $container->getDefinition('php')->getClass());
$this->assertFalse($collector->isPublic(), 'The "data_collector.cache" should be private after processing');
}
public function testProcessCacheObjectsAreDecorated()
{
$container = new ContainerBuilder();
$collector = $container->register('data_collector.cache', CacheDataCollector::class);
$container
->register('cache.object', ArrayCache::class)
->addTag('cache.pool', ['name' => 'cache.object']);
$container
->register('something_is_decorating_cache_object', TagAwareAdapter::class)
->setPublic(true)
->setDecoratedService('cache.object');
$container->register('some_service_using_cache_object', TraceableAdapter::class)
->setPublic(true)
->addArgument(new Reference('cache.object'));
$container->addCompilerPass(new CacheCollectorPass(), PassConfig::TYPE_BEFORE_REMOVING);
$container->compile();
$this->assertCount(1, $collector->getMethodCalls());
$this->assertEquals(
[
[
'addInstance',
[
'cache.object',
new Reference('something_is_decorating_cache_object'),
],
],
],
$collector->getMethodCalls()
);
}
}