diff --git a/src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php b/src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php index e394cf1057f8d..ef5642c3ae416 100644 --- a/src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php +++ b/src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php @@ -459,26 +459,26 @@ private function getAutowiredReference(TypedReference $reference, bool $filterTy $name = $target = (array_filter($reference->getAttributes(), static fn ($a) => $a instanceof Target)[0] ?? null)?->name; if (null !== $name ??= $reference->getName()) { - if ($this->container->has($alias = $type.' $'.$name) && !$this->container->findDefinition($alias)->isAbstract()) { + if ($this->container->has($alias = $type.' $'.$name) && $this->canDefinitionBeAutowired($alias)) { return new TypedReference($alias, $type, $reference->getInvalidBehavior()); } - if (null !== ($alias = $this->getCombinedAlias($type, $name)) && !$this->container->findDefinition($alias)->isAbstract()) { + if (null !== ($alias = $this->getCombinedAlias($type, $name)) && $this->canDefinitionBeAutowired($alias)) { return new TypedReference($alias, $type, $reference->getInvalidBehavior()); } $parsedName = (new Target($name))->getParsedName(); - if ($this->container->has($alias = $type.' $'.$parsedName) && !$this->container->findDefinition($alias)->isAbstract()) { + if ($this->container->has($alias = $type.' $'.$parsedName) && $this->canDefinitionBeAutowired($alias)) { return new TypedReference($alias, $type, $reference->getInvalidBehavior()); } - if (null !== ($alias = $this->getCombinedAlias($type, $parsedName)) && !$this->container->findDefinition($alias)->isAbstract()) { + if (null !== ($alias = $this->getCombinedAlias($type, $parsedName)) && $this->canDefinitionBeAutowired($alias)) { return new TypedReference($alias, $type, $reference->getInvalidBehavior()); } - if (($this->container->has($n = $name) && !$this->container->findDefinition($n)->isAbstract()) - || ($this->container->has($n = $parsedName) && !$this->container->findDefinition($n)->isAbstract()) + if (($this->container->has($n = $name) && $this->canDefinitionBeAutowired($n)) + || ($this->container->has($n = $parsedName) && $this->canDefinitionBeAutowired($n)) ) { foreach ($this->container->getAliases() as $id => $alias) { if ($n === (string) $alias && str_starts_with($id, $type.' $')) { @@ -492,17 +492,24 @@ private function getAutowiredReference(TypedReference $reference, bool $filterTy } } - if ($this->container->has($type) && !$this->container->findDefinition($type)->isAbstract()) { + if ($this->container->has($type) && $this->canDefinitionBeAutowired($type)) { return new TypedReference($type, $type, $reference->getInvalidBehavior()); } - if (null !== ($alias = $this->getCombinedAlias($type)) && !$this->container->findDefinition($alias)->isAbstract()) { + if (null !== ($alias = $this->getCombinedAlias($type)) && $this->canDefinitionBeAutowired($alias)) { return new TypedReference($alias, $type, $reference->getInvalidBehavior()); } return null; } + private function canDefinitionBeAutowired(string $id): bool + { + $definition = $this->container->findDefinition($id); + + return !$definition->isAbstract() && !$definition->hasTag('container.excluded'); + } + /** * Populates the list of available types. */ diff --git a/src/Symfony/Component/DependencyInjection/Tests/Compiler/AutowirePassTest.php b/src/Symfony/Component/DependencyInjection/Tests/Compiler/AutowirePassTest.php index 114d514adddec..d6bbbc70ffc09 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Compiler/AutowirePassTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Compiler/AutowirePassTest.php @@ -1322,7 +1322,7 @@ public function testTypeSymbolExcluded() { $container = new ContainerBuilder(); - $container->register(Foo::class)->setAbstract(true)->addTag('container.excluded', ['source' => 'for tests']); + $container->register(Foo::class)->addTag('container.excluded', ['source' => 'for tests']); $aDefinition = $container->register('a', NotGuessableArgument::class); $aDefinition->setAutowired(true); @@ -1339,7 +1339,7 @@ public function testTypeNamespaceExcluded() { $container = new ContainerBuilder(); - $container->register(__NAMESPACE__)->setAbstract(true)->addTag('container.excluded'); + $container->register(__NAMESPACE__)->addTag('container.excluded'); $aDefinition = $container->register('a', NotGuessableArgument::class); $aDefinition->setAutowired(true);