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
This commit is contained in:
Fabien Potencier 2017-12-11 12:39:44 -08:00
commit c2266bb949
2 changed files with 27 additions and 5 deletions

View File

@ -1305,6 +1305,11 @@ class ContainerBuilder extends Container implements TaggedContainerInterface
$format = '%%env(%s)%%';
}
$bag = $this->getParameterBag();
if (true === $format) {
$value = $bag->resolveValue($value);
}
if (is_array($value)) {
$result = array();
foreach ($value as $k => $v) {
@ -1317,11 +1322,6 @@ class ContainerBuilder extends Container implements TaggedContainerInterface
if (!is_string($value)) {
return $value;
}
$bag = $this->getParameterBag();
if (true === $format) {
$value = $bag->resolveValue($value);
}
$envPlaceholders = $bag instanceof EnvPlaceholderParameterBag ? $bag->getEnvPlaceholders() : $this->envPlaceholders;
foreach ($envPlaceholders as $env => $placeholders) {

View File

@ -615,6 +615,28 @@ class ContainerBuilderTest extends TestCase
unset($_ENV['DUMMY_ENV_VAR'], $_SERVER['DUMMY_SERVER_VAR'], $_SERVER['HTTP_DUMMY_VAR']);
}
public function testResolveEnvValuesWithArray()
{
$_ENV['ANOTHER_DUMMY_ENV_VAR'] = 'dummy';
$dummyArray = array('1' => 'one', '2' => 'two');
$container = new ContainerBuilder();
$container->setParameter('dummy', '%env(ANOTHER_DUMMY_ENV_VAR)%');
$container->setParameter('dummy2', $dummyArray);
$container->resolveEnvPlaceholders('%dummy%', true);
$container->resolveEnvPlaceholders('%dummy2%', true);
$this->assertInternalType('array', $container->resolveEnvPlaceholders('%dummy2%', true));
foreach ($dummyArray as $key => $value) {
$this->assertArrayHasKey($key, $container->resolveEnvPlaceholders('%dummy2%', true));
}
unset($_ENV['ANOTHER_DUMMY_ENV_VAR']);
}
public function testCompileWithResolveEnv()
{
putenv('DUMMY_ENV_VAR=du%%y');