diff --git a/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php b/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php index dd548f3c123d1..66bf26879b70e 100644 --- a/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php +++ b/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php @@ -92,6 +92,7 @@ class PhpDumper extends Dumper private $locatedIds = []; private $serviceLocatorTag; private $exportedVariables = []; + private $dynamicParameters = []; private $baseClass; /** @@ -141,6 +142,7 @@ public function dump(array $options = []) $this->targetDirRegex = null; $this->inlinedRequires = []; $this->exportedVariables = []; + $this->dynamicParameters = []; $options = array_merge([ 'class' => 'ProjectServiceContainer', 'base_class' => 'Container', @@ -223,11 +225,12 @@ public function dump(array $options = []) $this->preload = array_combine($options['preload_classes'], $options['preload_classes']); } + $code = $this->addDefaultParametersMethod(); $code = $this->startClass($options['class'], $baseClass, $this->inlineFactories && $proxyClasses). $this->addServices($services). $this->addDeprecatedAliases(). - $this->addDefaultParametersMethod() + $code ; $proxyClasses = $proxyClasses ?? $this->generateProxyClasses(); @@ -391,6 +394,7 @@ class %s extends {$options['class']} $this->circularReferences = []; $this->locatedIds = []; $this->exportedVariables = []; + $this->dynamicParameters = []; $this->preload = []; $unusedEnvs = []; @@ -1512,6 +1516,7 @@ private function addDefaultParametersMethod(): string if ($hasEnum || preg_match("/\\\$this->(?:getEnv\('(?:[-.\w]*+:)*+\w++'\)|targetDir\.'')/", $export[1])) { $dynamicPhp[$key] = sprintf('%scase %s: $value = %s; break;', $export[0], $this->export($key), $export[1]); + $this->dynamicParameters[$key] = true; } else { $php[] = sprintf('%s%s => %s,', $export[0], $this->export($key), $export[1]); } @@ -1916,20 +1921,18 @@ private function dumpLiteralClass(string $class): string private function dumpParameter(string $name): string { - if ($this->container->hasParameter($name)) { - $value = $this->container->getParameter($name); - $dumpedValue = $this->dumpValue($value, false); + if (!$this->container->hasParameter($name) || ($this->dynamicParameters[$name] ?? false)) { + return sprintf('$this->getParameter(%s)', $this->doExport($name)); + } - if (!$value || !\is_array($value)) { - return $dumpedValue; - } + $value = $this->container->getParameter($name); + $dumpedValue = $this->dumpValue($value, false); - if (!preg_match("/\\\$this->(?:getEnv\('(?:[-.\w]*+:)*+\w++'\)|targetDir\.'')/", $dumpedValue)) { - return sprintf('$this->parameters[%s]', $this->doExport($name)); - } + if (!$value || !\is_array($value)) { + return $dumpedValue; } - return sprintf('$this->getParameter(%s)', $this->doExport($name)); + return sprintf('$this->parameters[%s]', $this->doExport($name)); } private function getServiceCall(string $id, Reference $reference = null): string diff --git a/src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php b/src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php index 9763d2c158731..694413d670047 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php @@ -1237,6 +1237,11 @@ public function testDumpHandlesEnumeration() ->register('foo', FooClassWithEnumAttribute::class) ->setPublic(true) ->addArgument(FooUnitEnum::BAR); + $container + ->register('bar', \stdClass::class) + ->setPublic(true) + ->addArgument('%unit_enum%') + ->addArgument('%enum_array%'); $container->setParameter('unit_enum', FooUnitEnum::BAR); $container->setParameter('enum_array', [FooUnitEnum::BAR, FooUnitEnum::FOO]); @@ -1254,6 +1259,11 @@ public function testDumpHandlesEnumeration() $this->assertSame(FooUnitEnum::BAR, $container->getParameter('unit_enum')); $this->assertSame([FooUnitEnum::BAR, FooUnitEnum::FOO], $container->getParameter('enum_array')); $this->assertStringMatchesFormat(<<<'PHP' +%A + protected function getBarService() + { + return $this->services['bar'] = new \stdClass(\Symfony\Component\DependencyInjection\Tests\Fixtures\FooUnitEnum::BAR, $this->getParameter('enum_array')); + } %A private function getDynamicParameter(string $name) {
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: