diff --git a/Compiler/AutowirePass.php b/Compiler/AutowirePass.php index 6c3cf82ca..845f09c11 100644 --- a/Compiler/AutowirePass.php +++ b/Compiler/AutowirePass.php @@ -52,6 +52,15 @@ public function __construct(bool $throwOnAutowireException = true) $this->defaultArgument = new class() { public $value; public $names; + public $bag; + + public function withValue(\ReflectionParameter $parameter): self + { + $clone = clone $this; + $clone->value = $this->bag->escapeValue($parameter->getDefaultValue()); + + return $clone; + } }; } @@ -60,6 +69,8 @@ public function __construct(bool $throwOnAutowireException = true) */ public function process(ContainerBuilder $container) { + $this->defaultArgument->bag = $container->getParameterBag(); + try { $this->typesClone = clone $this; parent::process($container); @@ -67,6 +78,7 @@ public function process(ContainerBuilder $container) $this->decoratedClass = null; $this->decoratedId = null; $this->methodCalls = null; + $this->defaultArgument->bag = null; $this->defaultArgument->names = null; $this->getPreviousValue = null; $this->decoratedMethodIndex = null; @@ -287,8 +299,7 @@ private function autowireMethod(\ReflectionFunctionAbstract $reflectionMethod, a } // specifically pass the default value - $arguments[$index] = clone $this->defaultArgument; - $arguments[$index]->value = $parameter->getDefaultValue(); + $arguments[$index] = $this->defaultArgument->withValue($parameter); continue; } @@ -298,8 +309,7 @@ private function autowireMethod(\ReflectionFunctionAbstract $reflectionMethod, a $failureMessage = $this->createTypeNotFoundMessageCallback($ref, sprintf('argument "$%s" of method "%s()"', $parameter->name, $class !== $this->currentId ? $class.'::'.$method : $method)); if ($parameter->isDefaultValueAvailable()) { - $value = clone $this->defaultArgument; - $value->value = $parameter->getDefaultValue(); + $value = $this->defaultArgument->withValue($parameter); } elseif (!$parameter->allowsNull()) { throw new AutowiringFailedException($this->currentId, $failureMessage); } diff --git a/Tests/Compiler/AutowirePassTest.php b/Tests/Compiler/AutowirePassTest.php index e61e2d45f..ac8172503 100644 --- a/Tests/Compiler/AutowirePassTest.php +++ b/Tests/Compiler/AutowirePassTest.php @@ -1219,4 +1219,17 @@ public function testAutowireWithNamedArgs() $this->assertEquals([new TypedReference(A::class, A::class), 'abc'], $container->getDefinition('foo')->getArguments()); } + + public function testAutowireDefaultValueParametersLike() + { + $container = new ContainerBuilder(); + + $container->register('foo', ParametersLikeDefaultValue::class) + ->setAutowired(true) + ->setArgument(1, 'ok'); + + (new AutowirePass())->process($container); + + $this->assertSame('%%not%%one%%parameter%%here%%', $container->getDefinition('foo')->getArgument(0)); + } } diff --git a/Tests/Fixtures/includes/autowiring_classes.php b/Tests/Fixtures/includes/autowiring_classes.php index ad1f06a49..87440891d 100644 --- a/Tests/Fixtures/includes/autowiring_classes.php +++ b/Tests/Fixtures/includes/autowiring_classes.php @@ -431,3 +431,10 @@ public function __construct(NotExisting $notExisting) { } } + +class ParametersLikeDefaultValue +{ + public function __construct(string $parameterLike = '%not%one%parameter%here%', string $willBeSetToKeepFirstArgumentDefaultValue = 'ok') + { + } +}