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

Commite6fb077

Browse files
committed
[DependencyInjection] Load enum DI parameters as dynamic parameters
1 parent8d5d26b commite6fb077

File tree

2 files changed

+16
-14
lines changed

2 files changed

+16
-14
lines changed

‎src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1436,10 +1436,11 @@ private function addDefaultParametersMethod(): string
14361436
if ($key !==$resolvedKey =$this->container->resolveEnvPlaceholders($key)) {
14371437
thrownewInvalidArgumentException(sprintf('Parameter name cannot use env parameters: "%s".',$resolvedKey));
14381438
}
1439-
$export =$this->exportParameters([$value]);
1439+
$hasEnum =false;
1440+
$export =$this->exportParameters([$value],'',12,$hasEnum);
14401441
$export =explode('0 =>',substr(rtrim($export," ]\n"),2, -1),2);
14411442

1442-
if (preg_match("/\\\$this->(?:getEnv\('(?:[-.\w]*+:)*+\w++'\)|targetDir\.'')/",$export[1])) {
1443+
if ($hasEnum ||preg_match("/\\\$this->(?:getEnv\('(?:[-.\w]*+:)*+\w++'\)|targetDir\.'')/",$export[1])) {
14431444
$dynamicPhp[$key] =sprintf('%scase %s: $value = %s; break;',$export[0],$this->export($key),$export[1]);
14441445
}else {
14451446
$php[] =sprintf('%s%s => %s,',$export[0],$this->export($key),$export[1]);
@@ -1545,12 +1546,12 @@ protected function getDefaultParameters(): array
15451546
/**
15461547
* @throws InvalidArgumentException
15471548
*/
1548-
privatefunctionexportParameters(array$parameters,string$path ='',int$indent =12):string
1549+
privatefunctionexportParameters(array$parameters,string$path ='',int$indent =12,bool &$hasEnum =false):string
15491550
{
15501551
$php = [];
15511552
foreach ($parametersas$key =>$value) {
15521553
if (\is_array($value)) {
1553-
$value =$this->exportParameters($value,$path.'/'.$key,$indent +4);
1554+
$value =$this->exportParameters($value,$path.'/'.$key,$indent +4,$hasEnum);
15541555
}elseif ($valueinstanceof ArgumentInterface) {
15551556
thrownewInvalidArgumentException(sprintf('You cannot dump a container with parameters that contain special arguments. "%s" found in "%s".',\get_class($value),$path.'/'.$key));
15561557
}elseif ($valueinstanceof Variable) {
@@ -1561,6 +1562,9 @@ private function exportParameters(array $parameters, string $path = '', int $ind
15611562
thrownewInvalidArgumentException(sprintf('You cannot dump a container with parameters that contain references to other services (reference to service "%s" found in "%s").',$value,$path.'/'.$key));
15621563
}elseif ($valueinstanceof Expression) {
15631564
thrownewInvalidArgumentException(sprintf('You cannot dump a container with parameters that contain expressions. Expression "%s" found in "%s".',$value,$path.'/'.$key));
1565+
}elseif ($valueinstanceof \UnitEnum) {
1566+
$hasEnum =true;
1567+
$value =sprintf('\%s::%s',\get_class($value),$value->name);
15641568
}else {
15651569
$value =$this->export($value);
15661570
}
@@ -2089,8 +2093,6 @@ private function doExport($value, bool $resolveEnv = false)
20892093
$cleanParts =explode("\n",$value);
20902094
$cleanParts =array_map(function ($part) {returnvar_export($part,true); },$cleanParts);
20912095
$export =implode('."\n".',$cleanParts);
2092-
}elseif ($valueinstanceof \UnitEnum) {
2093-
returnsprintf('\%s::%s',\get_class($value),$value->name);
20942096
}else {
20952097
$export =var_export($value,true);
20962098
}

‎src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1233,18 +1233,18 @@ public function testDumpHandlesEnumeration()
12331233
$this->assertSame(FooUnitEnum::BAR,$container->get('foo')->getBar());
12341234
$this->assertSame(FooUnitEnum::BAR,$container->getParameter('unit_enum'));
12351235
$this->assertSame([FooUnitEnum::BAR, FooUnitEnum::FOO],$container->getParameter('enum_array'));
1236-
$this->assertStringMatchesFormat(<<<PHP
1236+
$this->assertStringMatchesFormat(<<<'PHP'
12371237
%A
1238-
protected functiongetDefaultParameters(): array
1238+
private functiongetDynamicParameter(string $name)
12391239
{
1240-
return [
1241-
'unit_enum' => \Symfony\Component\DependencyInjection\Tests\Fixtures\FooUnitEnum::BAR,
1242-
'enum_array' => [
1240+
switch ($name) {
1241+
case'unit_enum': $value = \Symfony\Component\DependencyInjection\Tests\Fixtures\FooUnitEnum::BAR; break;
1242+
case'enum_array': $value = [
12431243
0 => \Symfony\Component\DependencyInjection\Tests\Fixtures\FooUnitEnum::BAR,
12441244
1 => \Symfony\Component\DependencyInjection\Tests\Fixtures\FooUnitEnum::FOO,
1245-
],
1246-
];
1247-
}
1245+
]; break;
1246+
default: throw new InvalidArgumentException(sprintf('The dynamic parameter "%s" must be defined.', $name));
1247+
}
12481248
%A
12491249
PHP
12501250
,$dumpedContainer

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp