Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 14 additions & 11 deletions src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ class PhpDumper extends Dumper
private $locatedIds = [];
private $serviceLocatorTag;
private $exportedVariables = [];
private $dynamicParameters = [];
private $baseClass;

/**
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -391,6 +394,7 @@ class %s extends {$options['class']}
$this->circularReferences = [];
$this->locatedIds = [];
$this->exportedVariables = [];
$this->dynamicParameters = [];
$this->preload = [];

$unusedEnvs = [];
Expand Down Expand Up @@ -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]);
}
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand All @@ -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)
{
Expand Down