Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2e64d9d

Browse files
committed
[DependencyInjection] Escape % from parameter-like default values
1 parent 3bc1a40 commit 2e64d9d

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,11 @@ private function autowireMethod(\ReflectionFunctionAbstract $reflectionMethod, a
288288

289289
// specifically pass the default value
290290
$arguments[$index] = clone $this->defaultArgument;
291-
$arguments[$index]->value = $parameter->getDefaultValue();
291+
$defaultValue = $parameter->getDefaultValue();
292+
if (\is_string($defaultValue)) {
293+
$defaultValue = preg_replace('/(?<!%)(%)([^%]+)\1/', '%%\2%%', $defaultValue);
294+
}
295+
$arguments[$index]->value = $defaultValue;
292296

293297
continue;
294298
}
@@ -299,7 +303,11 @@ private function autowireMethod(\ReflectionFunctionAbstract $reflectionMethod, a
299303

300304
if ($parameter->isDefaultValueAvailable()) {
301305
$value = clone $this->defaultArgument;
302-
$value->value = $parameter->getDefaultValue();
306+
$defaultValue = $parameter->getDefaultValue();
307+
if (\is_string($defaultValue)) {
308+
$defaultValue = preg_replace('/(?<!%)(%)([^%]+)\1/', '%%\2%%', $defaultValue);
309+
}
310+
$value->value = $defaultValue;
303311
} elseif (!$parameter->allowsNull()) {
304312
throw new AutowiringFailedException($this->currentId, $failureMessage);
305313
}

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->assertEquals('%%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)