[FrameworkBundle] Resolve env params in debug:config command

This commit is contained in:
Nicolas Grekas 2016-11-29 18:32:01 +01:00
parent bd5af67f08
commit 695d10071b
5 changed files with 31 additions and 13 deletions

View File

@ -80,7 +80,7 @@ EOF
$this->validateConfiguration($extension, $configuration);
$configs = $container->getParameterBag()->resolveValue($configs);
$configs = $container->resolveEnvPlaceholders($container->getParameterBag()->resolveValue($configs));
$processor = new Processor();
$config = $processor->processConfiguration($configuration, $configs);

View File

@ -19,6 +19,7 @@ use Symfony\Component\Console\Output\OutputInterface;
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;
/**
@ -96,7 +97,11 @@ EOF
$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);

View File

@ -57,7 +57,7 @@ abstract class Descriptor implements DescriptorInterface
$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);

View File

@ -1026,34 +1026,47 @@ class ContainerBuilder extends Container implements TaggedContainerInterface
}
/**
* 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;
}
/**

View File

@ -495,7 +495,7 @@ class ContainerBuilderTest extends \PHPUnit_Framework_TestCase
$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());
}