Skip to content

Commit 4887fc3

Browse files
Phantas0sfabpot
authored andcommitted
[DI] Impossible to set an environment variable and then an array as container parameter
1 parent 785114b commit 4887fc3

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
@@ -1294,6 +1294,11 @@ public function resolveEnvPlaceholders($value, $format = null, array &$usedEnvs
12941294
$format = '%%env(%s)%%';
12951295
}
12961296

1297+
$bag = $this->getParameterBag();
1298+
if (true === $format) {
1299+
$value = $bag->resolveValue($value);
1300+
}
1301+
12971302
if (is_array($value)) {
12981303
$result = array();
12991304
foreach ($value as $k => $v) {
@@ -1306,11 +1311,6 @@ public function resolveEnvPlaceholders($value, $format = null, array &$usedEnvs
13061311
if (!is_string($value)) {
13071312
return $value;
13081313
}
1309-
1310-
$bag = $this->getParameterBag();
1311-
if (true === $format) {
1312-
$value = $bag->resolveValue($value);
1313-
}
13141314
$envPlaceholders = $bag instanceof EnvPlaceholderParameterBag ? $bag->getEnvPlaceholders() : $this->envPlaceholders;
13151315

13161316
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)