8000 [DependencyInjection] Escape `%` from parameter-like default values · symfony/symfony@edf11bc · GitHub
[go: up one dir, main page]

Skip to content

Commit edf11bc

Browse files
MatTheCatnicolas-grekas
authored andcommitted
[DependencyInjection] Escape % from parameter-like default values
1 parent 3bc1a40 commit edf11bc

File tree

3 files changed

+35
-4
lines changed

3 files changed

+35
-4lines changed

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use Symfony\Component\DependencyInjection\Exception\AutowiringFailedException;
2323
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
2424
use Symfony\Component\DependencyInjection\LazyProxy\ProxyHelper;
25+
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
2526
use Symfony\Component\DependencyInjection\TypedReference;
2627

2728
/**
@@ -52,6 +53,15 @@ public function __construct(bool $throwOnAutowireException = true)
5253
$this->defaultArgument = new class() {
5354
public $value;
5455
public $names;
56+
public $bag;
57+
58+
public function withValue(\ReflectionParameter $parameter): self
59+
{
60+
$clone = clone $this;
61+
$clone->value = $this->bag->escapeValue($parameter->getDefaultValue());
62+
63+
return $clone;
64+
}
5565
};
5666
}
5767

@@ -60,13 +70,16 @@ public function __construct(bool $throwOnAutowireException = true)
6070
*/
6171
public function process(ContainerBuilder $container)
6272
{
73+
$this->defaultArgument->bag = $container->getParameterBag();
74+
6375
try {
6476
$this->typesClone = clone $this;
6577
parent::process($container);
6678
} finally {
6779
$this->decoratedClass = null;
6880
$this->decoratedId = null;
6981
$this->methodCalls = null;
82+
$this->defaultArgument->bag = null;
7083
$this->defaultArgument->names = null;
7184
$this->getPreviousValue = null;
7285
$this->decoratedMethodIndex = null;
@@ -287,8 +300,7 @@ private function autowireMethod(\ReflectionFunctionAbstract $reflectionMethod, a
287300
}
288301

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

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

300312
if ($parameter->isDefaultValueAvailable()) {
301-
$value = clone $this->defaultArgument;
302-
$value->value = $parameter->getDefaultValue();
313+
$value = $this->defaultArgument->withValue($parameter);
303314
} elseif (!$parameter->allowsNull()) {
304315
throw new AutowiringFailedException($this->currentId, $failureMessage);
305316
}

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

Lines 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

Lines 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