Skip to content

[DependencyInjection] Don’t autowire excluded services #61411

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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.' $')) {
Expand All @@ -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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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);

Expand Down
Loading