[DI] Make ContainerBuilder::resolveEnvPlaceholders() able to inline the values of referenced env vars.
This commit is contained in:
parent
635d77b32a
commit
713b0813bc
@ -1241,6 +1241,10 @@ class FrameworkExtension extends Extension
|
||||
if (isset($config['prefix_seed'])) {
|
||||
$container->setParameter('cache.prefix.seed', $config['prefix_seed']);
|
||||
}
|
||||
if ($container->hasParameter('cache.prefix.seed')) {
|
||||
// Inline any env vars referenced in the parameter
|
||||
$container->setParameter('cache.prefix.seed', $container->resolveEnvPlaceholders($container->getParameter('cache.prefix.seed'), true));
|
||||
}
|
||||
foreach (array('doctrine', 'psr6', 'redis') as $name) {
|
||||
if (isset($config[$name = 'default_'.$name.'_provider'])) {
|
||||
$container->setAlias('cache.'.$name, new Alias(Compiler\CachePoolPass::getServiceProvider($container, $config[$name]), false));
|
||||
|
@ -1028,9 +1028,11 @@ class ContainerBuilder extends Container implements TaggedContainerInterface
|
||||
/**
|
||||
* Resolves env parameter placeholders in a string or an array.
|
||||
*
|
||||
* @param mixed $value The value to resolve
|
||||
* @param string|null $format A sprintf() format to use as replacement for env placeholders or null to use the default parameter format
|
||||
* @param array &$usedEnvs Env vars found while resolving are added to this array
|
||||
* @param mixed $value The value to resolve
|
||||
* @param string|true|null $format A sprintf() format returning the replacement for each env var name or
|
||||
* null to resolve back to the original "%env(VAR)%" format or
|
||||
* true to resolve to the actual values of the referenced env vars
|
||||
* @param array &$usedEnvs Env vars found while resolving are added to this array
|
||||
*
|
||||
* @return string The string with env parameters resolved
|
||||
*/
|
||||
@ -1054,12 +1056,20 @@ class ContainerBuilder extends Container implements TaggedContainerInterface
|
||||
}
|
||||
|
||||
$bag = $this->getParameterBag();
|
||||
if (true === $format) {
|
||||
$value = $bag->resolveValue($value);
|
||||
}
|
||||
$envPlaceholders = $bag instanceof EnvPlaceholderParameterBag ? $bag->getEnvPlaceholders() : $this->envPlaceholders;
|
||||
|
||||
foreach ($envPlaceholders as $env => $placeholders) {
|
||||
foreach ($placeholders as $placeholder) {
|
||||
if (false !== stripos($value, $placeholder)) {
|
||||
$value = str_ireplace($placeholder, sprintf($format, $env), $value);
|
||||
if (true === $format) {
|
||||
$resolved = $bag->escapeValue($this->getEnv($env));
|
||||
} else {
|
||||
$resolved = sprintf($format, $env);
|
||||
}
|
||||
$value = str_ireplace($placeholder, $resolved, $value);
|
||||
$usedEnvs[$env] = $env;
|
||||
$this->envCounters[$env] = isset($this->envCounters[$env]) ? 1 + $this->envCounters[$env] : 1;
|
||||
}
|
||||
|
@ -500,6 +500,18 @@ class ContainerBuilderTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertEquals(array('Foo' => 0, 'Bar' => 1), $container->getEnvCounters());
|
||||
}
|
||||
|
||||
public function testResolveEnvValues()
|
||||
{
|
||||
$_ENV['DUMMY_ENV_VAR'] = 'du%%y';
|
||||
|
||||
$container = new ContainerBuilder();
|
||||
$container->setParameter('bar', '%% %env(DUMMY_ENV_VAR)%');
|
||||
|
||||
$this->assertSame('%% du%%%%y', $container->resolveEnvPlaceholders('%bar%', true));
|
||||
|
||||
unset($_ENV['DUMMY_ENV_VAR']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \LogicException
|
||||
*/
|
||||
|
Reference in New Issue
Block a user