8000 bug #60392 [DependencyInjection][FrameworkBundle] Fix precedence of `… · symfony/symfony@27edcc2 · GitHub
[go: up one dir, main page]

Skip to content

Commit 27edcc2

Browse files
bug #60392 [DependencyInjection][FrameworkBundle] Fix precedence of App\Kernel alias and ignore container.excluded tag on synthetic services (nicolas-grekas)
This PR was merged into the 7.3 branch. Discussion ---------- [DependencyInjection][FrameworkBundle] Fix precedence of `App\Kernel` alias and ignore `container.excluded` tag on synthetic services | Q | A | ------------- | --- | Branch? | 7.3 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | Fix #60191 | License | MIT Autoconfiguration cannot make a service abstract so I'm removing the related calls, they're confusing. Then, I'm moving the definition of the `App\Kernel` alias after loading `services.yaml`, so that the alias overrides the discovered corresponding class. And I make instance-of-conditionals ignore `container.excluded` tags on synthetic services. An alternative could be to check if the class implements `KernelInterface` like proposed in #60191 (comment) but I think the rule I'm proposing here is more generic. Commits ------- 023c44c [DependencyInjection][FrameworkBundle] Fix precedence of App\Kernel alias and ignore container.excluded tag on synthetic services
2 parents fbff277 + 023c44c commit 27edcc2

File tree

4 fi 8000 les changed

+33
-12
lines changed

4 files changed

+33
-12
lines changed

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -791,34 +791,34 @@ static function (ChildDefinition $definition, AsPeriodicTask|AsCronTask $attribu
791791
}
792792

793793
$container->registerForAutoconfiguration(CompilerPassInterface::class)
794-
->addTag('container.excluded', ['source' => 'because it\'s a compiler pass'])->setAbstract(true);
794+
->addTag('container.excluded', ['source' => 'because it\'s a compiler pass']);
795795
$container->registerForAutoconfiguration(Constraint::class)
796-
->addTag('container.excluded', ['source' => 'because it\'s a validation constraint'])->setAbstract(true);
796+
->addTag('container.excluded', ['source' => 'because it\'s a validation constraint']);
797797
$container->registerForAutoconfiguration(TestCase::class)
798-
->addTag('container.excluded', ['source' => 'because it\'s a test case'])->setAbstract(true);
798+
->addTag('container.excluded', ['source' => 'because it\'s a test case']);
799799
$container->registerForAutoconfiguration(\UnitEnum::class)
800-
->addTag('container.excluded', ['source' => 'because it\'s an enum'])->setAbstract(true);
800+
->addTag('container.excluded', ['source' => 'because it\'s an enum']);
801801
$container->registerAttributeForAutoconfiguration(AsMessage::class, static function (ChildDefinition $definition) {
802-
$definition->addTag('container.excluded', ['source' => 'because it\'s a messenger message'])->setAbstract(true);
802+
$definition->addTag('container.excluded', ['source' => 'because it\'s a messenger message']);
803803
});
804804
$container->registerAttributeForAutoconfiguration(\Attribute::class, static function (ChildDefinition $definition) {
805-
$definition->addTag('container.excluded', ['source' => 'because it\'s an attribute'])->setAbstract(true);
805+
$definition->addTag('container.excluded', ['source' => 'because it\'s a PHP attribute']);
806806
});
807807
$container->registerAttributeForAutoconfiguration(Entity::class, static function (ChildDefinition $definition) {
808-
$definition->addTag('container.excluded', ['source' => 'because it\'s a doctrine entity'])->setAbstract(true);
808+
$definition->addTag('container.excluded', ['source' => 'because it\'s a Doctrine entity']);
809809
});
810810
$container->registerAttributeForAutoconfiguration(Embeddable::class, static function (ChildDefinition $definition) {
811-
$definition->addTag('container.excluded', ['source' => 'because it\'s a doctrine embeddable'])->setAbstract(true);
811+
$definition->addTag('container.excluded', ['source' => 'because it\'s a Doctrine embeddable']);
812812
});
813813
$container->registerAttributeForAutoconfiguration(MappedSuperclass::class, static function (ChildDefinition $definition) {
814-
$definition->addTag('container.excluded', ['source' => 'because it\'s a doctrine mapped superclass'])->setAbstract(true);
814+
$definition->addTag('container.excluded', ['source' => 'because it\'s a Doctrine mapped superclass']);
815815
});
816816

817817
$container->registerAttributeForAutoconfiguration(JsonStreamable::class, static function (ChildDefinition $definition, JsonStreamable $attribute) {
818818
$definition->addTag('json_streamer.streamable', [
819819
'object' => $attribute->asObject,
820820
'list' => $attribute->asList,
821-
])->addTag('container.excluded', ['source' => 'because it\'s a streamable JSON'])->setAbstract(true);
821+
])->addTag('container.excluded', ['source' => 'because it\'s a streamable JSON']);
822822
});
823823

824824
if (!$container->getParameter('kernel.debug')) {

src/Symfony/Bundle/FrameworkBundle/Kernel/MicroKernelTrait.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,6 @@ public function registerContainerConfiguration(LoaderInterface $loader): void
165165
->setPublic(true)
166166
;
167167
}
168-
$container->setAlias($kernelClass, 'kernel')->setPublic(true);
169168

170169
$kernelDefinition = $container->getDefinition('kernel');
171170
$kernelDefinition->addTag('routing.route_loader');
@@ -198,6 +197,8 @@ public function registerContainerConfiguration(LoaderInterface $loader): void
198197
$kernelLoader->registerAliasesForSinglyImplementedInterfaces();
199198
AbstractConfigurator::$valuePreProcessor = $valuePreProcessor;
200199
}
200+
201+
$container->setAlias($kernelClass, 'kernel')->setPublic(true);
201202
});
202203
}
203204

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,8 @@ private function processDefinition(ContainerBuilder $container, string $id, Defi
112112
$definition = substr_replace($definition, '53', 2, 2);
113113
$definition = substr_replace($definition, 'Child', 44, 0);
114114
}
115-
/** @var ChildDefinition $definition */
116115
$definition = unserialize($definition);
116+
/** @var ChildDefinition $definition */
117117
$definition->setParent($parent);
118118

119119
if (null !== $shared && !isset($definition->getChanges()['shared'])) {
@@ -149,6 +149,11 @@ private function processDefinition(ContainerBuilder $container, string $id, Defi
149149
->setAbstract(true);
150150
}
151151

152+
if ($definition->isSynthetic()) {
153+
// Ignore container.excluded tag on synthetic services
154+
$definition->clearTag('container.excluded');
155+
}
156+
152157
return $definition;
153158
}
154159

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,21 @@ public function testDecoratorsKeepBehaviorDescribingTags()
376376
], $container->getDefinition('decorator')->getTags());
377377
$this->assertFalse($container->hasParameter('container.behavior_describing_tags'));
378378
}
379+
380+
public function testSyntheticService()
381+
{
382+
$container = new ContainerBuilder();
383+
$container->register('kernel', \stdClass::class)
384+
->setInstanceofConditionals([
385+
\stdClass::class => (new ChildDefinition(''))
386+
->addTag('container.excluded'),
387+
])
388+
->setSynthetic(true);
389+
390+
(new ResolveInstanceofConditionalsPass())->process($container);
391+
392+
$this->assertSame([], $container->getDefinition('kernel')->getTags());
393+
}
379394
}
380395

381396
class DecoratorWithBehavior implements ResetInterface, ResourceCheckerInterface, ServiceSubscriberInterface

0 commit comments

Comments
 (0)
0