Fix CacheCollectorPass with decorated cache pools
This commit is contained in:
parent
4482fcf28d
commit
973442759c
@ -47,15 +47,13 @@ class CacheCollectorPass implements CompilerPassInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
foreach ($container->findTaggedServiceIds($this->cachePoolTag) as $id => $attributes) {
|
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($id, $poolName, $container);
|
||||||
$this->addToCollector($attributes[0]['name'], $container);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private function addToCollector(string $id, ContainerBuilder $container)
|
private function addToCollector(string $id, string $name, ContainerBuilder $container)
|
||||||
{
|
{
|
||||||
$definition = $container->getDefinition($id);
|
$definition = $container->getDefinition($id);
|
||||||
if ($definition->isAbstract()) {
|
if ($definition->isAbstract()) {
|
||||||
@ -77,7 +75,7 @@ class CacheCollectorPass implements CompilerPassInterface
|
|||||||
$container->setDefinition($id, $recorder);
|
$container->setDefinition($id, $recorder);
|
||||||
|
|
||||||
// Tell the collector to add the new instance
|
// 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);
|
$collectorDefinition->setPublic(false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -19,6 +19,8 @@ use Symfony\Component\Cache\Adapter\TraceableAdapter;
|
|||||||
use Symfony\Component\Cache\Adapter\TraceableTagAwareAdapter;
|
use Symfony\Component\Cache\Adapter\TraceableTagAwareAdapter;
|
||||||
use Symfony\Component\Cache\DataCollector\CacheDataCollector;
|
use Symfony\Component\Cache\DataCollector\CacheDataCollector;
|
||||||
use Symfony\Component\Cache\DependencyInjection\CacheCollectorPass;
|
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\ContainerBuilder;
|
||||||
use Symfony\Component\DependencyInjection\Reference;
|
use Symfony\Component\DependencyInjection\Reference;
|
||||||
|
|
||||||
@ -48,16 +50,51 @@ class CacheCollectorPassTest extends TestCase
|
|||||||
$this->assertEquals([
|
$this->assertEquals([
|
||||||
['addInstance', ['fs', new Reference('fs')]],
|
['addInstance', ['fs', new Reference('fs')]],
|
||||||
['addInstance', ['tagged_fs', new Reference('tagged_fs')]],
|
['addInstance', ['tagged_fs', new Reference('tagged_fs')]],
|
||||||
['addInstance', ['.php.inner', new Reference('.php.inner')]],
|
['addInstance', ['php', new Reference('.php.inner')]],
|
||||||
['addInstance', ['php', new Reference('php')]],
|
|
||||||
], $collector->getMethodCalls());
|
], $collector->getMethodCalls());
|
||||||
|
|
||||||
$this->assertSame(TraceableAdapter::class, $container->findDefinition('fs')->getClass());
|
$this->assertSame(TraceableAdapter::class, $container->findDefinition('fs')->getClass());
|
||||||
$this->assertSame(TraceableTagAwareAdapter::class, $container->getDefinition('tagged_fs')->getClass());
|
$this->assertSame(TraceableTagAwareAdapter::class, $container->getDefinition('tagged_fs')->getClass());
|
||||||
|
|
||||||
$this->assertSame(TraceableAdapter::class, $container->findDefinition('.php.inner')->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');
|
$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()
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user