diff --git a/Compiler/AutowirePass.php b/Compiler/AutowirePass.php index e394cf105..ef5642c3a 100644 --- a/Compiler/AutowirePass.php +++ b/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/Dumper/PhpDumper.php b/Dumper/PhpDumper.php index fb2d45f92..90050ef97 100644 --- a/Dumper/PhpDumper.php +++ b/Dumper/PhpDumper.php @@ -799,7 +799,7 @@ private function addServiceConfigurator(Definition $definition, string $variable if (\is_array($callable)) { if ($callable[0] instanceof Reference - || ($callable[0] instanceof Definition && $this->definitionVariables->contains($callable[0])) + || ($callable[0] instanceof Definition && $this->definitionVariables->offsetExists($callable[0])) ) { return \sprintf(" %s->%s(\$%s);\n", $this->dumpValue($callable[0]), $callable[1], $variableName); } @@ -1187,7 +1187,7 @@ private function addNewInstance(Definition $definition, string $return = '', ?st if (['...'] === $arguments && ('Closure' !== ($class = $definition->getClass() ?: 'Closure') || $definition->isLazy() && ( $callable[0] instanceof Reference - || ($callable[0] instanceof Definition && !$this->definitionVariables->contains($callable[0])) + || ($callable[0] instanceof Definition && !$this->definitionVariables->offsetExists($callable[0])) ))) { $initializer = 'fn () => '.$this->dumpValue($callable[0]); @@ -1195,7 +1195,7 @@ private function addNewInstance(Definition $definition, string $return = '', ?st } if ($callable[0] instanceof Reference - || ($callable[0] instanceof Definition && $this->definitionVariables->contains($callable[0])) + || ($callable[0] instanceof Definition && $this->definitionVariables->offsetExists($callable[0])) ) { return $return.\sprintf('%s->%s(%s)', $this->dumpValue($callable[0]), $callable[1], $arguments ? implode(', ', $arguments) : '').$tail; } @@ -1937,7 +1937,7 @@ private function dumpValue(mixed $value, bool $interpolate = true): string if ($value->hasErrors() && $e = $value->getErrors()) { return \sprintf('throw new RuntimeException(%s)', $this->export(reset($e))); } - if ($this->definitionVariables?->contains($value)) { + if ($this->definitionVariables?->offsetExists($value)) { return $this->dumpValue($this->definitionVariables[$value], $interpolate); } if ($value->getMethodCalls()) { diff --git a/Tests/Compiler/AutowirePassTest.php b/Tests/Compiler/AutowirePassTest.php index 114d514ad..d6bbbc70f 100644 --- a/Tests/Compiler/AutowirePassTest.php +++ b/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); diff --git a/Tests/Dumper/PhpDumperTest.php b/Tests/Dumper/PhpDumperTest.php index a117a69a0..f9302e818 100644 --- a/Tests/Dumper/PhpDumperTest.php +++ b/Tests/Dumper/PhpDumperTest.php @@ -1877,7 +1877,7 @@ public function testClosureProxy() { $container = new ContainerBuilder(); $container->register('closure_proxy', SingleMethodInterface::class) - ->setPublic('true') + ->setPublic(true) ->setFactory(['Closure', 'fromCallable']) ->setArguments([[new Reference('foo'), 'cloneFoo']]) ->setLazy(true); @@ -1899,12 +1899,12 @@ public function testClosure() { $container = new ContainerBuilder(); $container->register('closure', 'Closure') - ->setPublic('true') + ->setPublic(true) ->setFactory(['Closure', 'fromCallable']) ->setArguments([new Reference('bar')]); $container->register('bar', 'stdClass'); $container->register('closure_of_service_closure', 'Closure') - ->setPublic('true') + ->setPublic(true) ->setFactory(['Closure', 'fromCallable']) ->setArguments([new ServiceClosureArgument(new Reference('bar2'))]); $container->register('bar2', 'stdClass'); @@ -1918,15 +1918,15 @@ public function testAutowireClosure() { $container = new ContainerBuilder(); $container->register('foo', Foo::class) - ->setPublic('true'); + ->setPublic(true); $container->register('my_callable', MyCallable::class) - ->setPublic('true'); + ->setPublic(true); $container->register('baz', \Closure::class) ->setFactory(['Closure', 'fromCallable']) ->setArguments(['var_dump']) - ->setPublic('true'); + ->setPublic(true); $container->register('bar', LazyClosureConsumer::class) - ->setPublic('true') + ->setPublic(true) ->setAutowired(true); $container->compile(); $dumper = new PhpDumper($container); @@ -1952,12 +1952,12 @@ public function testLazyClosure() { $container = new ContainerBuilder(); $container->register('closure1', 'Closure') - ->setPublic('true') + ->setPublic(true) ->setFactory(['Closure', 'fromCallable']) ->setLazy(true) ->setArguments([[new Reference('foo'), 'cloneFoo']]); $container->register('closure2', 'Closure') - ->setPublic('true') + ->setPublic(true) ->setFactory(['Closure', 'fromCallable']) ->setLazy(true) ->setArguments([[new Reference('foo_void'), '__invoke']]); @@ -1991,10 +1991,10 @@ public function testLazyAutowireAttribute() { $container = new ContainerBuilder(); $container->register('foo', Foo::class) - ->setPublic('true'); + ->setPublic(true); $container->setAlias(Foo::class, 'foo'); $container->register('bar', LazyServiceConsumer::class) - ->setPublic('true') + ->setPublic(true) ->setAutowired(true); $container->compile(); $dumper = new PhpDumper($container); @@ -2020,7 +2020,7 @@ public function testLazyAutowireAttributeWithIntersection() { $container = new ContainerBuilder(); $container->register('foo', AAndIInterfaceConsumer::class) - ->setPublic('true') + ->setPublic(true) ->setAutowired(true); $container->compile(); @@ -2048,7 +2048,7 @@ public function testCallableAdapterConsumer() $container = new ContainerBuilder(); $container->register('foo', Foo::class); $container->register('bar', CallableAdapterConsumer::class) - ->setPublic('true') + ->setPublic(true) ->setAutowired(true); $container->compile(); $dumper = new PhpDumper($container);