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.
This commit is contained in:
Fabien Potencier 2016-12-17 09:53:28 +01:00
commit 4a597bdb37
3 changed files with 30 additions and 4 deletions

View File

@ -1248,6 +1248,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));

View File

@ -1044,9 +1044,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
*/
@ -1070,12 +1072,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;
}

View File

@ -509,6 +509,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
*/