Skip to content

Commit 4a597bd

Browse files
committed
feature symfony#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 symfony#20100). Commits ------- 713b081 [DI] Make ContainerBuilder::resolveEnvPlaceholders() able to inline the values of referenced env vars.
2 parents 3eab98d + 713b081 commit 4a597bd

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1248,6 +1248,10 @@ private function registerCacheConfiguration(array $config, ContainerBuilder $con
12481248
if (isset($config['prefix_seed'])) {
12491249
$container->setParameter('cache.prefix.seed', $config['prefix_seed']);
12501250
}
1251+
if ($container->hasParameter('cache.prefix.seed')) {
1252+
// Inline any env vars referenced in the parameter
1253+
$container->setParameter('cache.prefix.seed', $container->resolveEnvPlaceholders($container->getParameter('cache.prefix.seed'), true));
1254+
}
12511255
foreach (array('doctrine', 'psr6', 'redis') as $name) {
12521256
if (isset($config[$name = 'default_'.$name.'_provider'])) {
12531257
$container->setAlias('cache.'.$name, new Alias(Compiler\CachePoolPass::getServiceProvider($container, $config[$name]), false));

src/Symfony/Component/DependencyInjection/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
}

src/Symfony/Component/DependencyInjection/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)