Skip to content

Commit ebf5f9c

Browse files
Merge branch '6.2' into 6.3
* 6.2: [Filesystem] Follow symlinks when dumping files [DependencyInjection] Escape `%` from parameter-like default values
2 parents ec74292 + 98deb65 commit ebf5f9c

File tree

3 files changed

+34
-4
lines changed

3 files changed

+34
-4
lines changed

Compiler/AutowirePass.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,15 @@ public function __construct(bool $throwOnAutowireException = true)
5555
$this->defaultArgument = new class() {
5656
public $value;
5757
public $names;
58+
public $bag;
59+
60+
public function withValue(\ReflectionParameter $parameter): self
61+
{
62+
$clone = clone $this;
63+
$clone->value = $this->bag->escapeValue($parameter->getDefaultValue());
64+
65+
return $clone;
66+
}
5867
};
5968
}
6069

@@ -63,13 +72,16 @@ public function __construct(bool $throwOnAutowireException = true)
6372
*/
6473
public function process(ContainerBuilder $container)
6574
{
75+
$this->defaultArgument->bag = $container->getParameterBag();
76+
6677
try {
6778
$this->typesClone = clone $this;
6879
parent::process($container);
6980
} finally {
7081
$this->decoratedClass = null;
7182
$this->decoratedId = null;
7283
$this->methodCalls = null;
84+
$this->defaultArgument->bag = null;
7385
$this->defaultArgument->names = null;
7486
$this->getPreviousValue = null;
7587
$this->decoratedMethodIndex = null;
@@ -275,8 +287,7 @@ private function autowireMethod(\ReflectionFunctionAbstract $reflectionMethod, a
275287
$failureMessage = $this->createTypeNotFoundMessageCallback($ref, sprintf('argument "$%s" of method "%s()"', $parameter->name, $class !== $this->currentId ? $class.'::'.$method : $method));
276288

277289
if ($parameter->isDefaultValueAvailable()) {
278-
$value = clone $this->defaultArgument;
279-
$value->value = $parameter->getDefaultValue();
290+
$value = $this->defaultArgument->withValue($parameter);
280291
} elseif (!$parameter->allowsNull()) {
281292
throw new AutowiringFailedException($this->currentId, $failureMessage);
282293
}
@@ -374,8 +385,7 @@ private function autowireMethod(\ReflectionFunctionAbstract $reflectionMethod, a
374385
}
375386

376387
// specifically pass the default value
377-
$arguments[$index] = clone $this->defaultArgument;
378-
$arguments[$index]->value = $parameter->getDefaultValue();
388+
$arguments[$index] = $this->defaultArgument->withValue($parameter);
379389

380390
continue;
381391
}

Tests/Compiler/AutowirePassTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1284,6 +1284,19 @@ public function testAutowireWithNamedArgs()
12841284
$this->assertEquals([new TypedReference(A::class, A::class), 'abc'], $container->getDefinition('foo')->getArguments());
12851285
}
12861286

1287+
public function testAutowireDefaultValueParametersLike()
1288+
{
1289+
$container = new ContainerBuilder();
1290+
1291+
$container->register('foo', ParametersLikeDefaultValue::class)
1292+
->setAutowired(true)
1293+
->setArgument(1, 'ok');
1294+
1295+
(new AutowirePass())->process($container);
1296+
1297+
$this->assertSame('%%not%%one%%parameter%%here%%', $container->getDefinition('foo')->getArgument(0));
1298+
}
1299+
12871300
public function testAutowireAttribute()
12881301
{
12891302
$container = new ContainerBuilder();

Tests/Fixtures/includes/autowiring_classes.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,13 @@ public function __construct(NotExisting $notExisting)
528528
}
529529
}
530530

531+
class ParametersLikeDefaultValue
532+
{
533+
public function __construct(string $parameterLike = '%not%one%parameter%here%', string $willBeSetToKeepFirstArgumentDefaultValue = 'ok')
534+
{
535+
}
536+
}
537+
531538
class StaticConstructor
532539
{
533540
public function __construct(private string $bar)

0 commit comments

Comments
 (0)