Skip to content

Commit

Permalink
bug #20688 [FrameworkBundle] Resolve env params in debug:config comma…
Browse files Browse the repository at this point in the history
…nd (nicolas-grekas)

This PR was merged into the 3.2 branch.

Discussion
----------

[FrameworkBundle] Resolve env params in debug:config command

| Q             | A
| ------------- | ---
| Branch?       | 3.2
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #20696
| License       | MIT
| Doc PR        | -

Displays e.g. `url: '%env(DATABASE_URL)%'`
instead of `url: env_DATABASE_URL_b188317b1d181eca5f0be35aefdae9c4`
when doing `bin/console debug:config doctrine`

Commits
-------

695d100 [FrameworkBundle] Resolve env params in debug:config command
  • Loading branch information
nicolas-grekas committed Dec 8, 2016
2 parents a7b5080 + 695d100 commit 8e6023b
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 13 deletions.
Expand Up @@ -80,7 +80,7 @@ protected function execute(InputInterface $input, OutputInterface $output)

$this->validateConfiguration($extension, $configuration);

$configs = $container->getParameterBag()->resolveValue($configs);
$configs = $container->resolveEnvPlaceholders($container->getParameterBag()->resolveValue($configs));

$processor = new Processor();
$config = $processor->processConfiguration($configuration, $configs);
Expand Down
Expand Up @@ -19,6 +19,7 @@
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
use Symfony\Component\Config\FileLocator;

/**
Expand Down Expand Up @@ -96,7 +97,11 @@ protected function execute(InputInterface $input, OutputInterface $output)
$object = $this->getContainerBuilder();

if ($input->getOption('parameters')) {
$object = $object->getParameterBag();
$parameters = array();
foreach ($object->getParameterBag()->all() as $k => $v) {
$parameters[$k] = $object->resolveEnvPlaceholders($v);
}
$object = new ParameterBag($parameters);
$options = array();
} elseif ($parameter = $input->getOption('parameter')) {
$options = array('parameter' => $parameter);
Expand Down
Expand Up @@ -57,7 +57,7 @@ public function describe(OutputInterface $output, $object, array $options = arra
$this->describeContainerService($this->resolveServiceDefinition($object, $options['id']), $options);
break;
case $object instanceof ContainerBuilder && isset($options['parameter']):
$this->describeContainerParameter($object->getParameter($options['parameter']), $options);
$this->describeContainerParameter($object->resolveEnvPlaceholders($object->getParameter($options['parameter'])), $options);
break;
case $object instanceof ContainerBuilder:
$this->describeContainerServices($object, $options);
Expand Down
31 changes: 22 additions & 9 deletions src/Symfony/Component/DependencyInjection/ContainerBuilder.php
Expand Up @@ -1026,34 +1026,47 @@ public function getExpressionLanguageProviders()
}

/**
* Resolves env parameter placeholders in a string.
* Resolves env parameter placeholders in a string or an array.
*
* @param string $string The string to resolve
* @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
*
* @return string The string with env parameters resolved
*/
public function resolveEnvPlaceholders($string, $format = null, array &$usedEnvs = null)
public function resolveEnvPlaceholders($value, $format = null, array &$usedEnvs = null)
{
$bag = $this->getParameterBag();
$envPlaceholders = $bag instanceof EnvPlaceholderParameterBag ? $bag->getEnvPlaceholders() : $this->envPlaceholders;

if (null === $format) {
$format = '%%env(%s)%%';
}

if (is_array($value)) {
$result = array();
foreach ($value as $k => $v) {
$result[$this->resolveEnvPlaceholders($k, $format, $usedEnvs)] = $this->resolveEnvPlaceholders($v, $format, $usedEnvs);
}

return $result;
}

if (!is_string($value)) {
return $value;
}

$bag = $this->getParameterBag();
$envPlaceholders = $bag instanceof EnvPlaceholderParameterBag ? $bag->getEnvPlaceholders() : $this->envPlaceholders;

foreach ($envPlaceholders as $env => $placeholders) {
foreach ($placeholders as $placeholder) {
if (false !== stripos($string, $placeholder)) {
$string = str_ireplace($placeholder, sprintf($format, $env), $string);
if (false !== stripos($value, $placeholder)) {
$value = str_ireplace($placeholder, sprintf($format, $env), $value);
$usedEnvs[$env] = $env;
$this->envCounters[$env] = isset($this->envCounters[$env]) ? 1 + $this->envCounters[$env] : 1;
}
}
}

return $string;
return $value;
}

/**
Expand Down
Expand Up @@ -495,7 +495,7 @@ public function testMerge()
$bag = new EnvPlaceholderParameterBag();
$bag->get('env(Foo)');
$config = new ContainerBuilder($bag);
$config->resolveEnvPlaceholders($bag->get('env(Bar)'));
$this->assertSame(array('%env(Bar)%'), $config->resolveEnvPlaceholders(array($bag->get('env(Bar)'))));
$container->merge($config);
$this->assertEquals(array('Foo' => 0, 'Bar' => 1), $container->getEnvCounters());
}
Expand Down

0 comments on commit 8e6023b

Please sign in to comment.