Skip to content

Commit 7615ef3

Browse files
Merge branch '6.2' into 6.3
* 6.2: [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 6.2.8 Update VERSION for 6.2.7 Update CHANGELOG for 6.2.7 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 [Messenger] Fix TransportNamesStamp deserialization Removed @internal tag on TraceableAuthenticator::getAuthenticator()
2 parents bdab397 + e0e074b commit 7615ef3

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
@@ -90,6 +90,7 @@ class PhpDumper extends Dumper
9090
private array $locatedIds = [];
9191
private string $serviceLocatorTag;
9292
private array $exportedVariables = [];
93+
private array $dynamicParameters = [];
9394
private string $baseClass;
9495
private string $class;
9596
private DumperInterface $proxyDumper;
@@ -135,6 +136,7 @@ public function dump(array $options = []): string|array
135136
$this->targetDirRegex = null;
136137
$this->inlinedRequires = [];
137138
$this->exportedVariables = [];
139+
$this->dynamicParameters = [];
138140
$options = array_merge([
139141
'class' => 'ProjectServiceContainer',
140142
'base_class' => 'Container',
@@ -232,11 +234,12 @@ public function dump(array $options = []): string|array
232234
$this->preload = array_combine($options['preload_classes'], $options['preload_classes']);
233235
}
234236

237+
$code = $this->addDefaultParametersMethod();
235238
$code =
236239
$this->startClass($options['class'], $baseClass, $this->inlineFactories && $proxyClasses).
237240
$this->addServices($services).
238241
$this->addDeprecatedAliases().
239-
$this->addDefaultParametersMethod()
242+
$code
240243
;
241244

242245
$proxyClasses ??= $this->generateProxyClasses();
@@ -400,6 +403,7 @@ class %s extends {$options['class']}
400403
$this->circularReferences = [];
401404
$this->locatedIds = [];
402405
$this->exportedVariables = [];
406+
$this->dynamicParameters = [];
403407
$this->preload = [];
404408

405409
$unusedEnvs = [];
@@ -1570,6 +1574,7 @@ private function addDefaultParametersMethod(): string
15701574

15711575
if ($hasEnum || preg_match("/\\\$container->(?:getEnv\('(?:[-.\w\\\\]*+:)*+\w++'\)|targetDir\.'')/", $export[1])) {
15721576
$dynamicPhp[$key] = sprintf('%s%s => %s,', $export[0], $this->export($key), $export[1]);
1577+
$this->dynamicParameters[$key] = true;
15731578
} else {
15741579
$php[] = sprintf('%s%s => %s,', $export[0], $this->export($key), $export[1]);
15751580
}
@@ -1988,20 +1993,18 @@ private function dumpLiteralClass(string $class): string
19881993

19891994
private function dumpParameter(string $name): string
19901995
{
1991-
if ($this->container->hasParameter($name)) {
1992-
$value = $this->container->getParameter($name);
1993-
$dumpedValue = $this->dumpValue($value, false);
1996+
if (!$this->container->hasParameter($name) || ($this->dynamicParameters[$name] ?? false)) {
1997+
return sprintf('$container->getParameter(%s)', $this->doExport($name));
1998+
}
19941999

1995-
if (!$value || !\is_array($value)) {
1996-
return $dumpedValue;
1997-
}
2000+
$value = $this->container->getParameter($name);
2001+
$dumpedValue = $this->dumpValue($value, false);
19982002

1999-
if (!preg_match("/\\\$container->(?:getEnv\('(?:[-.\w\\\\]*+:)*+\w++'\)|targetDir\.'')/", $dumpedValue)) {
2000-
return sprintf('$container->parameters[%s]', $this->doExport($name));
2001-
}
2003+
if (!$value || !\is_array($value)) {
2004+
return $dumpedValue;
20022005
}
20032006

2004-
return sprintf('$container->getParameter(%s)', $this->doExport($name));
2007+
return sprintf('$container->parameters[%s]', $this->doExport($name));
20052008
}
20062009

20072010
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
@@ -1260,6 +1260,11 @@ public function testDumpHandlesEnumeration()
12601260
->register('foo', FooClassWithEnumAttribute::class)
12611261
->setPublic(true)
12621262
->addArgument(FooUnitEnum::BAR);
1263+
$container
1264+
->register('bar', \stdClass::class)
1265+
->setPublic(true)
1266+
->addArgument('%unit_enum%')
1267+
->addArgument('%enum_array%');
12631268

12641269
$container->setParameter('unit_enum', FooUnitEnum::BAR);
12651270
$container->setParameter('enum_array', [FooUnitEnum::BAR, FooUnitEnum::FOO]);
@@ -1277,6 +1282,11 @@ public function testDumpHandlesEnumeration()
12771282
$this->assertSame(FooUnitEnum::BAR, $container->getParameter('unit_enum'));
12781283
$this->assertSame([FooUnitEnum::BAR, FooUnitEnum::FOO], $container->getParameter('enum_array'));
12791284
$this->assertStringMatchesFormat(<<<'PHP'
1285+
%A
1286+
protected static function getBarService($container)
1287+
{
1288+
return $container->services['bar'] = new \stdClass(\Symfony\Component\DependencyInjection\Tests\Fixtures\FooUnitEnum::BAR, $container->getParameter('enum_array'));
1289+
}
12801290
%A
12811291
private function getDynamicParameter(string $name)
12821292
{

0 commit comments

Comments
 (0)