Skip to content

Commit 98deb65

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

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
@@ -58,18 +58,30 @@ public function __construct(bool $throwOnAutowireException = true)
5858
$this->defaultArgument = new class() {
5959
public $value;
6060
public $names;
61+
public $bag;
62+
63+
public function withValue(\ReflectionParameter $parameter): self
64+
{
65+
$clone = clone $this;
66+
$clone->value = $this->bag->escapeValue($parameter->getDefaultValue());
67+
68+
return $clone;
69+
}
6170
};
6271
}
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;
@@ -315,8 +327,7 @@ private function autowireMethod(\ReflectionFunctionAbstract $reflectionMethod, a
315327
}
316328

317329
// specifically pass the default value
318-
$arguments[$index] = clone $this->defaultArgument;
319-
$arguments[$index]->value = $parameter->getDefaultValue();
330+
$arguments[$index] = $this->defaultArgument->withValue($parameter);
320331

321332
continue;
322333
}
@@ -326,8 +337,7 @@ private function autowireMethod(\ReflectionFunctionAbstract $reflectionMethod, a
326337
$failureMessage = $this->createTypeNotFoundMessageCallback($ref, sprintf('argument "$%s" of method "%s()"', $parameter->name, $class !== $this->currentId ? $class.'::'.$method : $method));
327338

328339
if ($parameter->isDefaultValueAvailable()) {
329-
$value = clone $this->defaultArgument;
330-
$value->value = $parameter->getDefaultValue();
340+
$value = $this->defaultArgument->withValue($parameter);
331341
} elseif (!$parameter->allowsNull()) {
332342
throw new AutowiringFailedException($this->currentId, $failureMessage);
333343
}

Tests/Compiler/AutowirePassTest.php

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

1177+
public function testAutowireDefaultValueParametersLike()
1178+
{
1179+
$container = new ContainerBuilder();
1180+
1181+
$container->register('foo', ParametersLikeDefaultValue::class)
1182+
->setAutowired(true)
1183+
->setArgument(1, 'ok');
1184+
1185+
(new AutowirePass())->process($container);
1186+
1187+
$this->assertSame('%%not%%one%%parameter%%here%%', $container->getDefinition('foo')->getArgument(0));
1188+
}
1189+
11771190
public function testAutowireAttribute()
11781191
{
11791192
$container = new ContainerBuilder();

Tests/Fixtures/includes/autowiring_classes.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,3 +422,10 @@ public function __construct(NotExisting $notExisting)
422422
{
423423
}
424424
}
425+
426+
class ParametersLikeDefaultValue
427+
{
428+
public function __construct(string $parameterLike = '%not%one%parameter%here%', string $willBeSetToKeepFirstArgumentDefaultValue = 'ok')
429+
{
430+
}
431+
}

0 commit comments

Comments
 (0)