Skip to content

Commit a347ae3

Browse files
committed
Fix CacheCollectorPass priority
1 parent 744a4eb commit a347ae3

File tree

4 files changed

+69
-7
lines changed

4 files changed

+69
-7
lines changed

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/CacheCollectorPass.php

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\Cache\Adapter\TraceableTagAwareAdapter;
1717
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
1818
use Symfony\Component\DependencyInjection\ContainerBuilder;
19+
use Symfony\Component\DependencyInjection\Definition;
1920
use Symfony\Component\DependencyInjection\Reference;
2021

2122
/**
@@ -41,13 +42,25 @@ public function process(ContainerBuilder $container)
4142
continue;
4243
}
4344

44-
$container->register($id.'.recorder', is_subclass_of($definition->getClass(), TagAwareAdapterInterface::class) ? TraceableTagAwareAdapter::class : TraceableAdapter::class)
45-
->setDecoratedService($id)
46-
->addArgument(new Reference($id.'.recorder.inner'))
47-
->setPublic(false);
45+
$recorder = new Definition(is_subclass_of($definition->getClass(), TagAwareAdapterInterface::class) ? TraceableTagAwareAdapter::class : TraceableAdapter::class);
46+
$recorder->setTags($definition->getTags());
47+
$recorder->setPublic($definition->isPublic());
48+
$recorder->setArguments(array(new Reference($innerId = $id.'.recorder_inner')));
49+
50+
$definition->setTags(array());
51+
$definition->setPublic(false);
52+
53+
if ($types = $definition->getAutowiringTypes(false)) {
54+
$recorder->setAutowiringTypes($types);
55+
$definition->setAutowiringTypes(array());
56+
}
57+
58+
$container->setDefinition($innerId, $definition);
59+
$container->setDefinition($id, $recorder);
4860

4961
// Tell the collector to add the new instance
5062
$collectorDefinition->addMethodCall('addInstance', array($id, new Reference($id)));
63+
$collectorDefinition->setPublic(false);
5164
}
5265
}
5366
}

src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public function build(ContainerBuilder $container)
115115
$container->addCompilerPass(new UnusedTagsPass(), PassConfig::TYPE_AFTER_REMOVING);
116116
$container->addCompilerPass(new ContainerBuilderDebugDumpPass(), PassConfig::TYPE_BEFORE_REMOVING, -255);
117117
$this->addCompilerPassIfExists($container, ConfigCachePass::class);
118-
$container->addCompilerPass(new CacheCollectorPass());
118+
$container->addCompilerPass(new CacheCollectorPass(), PassConfig::TYPE_BEFORE_REMOVING);
119119
}
120120
}
121121

src/Symfony/Bundle/FrameworkBundle/Resources/config/cache_debug.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
<services>
88
<defaults public="false" />
99

10-
<!-- DataCollector -->
11-
<service id="data_collector.cache" class="Symfony\Component\Cache\DataCollector\CacheDataCollector">
10+
<!-- DataCollector (public to prevent inlining, made private in CacheCollectorPass) -->
11+
<service id="data_collector.cache" class="Symfony\Component\Cache\DataCollector\CacheDataCollector" public="true">
1212
<tag name="data_collector" template="@WebProfiler/Collector/cache.html.twig" id="cache" priority="275" />
1313
</service>
1414
</services>
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\Compiler;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\CacheCollectorPass;
16+
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
17+
use Symfony\Component\Cache\Adapter\TagAwareAdapter;
18+
use Symfony\Component\Cache\Adapter\TraceableAdapter;
19+
use Symfony\Component\Cache\Adapter\TraceableTagAwareAdapter;
20+
use Symfony\Component\Cache\DataCollector\CacheDataCollector;
21+
use Symfony\Component\DependencyInjection\ContainerBuilder;
22+
use Symfony\Component\DependencyInjection\Reference;
23+
24+
class CacheCollectorPassTest extends TestCase
25+
{
26+
public function testProcess()
27+
{
28+
$container = new ContainerBuilder();
29+
$container
30+
->register('fs', FilesystemAdapter::class)
31+
->addTag('cache.pool');
32+
$container
33+
->register('tagged_fs', TagAwareAdapter::class)
34+
->addArgument(new Reference('fs'))
35+
->addTag('cache.pool');
36+
37+
$collector = $container->register('data_collector.cache', CacheDataCollector::class);
38+
(new CacheCollectorPass())->process($container);
39+
40+
$this->assertEquals(array(
41+
array('addInstance', array('fs', new Reference('fs'))),
42+
array('addInstance', array('tagged_fs', new Reference('tagged_fs'))),
43+
), $collector->getMethodCalls());
44+
45+
$this->assertSame(TraceableAdapter::class, $container->findDefinition('fs')->getClass());
46+
$this->assertSame(TraceableTagAwareAdapter::class, $container->getDefinition('tagged_fs')->getClass());
47+
$this->assertFalse($collector->isPublic(), 'The "data_collector.cache" should be private after processing');
48+
}
49+
}

0 commit comments

Comments
 (0)