Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 713b081

Browse files
[DI] Make ContainerBuilder::resolveEnvPlaceholders() able to inline the values of referenced env vars.
1 parent 635d77b commit 713b081

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
@@ -1241,6 +1241,10 @@ private function registerCacheConfiguration(array $config, ContainerBuilder $con
12411241
if (isset($config['prefix_seed'])) {
12421242
$container->setParameter('cache.prefix.seed', $config['prefix_seed']);
12431243
}
1244+
if ($container->hasParameter('cache.prefix.seed')) {
1245+
// Inline any env vars referenced in the parameter
1246+
$container->setParameter('cache.prefix.seed', $container->resolveEnvPlaceholders($container->getParameter('cache.prefix.seed'), true));
1247+
}
12441248
foreach (array('doctrine', 'psr6', 'redis') as $name) {
12451249
if (isset($config[$name = 'default_'.$name.'_provider'])) {
12461250
$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
@@ -1028,9 +1028,11 @@ public function getExpressionLanguageProviders()
10281028
/**
10291029
* Resolves env parameter placeholders in a string or an array.
10301030
*
1031-
* @param mixed $value The value to resolve
1032-
* @param string|null $format A sprintf() format to use as replacement for env placeholders or null to use the default parameter format
1033-
* @param array &$usedEnvs Env vars found while resolving are added to this array
1031+
* @param mixed $value The value to resolve
1032+
* @param string|true|null $format A sprintf() format returning the replacement for each env var name or
1033+
* null to resolve back to the original "%env(VAR)%" format or
1034+
* true to resolve to the actual values of the referenced env vars
1035+
* @param array &$usedEnvs Env vars found while resolving are added to this array
10341036
*
10351037
* @return string The string with env parameters resolved
10361038
*/
@@ -1054,12 +1056,20 @@ public function resolveEnvPlaceholders($value, $format = null, array &$usedEnvs
10541056
}
10551057

10561058
$bag = $this->getParameterBag();
1059+
if (true === $format) {
1060+
$value = $bag->resolveValue($value);
1061+
}
10571062
$envPlaceholders = $bag instanceof EnvPlaceholderParameterBag ? $bag->getEnvPlaceholders() : $this->envPlaceholders;
10581063

10591064
foreach ($envPlaceholders as $env => $placeholders) {
10601065
foreach ($placeholders as $placeholder) {
10611066
if (false !== stripos($value, $placeholder)) {
1062-
$value = str_ireplace($placeholder, sprintf($format, $env), $value);
1067+
if (true === $format) {
1068+
$resolved = $bag->escapeValue($this->getEnv($env));
1069+
} else {
1070+
$resolved = sprintf($format, $env);
1071+
}
1072+
$value = str_ireplace($placeholder, $resolved, $value);
10631073
$usedEnvs[$env] = $env;
10641074
$this->envCounters[$env] = isset($this->envCounters[$env]) ? 1 + $this->envCounters[$env] : 1;
10651075
}

src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php

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

503+
public function testResolveEnvValues()
504+
{
505+
$_ENV['DUMMY_ENV_VAR'] = 'du%%y';
506+
507+
$container = new ContainerBuilder();
508+
$container->setParameter('bar', '%% %env(DUMMY_ENV_VAR)%');
509+
510+
$this->assertSame('%% du%%%%y', $container->resolveEnvPlaceholders('%bar%', true));
511+
512+
unset($_ENV['DUMMY_ENV_VAR']);
513+
}
514+
503515
/**
504516
* @expectedException \LogicException
505517
*/

0 commit comments

Comments
 (0)