8000 [FrameworkBundle] Resolve env params in debug:config command by nicolas-grekas · Pull Request #20688 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[FrameworkBundle] Resolve env params in debug:config command #20688

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 8, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
< 43FB /tr>
Original file line number Diff line number Diff line change
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