Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Commitcc6c007

Browse files
bug#50478 [DependencyInjection] Escape% from parameter-like default values (MatTheCat)
This PR was merged into the 5.4 branch.Discussion----------[DependencyInjection] Escape `%` from parameter-like default values| Q | A| ------------- | ---| Branch? | 5.4| Bug fix? | yes| New feature? | no| Deprecations? | no| Tickets |Fix#50469| License | MIT| Doc PR | N/AWhen string default values are autowired, they shouldn’t reference parameters.This PR tries to address the issue by escaping them in the `AutowirePass`.Commits-------bb4eeb0 [DependencyInjection] Escape `%` from parameter-like default values
2 parents3bc1a40 +bb4eeb0 commitcc6c007

File tree

3 files changed

+34
-4
lines changed

3 files changed

+34
-4
lines changed

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

Lines 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 =newclass() {
5353
public$value;
5454
public$names;
55+
public$bag;
56+
57+
publicfunctionwithValue(\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
publicfunctionprocess(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
thrownewAutowiringFailedException($this->currentId,$failureMessage);
305315
}

‎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([newTypedReference(A::class, A::class),'abc'],$container->getDefinition('foo')->getArguments());
12211221
}
1222+
1223+
publicfunctiontestAutowireDefaultValueParametersLike()
1224+
{
1225+
$container =newContainerBuilder();
1226+
1227+
$container->register('foo', ParametersLikeDefaultValue::class)
1228+
->setAutowired(true)
1229+
->setArgument(1,'ok');
1230+
1231+
(newAutowirePass())->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+
publicfunction__construct(string$parameterLike ='%not%one%parameter%here%',string$willBeSetToKeepFirstArgumentDefaultValue ='ok')
438+
{
439+
}
440+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp