Skip to content

Commit e0e074b

Browse files
Merge branch '5.4' into 6.2
* 5.4: [Translation] Decouple TranslatorPathsPass from "debug." convention [VarDumper] Add a bit of test coverage [TwigBridge] Fix flagged malicious url [HttpClient] Fix encoding "+" in URLs [DependencyInjection] Fix dumping array of enums parameters Bump Symfony version to 5.4.22 Update VERSION for 5.4.21 Update CONTRIBUTORS for 5.4.21 Update CHANGELOG for 5.4.21 Removed @internal tag on TraceableAuthenticator::getAuthenticator()
2 parents 83369dd + 2ab1b77 commit e0e074b

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
lines changed

Dumper/PhpDumper.php

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ class PhpDumper extends Dumper
8888
private array $locatedIds = [];
8989
private string $serviceLocatorTag;
9090
private array $exportedVariables = [];
91+
private array $dynamicParameters = [];
9192
private string $baseClass;
9293
private string $class;
9394
private DumperInterface $proxyDumper;
@@ -131,6 +132,7 @@ public function dump(array $options = []): string|array
131132
$this->targetDirRegex = null;
132133
$this->inlinedRequires = [];
133134
$this->exportedVariables = [];
135+
$this->dynamicParameters = [];
134136
$options = array_merge([
135137
'class' => 'ProjectServiceContainer',
136138
'base_class' => 'Container',
@@ -206,11 +208,12 @@ public function dump(array $options = []): string|array
206208
$this->preload = array_combine($options['preload_classes'], $options['preload_classes']);
207209
}
208210

211+
$code = $this->addDefaultParametersMethod();
209212
$code =
210213
$this->startClass($options['class'], $baseClass, $this->inlineFactories && $proxyClasses).
211214
$this->addServices($services).
212215
$this->addDeprecatedAliases().
213-
$this->addDefaultParametersMethod()
216+
$code
214217
;
215218

216219
$proxyClasses ??= $this->generateProxyClasses();
@@ -374,6 +377,7 @@ class %s extends {$options['class']}
374377
$this->circularReferences = [];
375378
$this->locatedIds = [];
376379
$this->exportedVariables = [];
380+
$this->dynamicParameters = [];
377381
$this->preload = [];
378382

379383
$unusedEnvs = [];
@@ -1519,6 +1523,7 @@ private function addDefaultParametersMethod(): string
15191523

15201524
if ($hasEnum || preg_match("/\\\$this->(?:getEnv\('(?:[-.\w\\\\]*+:)*+\w++'\)|targetDir\.'')/", $export[1])) {
15211525
$dynamicPhp[$key] = sprintf('%s%s => %s,', $export[0], $this->export($key), $export[1]);
1526+
$this->dynamicParameters[$key] = true;
15221527
} else {
15231528
$php[] = sprintf('%s%s => %s,', $export[0], $this->export($key), $export[1]);
15241529
}
@@ -1917,20 +1922,18 @@ private function dumpLiteralClass(string $class): string
19171922

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

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

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

1933-
return sprintf('$this->getParameter(%s)', $this->doExport($name));
1936+
return sprintf('$this->parameters[%s]', $this->doExport($name));
19341937
}
19351938

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

Tests/Dumper/PhpDumperTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1228,6 +1228,11 @@ public function testDumpHandlesEnumeration()
12281228
->register('foo', FooClassWithEnumAttribute::class)
12291229
->setPublic(true)
12301230
->addArgument(FooUnitEnum::BAR);
1231+
$container
1232+
->register('bar', \stdClass::class)
1233+
->setPublic(true)
1234+
->addArgument('%unit_enum%')
1235+
->addArgument('%enum_array%');
12311236

12321237
$container->setParameter('unit_enum', FooUnitEnum::BAR);
12331238
$container->setParameter('enum_array', [FooUnitEnum::BAR, FooUnitEnum::FOO]);
@@ -1245,6 +1250,11 @@ public function testDumpHandlesEnumeration()
12451250
$this->assertSame(FooUnitEnum::BAR, $container->getParameter('unit_enum'));
12461251
$this->assertSame([FooUnitEnum::BAR, FooUnitEnum::FOO], $container->getParameter('enum_array'));
12471252
$this->assertStringMatchesFormat(<<<'PHP'
1253+
%A
1254+
protected function getBarService()
1255+
{
1256+
return $this->services['bar'] = new \stdClass(\Symfony\Component\DependencyInjection\Tests\Fixtures\FooUnitEnum::BAR, $this->getParameter('enum_array'));
1257+
}
12481258
%A
12491259
private function getDynamicParameter(string $name)
12501260
{

0 commit comments

Comments
 (0)