8000 bug #49578 [DependencyInjection] Fix dumping array of enums parameter… · symfony/symfony@c7b787b · GitHub
[go: up one dir, main page]

Skip to content

Commit c7b787b

Browse files
bug #49578 [DependencyInjection] Fix dumping array of enums parameters (fancyweb)
This PR was merged into the 5.4 branch. Discussion ---------- [DependencyInjection] Fix dumping array of enums parameters | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | #49505 | License | MIT | Doc PR | - Commits ------- 97c5874 [DependencyInjection] Fix dumping array of enums parameters
2 parents 8197ada + 97c5874 commit c7b787b

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
lines changed

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

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ class PhpDumper extends Dumper
9292
private $locatedIds = [];
9393
private $serviceLocatorTag;
9494
private $exportedVariables = [];
95+
private $dynamicParameters = [];
9596
private $baseClass;
9697

9798
/**
@@ -141,6 +142,7 @@ public function dump(array $options = [])
141142
$this->targetDirRegex = null;
142143
$this->inlinedRequires = [];
143144
$this->exportedVariables = [];
145+
$this->dynamicParameters = [];
144146
$options = array_merge([
145147
'class' => 'ProjectServiceContainer',
146148
'base_class' => 'Container',
@@ -223,11 +225,12 @@ public function dump(array $options = [])
223225
$this->preload = array_combine($options['preload_classes'], $options['preload_classes']);
224226
}
225227

228+
$code = $this->addDefaultParametersMethod();
226229
$code =
227230
$this->startClass($options['class'], $baseClass, $this->inlineFactories && $proxyClasses).
228231
$this->addServices($services).
229232
$this->addDeprecatedAliases().
230-
$this->addDefaultParametersMethod()
233+
$code
231234
;
232235

233236
$proxyClasses = $proxyClasses ?? $this->generateProxyClasses();
@@ -391,6 +394,7 @@ class %s extends {$options['class']}
391394
$this->circularReferences = [];
392395
$this->locatedIds = [];
393396
$this->exportedVariables = [];
397+
$this->dynamicParameters = [];
394398
$this->preload = [];
395399

396400
$unusedEnvs = [];
@@ -1512,6 +1516,7 @@ private function addDefaultParametersMethod(): string
15121516

15131517
if ($hasEnum || preg_match("/\\\$this->(?:getEnv\('(?:[-.\w]*+:)*+\w++'\)|targetDir\.'')/", $export[1])) {
15141518
$dynamicPhp[$key] = sprintf('%scase %s: $value = %s; break;', $export[0], $this->export($key), $export[1]);
1519+
$this->dynamicParameters[$key] = true;
15151520
} else {
15161521
$php[] = sprintf('%s%s => %s,', $export[0], $this->export($key), $export[1]);
15171522
}
@@ -1916,20 +1921,18 @@ private function dumpLiteralClass(string $class): string
19161921

19171922
private function dumpParameter(string $name): string
19181923
{
1919-
if ($this->container->hasParameter($name)) {
1920-
$value = $this->container->getParameter($name);
1921-
$dumpedValue = $this->dumpValue($value, false);
1924+
if (!$this->container->hasParameter($name) || ($this->dynamicParameters[$name] ?? false)) {
1925+
return sprintf('$this->getParameter(%s)', $this->doExport($name));
1926+
}
19221927

1923-
if (!$value || !\is_array($value)) {
1924-
return $dumpedValue;
1925-
}
1928+
$value = $this->container->getParameter($name);
1929+
$dumpedValue = $this->dumpValue($value, false);
19261930

1927-
if (!preg_match("/\\\$this->(?:getEnv\('(?:[-.\w]*+:)*+\w++'\)|targetDir\.'')/", $dumpedValue)) {
1928-
return sprintf('$this->parameters[%s]', $this->doExport($name));
1929-
}
1931+
if (!$value || !\is_array($value)) {
1932+
return $dumpedValue;
19301933
}
19311934

1932-
return sprintf('$this->getParameter(%s)', $this->doExport($name));
1935+
return sprintf('$this->parameters[%s]', $this->doExport($name));
19331936
}
19341937

19351938
private function getServiceCall(string $id, Reference $reference = null): string

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1237,6 +1237,11 @@ public function testDumpHandlesEnumeration()
12371237
->register('foo', FooClassWithEnumAttribute::class)
12381238
->setPublic(true)
12391239
->addArgument(FooUnitEnum::BAR);
1240+
$container
1241+
->register('bar', \stdClass::class)
1242+
->setPublic(true)
1243+
->addArgument('%unit_enum%')
1244+
->addArgument('%enum_array%');
12401245

12411246
$container->setParameter('unit_enum', FooUnitEnum::BAR);
12421247
$container->setParameter('enum_array', [FooUnitEnum::BAR, FooUnitEnum::FOO]);
@@ -1254,6 +1259,11 @@ public function testDumpHandlesEnumeration()
12541259
$this->assertSame(FooUnitEnum::BAR, $container->getParameter('unit_enum'));
12551260
$this->assertSame([FooUnitEnum::BAR, FooUnitEnum::FOO], $container->getParameter('enum_array'));
12561261
$this->assertStringMatchesFormat(<<<'PHP'
1262+
%A
1263+
protected function getBarService()
1264+
{
1265+
return $this->services['bar'] = new \stdClass(\Symfony\Component\DependencyInjection\Tests\Fixtures\FooUnitEnum::BAR, $this->getParameter('enum_array'));
1266+
}
12571267
%A
12581268
private function getDynamicParameter(string $name)
12591269
{

0 commit comments

Comments
 (0)
0