Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit bb4eeb0

Browse filesBrowse files
MatTheCatnicolas-grekas
authored andcommitted
[DependencyInjection] Escape % from parameter-like default values
1 parent 3bc1a40 commit bb4eeb0
Copy full SHA for bb4eeb0

File tree

3 files changed

+34
-4
lines changed
Filter options

3 files changed

+34
-4
lines changed

‎src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php
+14-4Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,15 @@ public function __construct(bool $throwOnAutowireException = true)
5252
$this->defaultArgument = new class() {
5353
public $value;
5454
public $names;
55+
public $bag;
56+
57+
public function withValue(\ReflectionParameter $parameter): self
58+
{
59+
$clone = clone $this;
60+
$clone->value = $this->bag->escapeValue($parameter->getDefaultValue());
61+
62+
return $clone;
63+
}
5564
};
5665
}
5766

@@ -60,13 +69,16 @@ public function __construct(bool $throwOnAutowireException = true)
6069
*/
6170
public function process(ContainerBuilder $container)
6271
{
72+
$this->defaultArgument->bag = $container->getParameterBag();
73+
6374
try {
6475
$this->typesClone = clone $this;
6576
parent::process($container);
6677
} finally {
6778
$this->decoratedClass = null;
6879
$this->decoratedId = null;
6980
$this->methodCalls = null;
81+
$this->defaultArgument->bag = null;
7082
$this->defaultArgument->names = null;
7183
$this->getPreviousValue = null;
7284
$this->decoratedMethodIndex = null;
@@ -287,8 +299,7 @@ private function autowireMethod(\ReflectionFunctionAbstract $reflectionMethod, a
287299
}
288300

289301
// specifically pass the default value
290-
$arguments[$index] = clone $this->defaultArgument;
291-
$arguments[$index]->value = $parameter->getDefaultValue();
302+
$arguments[$index] = $this->defaultArgument->withValue($parameter);
292303

293304
continue;
294305
}
@@ -298,8 +309,7 @@ private function autowireMethod(\ReflectionFunctionAbstract $reflectionMethod, a
298309
$failureMessage = $this->createTypeNotFoundMessageCallback($ref, sprintf('argument "$%s" of method "%s()"', $parameter->name, $class !== $this->currentId ? $class.'::'.$method : $method));
299310

300311
if ($parameter->isDefaultValueAvailable()) {
301-
$value = clone $this->defaultArgument;
302-
$value->value = $parameter->getDefaultValue();
312+
$value = $this->defaultArgument->withValue($parameter);
303313
} elseif (!$parameter->allowsNull()) {
304314
throw new AutowiringFailedException($this->currentId, $failureMessage);
305315
}

‎src/Symfony/Component/DependencyInjection/Tests/Compiler/AutowirePassTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Tests/Compiler/AutowirePassTest.php
+13Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1219,4 +1219,17 @@ public function testAutowireWithNamedArgs()
12191219

12201220
$this->assertEquals([new TypedReference(A::class, A::class), 'abc'], $container->getDefinition('foo')->getArguments());
12211221
}
1222+
1223+
public function testAutowireDefaultValueParametersLike()
1224+
{
1225+
$container = new ContainerBuilder();
1226+
1227+
$container->register('foo', ParametersLikeDefaultValue::class)
1228+
->setAutowired(true)
1229+
->setArgument(1, 'ok');
1230+
1231+
(new AutowirePass())->process($container);
1232+
1233+
$this->assertSame('%%not%%one%%parameter%%here%%', $container->getDefinition('foo')->getArgument(0));
1234+
}
12221235
}

‎src/Symfony/Component/DependencyInjection/Tests/Fixtures/includes/autowiring_classes.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Tests/Fixtures/includes/autowiring_classes.php
+7Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,3 +431,10 @@ public function __construct(NotExisting $notExisting)
431431
{
432432
}
433433
}
434+
435+
class ParametersLikeDefaultValue
436+
{
437+
public function __construct(string $parameterLike = '%not%one%parameter%here%', string $willBeSetToKeepFirstArgumentDefaultValue = 'ok')
438+
{
439+
}
440+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.