Skip to content

Commit 0370545

Browse files
bug #20688 [FrameworkBundle] Resolve env params in debug:config command (nicolas-grekas)
This PR was merged into the 3.2 branch. Discussion ---------- [FrameworkBundle] Resolve env params in debug:config command | Q | A | ------------- | --- | Branch? | 3.2 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #20696 | License | MIT | Doc PR | - Displays e.g. `url: '%env(DATABASE_URL)%'` instead of `url: env_DATABASE_URL_b188317b1d181eca5f0be35aefdae9c4` when doing `bin/console debug:config doctrine` Commits ------- 695d100 [FrameworkBundle] Resolve env params in debug:config command
2 parents 93aedf1 + 1fec6bd commit 0370545

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

ContainerBuilder.php

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1026,34 +1026,47 @@ public function getExpressionLanguageProviders()
10261026
}
10271027

10281028
/**
1029-
* Resolves env parameter placeholders in a string.
1029+
* Resolves env parameter placeholders in a string or an array.
10301030
*
1031-
* @param string $string The string to resolve
1031+
* @param mixed $value The value to resolve
10321032
* @param string|null $format A sprintf() format to use as replacement for env placeholders or null to use the default parameter format
10331033
* @param array &$usedEnvs Env vars found while resolving are added to this array
10341034
*
10351035
* @return string The string with env parameters resolved
10361036
*/
1037-
public function resolveEnvPlaceholders($string, $format = null, array &$usedEnvs = null)
1037+
public function resolveEnvPlaceholders($value, $format = null, array &$usedEnvs = null)
10381038
{
1039-
$bag = $this->getParameterBag();
1040-
$envPlaceholders = $bag instanceof EnvPlaceholderParameterBag ? $bag->getEnvPlaceholders() : $this->envPlaceholders;
1041-
10421039
if (null === $format) {
10431040
$format = '%%env(%s)%%';
10441041
}
10451042

1043+
if (is_array($value)) {
1044+
$result = array();
1045+
foreach ($value as $k => $v) {
1046+
$result[$this->resolveEnvPlaceholders($k, $format, $usedEnvs)] = $this->resolveEnvPlaceholders($v, $format, $usedEnvs);
1047+
}
1048+
1049+
return $result;
1050+
}
1051+
1052+
if (!is_string($value)) {
1053+
return $value;
1054+
}
1055+
1056+
$bag = $this->getParameterBag();
1057+
$envPlaceholders = $bag instanceof EnvPlaceholderParameterBag ? $bag->getEnvPlaceholders() : $this->envPlaceholders;
1058+
10461059
foreach ($envPlaceholders as $env => $placeholders) {
10471060
foreach ($placeholders as $placeholder) {
1048-
if (false !== stripos($string, $placeholder)) {
1049-
$string = str_ireplace($placeholder, sprintf($format, $env), $string);
1061+
if (false !== stripos($value, $placeholder)) {
1062+
$value = str_ireplace($placeholder, sprintf($format, $env), $value);
10501063
$usedEnvs[$env] = $env;
10511064
$this->envCounters[$env] = isset($this->envCounters[$env]) ? 1 + $this->envCounters[$env] : 1;
10521065
}
10531066
}
10541067
}
10551068

1056-
return $string;
1069+
return $value;
10571070
}
10581071

10591072
/**

Tests/ContainerBuilderTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,7 @@ public function testMerge()
495495
$bag = new EnvPlaceholderParameterBag();
496496
$bag->get('env(Foo)');
497497
$config = new ContainerBuilder($bag);
498-
$config->resolveEnvPlaceholders($bag->get('env(Bar)'));
498+
$this->assertSame(array('%env(Bar)%'), $config->resolveEnvPlaceholders(array($bag->get('env(Bar)'))));
499499
$container->merge($config);
500500
$this->assertEquals(array('Foo' => 0, 'Bar' => 1), $container->getEnvCounters());
501501
}

0 commit comments

Comments
 (0)