Skip to content

Commit 0a9e93c

Browse files
committed
feature #20618 [DI] Make ContainerBuilder::resolveEnvPlaceholders() able to inline the values of referenced env vars. (nicolas-grekas)
This PR was merged into the 3.3-dev branch. Discussion ---------- [DI] Make ContainerBuilder::resolveEnvPlaceholders() able to inline the values of referenced env vars. | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - Being able to resolve environment variables at compile time as a replacement for `SYMFONY__` special env vars, unlocking their deprecation (see #20100). Commits ------- 713b081 [DI] Make ContainerBuilder::resolveEnvPlaceholders() able to inline the values of referenced env vars.
2 parents afd1e75 + 0f29f9a commit 0a9e93c

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

ContainerBuilder.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1044,9 +1044,11 @@ public function getExpressionLanguageProviders()
10441044
/**
10451045
* Resolves env parameter placeholders in a string or an array.
10461046
*
1047-
* @param mixed $value The value to resolve
1048-
* @param string|null $format A sprintf() format to use as replacement for env placeholders or null to use the default parameter format
1049-
* @param array &$usedEnvs Env vars found while resolving are added to this array
1047+
* @param mixed $value The value to resolve
1048+
* @param string|true|null $format A sprintf() format returning the replacement for each env var name or
1049+
* null to resolve back to the original "%env(VAR)%" format or
1050+
* true to resolve to the actual values of the referenced env vars
1051+
* @param array &$usedEnvs Env vars found while resolving are added to this array
10501052
*
10511053
* @return string The string with env parameters resolved
10521054
*/
@@ -1070,12 +1072,20 @@ public function resolveEnvPlaceholders($value, $format = null, array &$usedEnvs
10701072
}
10711073

10721074
$bag = $this->getParameterBag();
1075+
if (true === $format) {
1076+
$value = $bag->resolveValue($value);
1077+
}
10731078
$envPlaceholders = $bag instanceof EnvPlaceholderParameterBag ? $bag->getEnvPlaceholders() : $this->envPlaceholders;
10741079

10751080
foreach ($envPlaceholders as $env => $placeholders) {
10761081
foreach ($placeholders as $placeholder) {
10771082
if (false !== stripos($value, $placeholder)) {
1078-
$value = str_ireplace($placeholder, sprintf($format, $env), $value);
1083+
if (true === $format) {
1084+
$resolved = $bag->escapeValue($this->getEnv($env));
1085+
} else {
1086+
$resolved = sprintf($format, $env);
1087+
}
1088+
$value = str_ireplace($placeholder, $resolved, $value);
10791089
$usedEnvs[$env] = $env;
10801090
$this->envCounters[$env] = isset($this->envCounters[$env]) ? 1 + $this->envCounters[$env] : 1;
10811091
}

Tests/ContainerBuilderTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,18 @@ public function testMerge()
509509
$this->assertEquals(array('Foo' => 0, 'Bar' => 1), $container->getEnvCounters());
510510
}
511511

512+
public function testResolveEnvValues()
513+
{
514+
$_ENV['DUMMY_ENV_VAR'] = 'du%%y';
515+
516+
$container = new ContainerBuilder();
517+
$container->setParameter('bar', '%% %env(DUMMY_ENV_VAR)%');
518+
519+
$this->assertSame('%% du%%%%y', $container->resolveEnvPlaceholders('%bar%', true));
520+
521+
unset($_ENV['DUMMY_ENV_VAR']);
522+
}
523+
512524
/**
513525
* @expectedException \LogicException
514526
*/

0 commit comments

Comments
 (0)