Skip to content

Commit 20ab1ba

Browse files
committed
bug #25333 [DI] Impossible to set an environment variable and then an array as container parameter (Phantas0s)
This PR was squashed before being merged into the 3.3 branch (closes #25333). Discussion ---------- [DI] Impossible to set an environment variable and then an array as container parameter | Q | A | ------------- | --- | Branch? | 3.3 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #25245 | License | MIT When an environment variables and then an array is set as container parameters, an error is thrown (Warning: stripos() expects parameter 1 to be string, array given). You can run my test without the fix in the Container builder to see it. Commits ------- 484a082eb1 [DI] Impossible to set an environment variable and then an array as container parameter
2 parents ce52d13 + 4887fc3 commit 20ab1ba

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

ContainerBuilder.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1305,6 +1305,11 @@ public function resolveEnvPlaceholders($value, $format = null, array &$usedEnvs
13051305
$format = '%%env(%s)%%';
13061306
}
13071307

1308+
$bag = $this->getParameterBag();
1309+
if (true === $format) {
1310+
$value = $bag->resolveValue($value);
1311+
}
1312+
13081313
if (is_array($value)) {
13091314
$result = array();
13101315
foreach ($value as $k => $v) {
@@ -1317,11 +1322,6 @@ public function resolveEnvPlaceholders($value, $format = null, array &$usedEnvs
13171322
if (!is_string($value)) {
13181323
return $value;
13191324
}
1320-
1321-
$bag = $this->getParameterBag();
1322-
if (true === $format) {
1323-
$value = $bag->resolveValue($value);
1324-
}
13251325
$envPlaceholders = $bag instanceof EnvPlaceholderParameterBag ? $bag->getEnvPlaceholders() : $this->envPlaceholders;
13261326

13271327
foreach ($envPlaceholders as $env => $placeholders) {

Tests/ContainerBuilderTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,28 @@ public function testResolveEnvValues()
615615
unset($_ENV['DUMMY_ENV_VAR'], $_SERVER['DUMMY_SERVER_VAR'], $_SERVER['HTTP_DUMMY_VAR']);
616616
}
617617

618+
public function testResolveEnvValuesWithArray()
619+
{
620+
$_ENV['ANOTHER_DUMMY_ENV_VAR'] = 'dummy';
621+
622+
$dummyArray = array('1' => 'one', '2' => 'two');
623+
624+
$container = new ContainerBuilder();
625+
$container->setParameter('dummy', '%env(ANOTHER_DUMMY_ENV_VAR)%');
626+
$container->setParameter('dummy2', $dummyArray);
627+
628+
$container->resolveEnvPlaceholders('%dummy%', true);
629+
$container->resolveEnvPlaceholders('%dummy2%', true);
630+
631+
$this->assertInternalType('array', $container->resolveEnvPlaceholders('%dummy2%', true));
632+
633+
foreach ($dummyArray as $key => $value) {
634+
$this->assertArrayHasKey($key, $container->resolveEnvPlaceholders('%dummy2%', true));
635+
}
636+
637+
unset($_ENV['ANOTHER_DUMMY_ENV_VAR']);
638+
}
639+
618640
public function testCompileWithResolveEnv()
619641
{
620642
putenv('DUMMY_ENV_VAR=du%%y');

0 commit comments

Comments
 (0)