8000 bug #46427 [FrameworkBundle] fix wiring of annotations.cached_reader … · symfony/symfony@11a87ad · GitHub
[go: up one dir, main page]

Skip to content

Commit 11a87ad

Browse files
bug #46427 [FrameworkBundle] fix wiring of annotations.cached_reader (nicolas-grekas)
This PR was merged into the 4.4 branch. Discussion ---------- [FrameworkBundle] fix wiring of annotations.cached_reader | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix #46202 | License | MIT | Doc PR | - Also fixes #26088 (comment) Instead of trying to hack the compilation of the container to prevent it from inlining `annotations.cached_reader`, this PR relies on an `container.do_not_inline` tag that just does that, prevent inlining so that removing passes can reliably fetch the corresponding services from the container by id or by tag. Commits ------- e32b7b1 [FrameworkBundle] fix wiring of annotations.cached_reader
2 parents c1ef4de + e32b7b1 commit 11a87ad

File tree

8 files changed

+42
-6
lines changed

8 files changed

+42
-6
lines changed

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ public function process(ContainerBuilder $container)
2929
// "annotation_reader" at build time don't get any cache
3030
foreach ($container->findTaggedServiceIds('annotations.cached_reader') as $id => $tags) {
3131
$reader = $container->getDefinition($id);
32-
$reader->setPublic(false);
3332
$properties = $reader->getProperties();
3433

3534
if (isset($properties['cacheProviderBackup'])) {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class UnusedTagsPass implements CompilerPassInterface
2828
'cache.pool.clearer',
2929
'config_cache.resource_checker',
3030
'console.command',
31+
'container.do_not_inline',
3132
'container.env_var_loader',
3233
'container.env_var_processor',
3334
'container.hot_path',

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,8 @@ public function load(array $configs, ContainerBuilder $container)
469469
->addTag('routing.route_loader');
470470

471471
$container->setParameter('container.behavior_describing_tags', [
472+
'annotations.cached_reader',
473+
'container.do_not_inline',
472474
'container.service_locator',
473475
'container.service_subscriber',
474476
'kernel.event_subscriber',
@@ -1463,11 +1465,9 @@ private function registerAnnotationsConfiguration(array $config, ContainerBuilde
14631465

14641466
$container
14651467
->getDefinition('annotations.cached_reader')
1466-
->setPublic(true) // set to false in AddAnnotationsCachedReaderPass
14671468
->replaceArgument(2, $config['debug'])
14681469
// reference the cache provider without using it until AddAnnotationsCachedReaderPass runs
14691470
->addArgument(new ServiceClosureArgument(new Reference($cacheService)))
1470-
->addTag('annotations.cached_reader')
14711471
;
14721472

14731473
$container->setAlias('annotation_reader', 'annotations.cached_reader')->setPrivate(true);

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
</service>
3232
</argument>
3333
<argument /><!-- Debug-Flag -->
34+
<tag name="annotations.cached_reader" />
35+
<tag name="container.do_not_inline" />
3436
</service>
3537

3638
<service id="annotations.filesystem_cache_adapter" class=" 6D47 Symfony\Component\Cache\Adapter\FilesystemAdapter">

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1699,6 +1699,8 @@ public function testRegisterParameterCollectingBehaviorDescribingTags()
16991699

17001700
$this->assertTrue($container->hasParameter('container.behavior_describing_tags'));
17011701
$this->assertEquals([
1702+
'annotations.cached_reader',
1703+
'container.do_not_inline',
17021704
'container.service_locator',
17031705
'container.service_subscriber',
17041706
'kernel.event_subscriber',

src/Symfony/Component/DependencyInjection/Compiler/DecoratorServicePass.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ public function process(ContainerBuilder $container)
4040
}
4141
$decoratingDefinitions = [];
4242

43+
$tagsToKeep = $container->hasParameter('container.behavior_describing_tags')
44+
? $container->getParameter('container.behavior_describing_tags')
45+
: ['container.do_not_inline', 'container.service_locator', 'container.service_subscriber'];
46+
4347
foreach ($definitions as [$id, $definition]) {
4448
$decoratedService = $definition->getDecoratedService();
4549
[$inner, $renamedId] = $decoratedService;
@@ -89,8 +93,8 @@ public function process(ContainerBuilder $container)
8993
$decoratingTags = $decoratingDefinition->getTags();
9094
$resetTags = [];
9195

92-
// container.service_locator and container.service_subscriber have special logic and they must not be transferred out to decorators
93-
foreach (['container.service_locator', 'container.service_subscriber'] as $containerTag) {
96+
// Behavior-describing tags must not be transferred out to decorators
97+
foreach ($tagsToKeep as $containerTag) {
9498
if (isset($decoratingTags[$containerTag])) {
9599
$resetTags[$containerTag] = $decoratingTags[$containerTag];
96100
unset($decoratingTags[$containerTag]);

src/Symfony/Component/DependencyInjection/Compiler/InlineServiceDefinitionsPass.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ protected function processValue($value, $isRoot = false)
177177
*/
178178
private function isInlineableDefinition(string $id, Definition $definition): bool
179179
{
180-
if ($definition->hasErrors() || $definition->isDeprecated() || $definition->isLazy() || $definition->isSynthetic()) {
180+
if ($definition->hasErrors() || $definition->isDeprecated() || $definition->isLazy() || $definition->isSynthetic() || $definition->hasTag('container.do_not_inline')) {
181181
return false;
182182
}
183183

src/Symfony/Component/DependencyInjection/Tests/Compiler/InlineServiceDefinitionsPassTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,34 @@ public function testProcessDoesNotSetLazyArgumentValuesAfterInlining()
325325
$this->assertSame('inline', (string) $values[0]);
326326
}
327327

328+
public function testDoNotInline()
329+
{
330+
$container = new ContainerBuilder();
331+
$container->register('decorated1', 'decorated1')->addTag('container.do_not_inline');
332+
$container->register('decorated2', 'decorated2')->addTag('container.do_not_inline');
333+
$container->setAlias('alias2', 'decorated2');
334+
335+
$container
336+
->register('s1', 's1')
337+
->setDecoratedService('decorated1')
338+
->setPublic(true)
339+
->setProperties(['inner' => new Reference('s1.inner')]);
340+
341+
$container
342+
->register('s2', 's2')
343+
->setDecoratedService('alias2')
344+
->setPublic(true)
345+
->setProperties(['inner' => new Reference('s2.inner')]);
346+
347+
$container->compile();
348+
349+
$this->assertFalse($container->hasAlias('alias2'));
350+
$this->assertEquals(new Reference('decorated2'), $container->getDefinition('s2')->getProperties()['inner']);
351+
$this->assertEquals(new Reference('s1.inner'), $container->getDefinition('s1')->getProperties()['inner']);
352+
$this->assertSame('decorated2', $container->getDefinition('decorated2')->getClass());
353+
$this->assertSame('decorated1', $container->getDefinition('s1.inner')->getClass());
354+
}
355+
328356
protected function process(ContainerBuilder $container)
329357
{
330358
(new InlineServiceDefinitionsPass(new AnalyzeServiceReferencesPass()))->process($container);

0 commit comments

Comments
 (0)
0