8000 [DependencyInjection] Load enum DI parameters as dynamic parameters · symfony/symfony@da3f323 · GitHub
[go: up one dir, main page]

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 da3f323

Browse files
committed
[DependencyInjection] Load enum DI parameters as dynamic parameters
1 parent 4470c32 commit da3f323

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
throw new InvalidArgumentException(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-
private function exportParameters(array $parameters, string $path = '', int $indent = 12): string
1549+
private function exportParameters(array $parameters, string $path = '', int $indent = 12, &$hasEnum = false): string
15491550
{
15501551
$php = [];
15511552
foreach ($parameters as $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 ($value instanceof ArgumentInterface) {
15551556
throw new InvalidArgumentException(sprintf('You cannot dump a container with parameters that contain special arguments. "%s" found in "%s".', \get_class($value), $path.'/'.$key));
15561557
} elseif ($value instanceof Variable) {
@@ -1561,6 +1562,9 @@ private function exportParameters(array $parameters, string $path = '', int $ind
15611562
throw new InvalidArgumentException(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 ($value instanceof Expression) {
15631564
throw new InvalidArgumentException(sprintf('You cannot dump a container with parameters that contain expressions. Expression "%s" found in "%s".', $value, $path.'/'.$key));
1565+
} elseif ($value instanceof \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) { return var_export($part, true); }, $cleanParts);
20912095
$export = implode('."\n".', $cleanParts);
2092-
} elseif ($value instanceof \UnitEnum) {
2093-
return sprintf('\%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 function getDefaultParameters(): array
1238+
private function getDynamicParameter(string $name)
12391239
{
1240-
return [
1241-
'unit_enum' => \Symfony\Component\DependencyInjection\Tests\Fixtures\FooUnitEnum::B A204 AR,
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)
0