Skip to content

Commit

Permalink
[DependencyInjection] resolve array env vars
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomason, James authored and fabpot committed May 18, 2018
1 parent fb88119 commit 4c3b950
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1408,6 +1408,7 @@ public function resolveEnvPlaceholders($value, $format = null, array &$usedEnvs
}
$envPlaceholders = $bag instanceof EnvPlaceholderParameterBag ? $bag->getEnvPlaceholders() : $this->envPlaceholders;

$completed = false;
foreach ($envPlaceholders as $env => $placeholders) {
foreach ($placeholders as $placeholder) {
if (false !== stripos($value, $placeholder)) {
Expand All @@ -1418,14 +1419,19 @@ public function resolveEnvPlaceholders($value, $format = null, array &$usedEnvs
}
if ($placeholder === $value) {
$value = $resolved;
$completed = true;
} else {
if (!is_string($resolved) && !is_numeric($resolved)) {
throw new RuntimeException(sprintf('A string value must be composed of strings and/or numbers, but found parameter "env(%s)" of type %s inside string value "%s".', $env, gettype($resolved), $value));
throw new RuntimeException(sprintf('A string value must be composed of strings and/or numbers, but found parameter "env(%s)" of type %s inside string value "%s".', $env, gettype($resolved), $this->resolveEnvPlaceholders($value)));
}
$value = str_ireplace($placeholder, $resolved, $value);
}
$usedEnvs[$env] = $env;
$this->envCounters[$env] = isset($this->envCounters[$env]) ? 1 + $this->envCounters[$env] : 1;

if ($completed) {
break 2;
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -665,17 +665,49 @@ public function testCompileWithResolveEnv()
putenv('DUMMY_ENV_VAR');
}

public function testCompileWithArrayResolveEnv()
{
putenv('ARRAY={"foo":"bar"}');

$container = new ContainerBuilder();
$container->setParameter('foo', '%env(json:ARRAY)%');
$container->compile(true);

$this->assertSame(array('foo' => 'bar'), $container->getParameter('foo'));

putenv('ARRAY');
}

public function testCompileWithArrayAndAnotherResolveEnv()
{
putenv('DUMMY_ENV_VAR=abc');
putenv('ARRAY={"foo":"bar"}');

$container = new ContainerBuilder();
$container->setParameter('foo', '%env(json:ARRAY)%');
$container->setParameter('bar', '%env(DUMMY_ENV_VAR)%');
$container->compile(true);

$this->assertSame(array('foo' => 'bar'), $container->getParameter('foo'));
$this->assertSame('abc', $container->getParameter('bar'));

putenv('DUMMY_ENV_VAR');
putenv('ARRAY');
}

/**
* @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
* @expectedExceptionMessage A string value must be composed of strings and/or numbers, but found parameter "env(ARRAY)" of type array inside string value "ABC %env(ARRAY)%".
* @expectedExceptionMessage A string value must be composed of strings and/or numbers, but found parameter "env(json:ARRAY)" of type array inside string value "ABC %env(json:ARRAY)%".
*/
public function testCompileWithArrayResolveEnv()
public function testCompileWithArrayInStringResolveEnv()
{
$bag = new TestingEnvPlaceholderParameterBag();
$container = new ContainerBuilder($bag);
$container->setParameter('foo', '%env(ARRAY)%');
$container->setParameter('bar', 'ABC %env(ARRAY)%');
putenv('ARRAY={"foo":"bar"}');

$container = new ContainerBuilder();
$container->setParameter('foo', 'ABC %env(json:ARRAY)%');
$container->compile(true);

putenv('ARRAY');
}

/**
Expand Down Expand Up @@ -1418,11 +1450,3 @@ public function __construct(A $a)
{
}
}

class TestingEnvPlaceholderParameterBag extends EnvPlaceholderParameterBag
{
public function get($name)
{
return 'env(array)' === strtolower($name) ? array(123) : parent::get($name);
}
}

0 comments on commit 4c3b950

Please sign in to comment.