Skip to content

Commit ab6c38d

Browse files
committed
[DependencyInjection] Don’t autowire excluded services
1 parent 6feaaef commit ab6c38d

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

Compiler/AutowirePass.php

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -459,26 +459,26 @@ private function getAutowiredReference(TypedReference $reference, bool $filterTy
459459
$name = $target = (array_filter($reference->getAttributes(), static fn ($a) => $a instanceof Target)[0] ?? null)?->name;
460460

461461
if (null !== $name ??= $reference->getName()) {
462-
if ($this->container->has($alias = $type.' $'.$name) && !$this->container->findDefinition($alias)->isAbstract()) {
462+
if ($this->container->has($alias = $type.' $'.$name) && $this->canDefinitionBeAutowired($alias)) {
463463
return new TypedReference($alias, $type, $reference->getInvalidBehavior());
464464
}
465465

466-
if (null !== ($alias = $this->getCombinedAlias($type, $name)) && !$this->container->findDefinition($alias)->isAbstract()) {
466+
if (null !== ($alias = $this->getCombinedAlias($type, $name)) && $this->canDefinitionBeAutowired($alias)) {
467467
return new TypedReference($alias, $type, $reference->getInvalidBehavior());
468468
}
469469

470470
$parsedName = (new Target($name))->getParsedName();
471471

472-
if ($this->container->has($alias = $type.' $'.$parsedName) && !$this->container->findDefinition($alias)->isAbstract()) {
472+
if ($this->container->has($alias = $type.' $'.$parsedName) && $this->canDefinitionBeAutowired($alias)) {
473473
return new TypedReference($alias, $type, $reference->getInvalidBehavior());
474474
}
475475

476-
if (null !== ($alias = $this->getCombinedAlias($type, $parsedName)) && !$this->container->findDefinition($alias)->isAbstract()) {
476+
if (null !== ($alias = $this->getCombinedAlias($type, $parsedName)) && $this->canDefinitionBeAutowired($alias)) {
477477
return new TypedReference($alias, $type, $reference->getInvalidBehavior());
478478
}
479479

480-
if (($this->container->has($n = $name) && !$this->container->findDefinition($n)->isAbstract())
481-
|| ($this->container->has($n = $parsedName) && !$this->container->findDefinition($n)->isAbstract())
480+
if (($this->container->has($n = $name) && $this->canDefinitionBeAutowired($n))
481+
|| ($this->container->has($n = $parsedName) && $this->canDefinitionBeAutowired($n))
482482
) {
483483
foreach ($this->container->getAliases() as $id => $alias) {
484484
if ($n === (string) $alias && str_starts_with($id, $type.' $')) {
@@ -492,17 +492,24 @@ private function getAutowiredReference(TypedReference $reference, bool $filterTy
492492
}
493493
}
494494

495-
if ($this->container->has($type) && !$this->container->findDefinition($type)->isAbstract()) {
495+
if ($this->container->has($type) && $this->canDefinitionBeAutowired($type)) {
496496
return new TypedReference($type, $type, $reference->getInvalidBehavior());
497497
}
498498

499-
if (null !== ($alias = $this->getCombinedAlias($type)) && !$this->container->findDefinition($alias)->isAbstract()) {
499+
if (null !== ($alias = $this->getCombinedAlias($type)) && $this->canDefinitionBeAutowired($alias)) {
500500
return new TypedReference($alias, $type, $reference->getInvalidBehavior());
501501
}
502502

503503
return null;
504504
}
505505

506+
private function canDefinitionBeAutowired(string $id): bool
507+
{
508+
$definition = $this->container->findDefinition($id);
509+
510+
return !$definition->isAbstract() && !$definition->hasTag('container.excluded');
511+
}
512+
506513
/**
507514
* Populates the list of available types.
508515
*/

Tests/Compiler/AutowirePassTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1322,7 +1322,7 @@ public function testTypeSymbolExcluded()
13221322
{
13231323
$container = new ContainerBuilder();
13241324

1325-
$container->register(Foo::class)->setAbstract(true)->addTag('container.excluded', ['source' => 'for tests']);
1325+
$container->register(Foo::class)->addTag('container.excluded', ['source' => 'for tests']);
13261326
$aDefinition = $container->register('a', NotGuessableArgument::class);
13271327
$aDefinition->setAutowired(true);
13281328

@@ -1339,7 +1339,7 @@ public function testTypeNamespaceExcluded()
13391339
{
13401340
$container = new ContainerBuilder();
13411341

1342-
$container->register(__NAMESPACE__)->setAbstract(true)->addTag('container.excluded');
1342+
$container->register(__NAMESPACE__)->addTag('container.excluded');
13431343
$aDefinition = $container->register('a', NotGuessableArgument::class);
13441344
$aDefinition->setAutowired(true);
13451345

0 commit comments

Comments
 (0)