8000 [FrameworkBundle] [Console] Added resolve-env option to debug:config … · symfony/symfony@7910a87 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7910a87

Browse files
[FrameworkBundle] [Console] Added resolve-env option to debug:config command
This option replaces environment variables placeholders by their actual value in the command output
1 parent bb1e1e5 commit 7910a87

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ CHANGELOG
1919
* Add `KernelTestCase::getContainer()` as the best way to get a container in tests
2020
* Rename the container parameter `profiler_listener.only_master_requests` to `profiler_listener.only_main_requests`
2121
* Add service `fragment.uri_generator` to generate the URI of a fragment
22+
* Add `--resolve-env` option to `debug:config` command to resolve the value of environment variables in the command output
2223

2324
5.2.0
2425
-----

src/Symfony/Bundle/FrameworkBundle/Command/ConfigDebugCommand.php

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\Console\Exception\LogicException;
1616
use Symfony\Component\Console\Input\InputArgument;
1717
use Symfony\Component\Console\Input\InputInterface;
18+
use Symfony\Component\Console\Input\InputOption;
1819
use Symfony\Component\Console\Output\OutputInterface;
1920
use Symfony\Component\Console\Style\SymfonyStyle;
2021
use Symfony\Component\DependencyInjection\Compiler\ValidateEnvPlaceholdersPass;
@@ -44,6 +45,7 @@ protected function configure()
4445
->setDefinition([
4546
new InputArgument('name', InputArgument::OPTIONAL, 'The bundle name or the extension alias'),
4647
new InputArgument('path', InputArgument::OPTIONAL, 'The configuration option path'),
48+
new InputOption('resolve-env', null, InputOption::VALUE_NONE, 'Resolve the value of environment variables'),
4749
])
4850
->setDescription(self::$defaultDescription)
4951
->setHelp(<<<'EOF'
@@ -105,7 +107,21 @@ protected function execute(InputInterface $input, OutputInterface $output): int
105107
throw new \LogicException(sprintf('The extension with alias "%s" does not have configuration.', $extensionAlias));
106108
}
107109

108-
$config = $container->resolveEnvPlaceholders($extensionConfig[$extensionAlias]);
110+
$config = $container->resolveEnvPlaceholders($extensionConfig[$extensionAlias], null, $usedEnvs);
111+
112+
if ($input->getOption('resolve-env')) {
113+
$resolvedEnvVars = [];
114+
foreach ($usedEnvs as $env) {
115+
$resolvedEnvVars[$env] = $this->resolveEnvironmentVariableValue($env, $container);
116+
}
117+
118+
array_walk_recursive($config, function (&$value) use ($resolvedEnvVars) {
119+
preg_match_all('{%env\(((?:\w++:)*+\w++)\)%}', $value, $envVars);
120+
$name = current($envVars[1]);
121+
122+
$value = $name ? $resolvedEnvVars[$name] : $value;
123+
});
124+
}
109125

110126
if (null === $path = $input->getArgument('path')) {
111127
$io->title(
@@ -166,4 +182,32 @@ private function getConfigForPath(array $config, string $path, string $alias)
166182

167183
return $config;
168184
}
185+
186+
/**
187+
* Returns and resolve the value of a given environment variable.
188+
*
189+
* @return string|null
190+
*/
191+
private function resolveEnvironmentVariableValue(string $env, ContainerBuilder $container)
192+
{
193+
$bag = $container->getParameterBag();
194+
$getDefaultParameter = function (string $name) {
195+
return parent::get($name);
196+
};
197+
$getDefaultParameter = $getDefaultParameter->bindTo($bag, \get_class($bag));
198+
199+
$getEnvReflection = new \ReflectionMethod($container, 'getEnv');
200+
$getEnvReflection->setAccessible(true);
201+
202+
if (false !== $i = strrpos($name = $env, ':')) {
203+
$name = substr($env, $i + 1);
204+
}
205+
$defaultValue = ($hasDefault = $container->hasParameter("env($name)")) ? $getDefaultParameter("env($name)") : null;
206+
if (false === ($runtimeValue = $_ENV[$name] ?? $_SERVER[$name] ?? getenv($name))) {
207+
$runtimeValue = null;
208+
}
209+
$processedValue = ($hasRuntime = null !== $runtimeValue) || $hasDefault ? $getEnvReflection->invoke($container, $env) : null;
210+
211+
return $processedValue ?? $runtimeValue ?? $defaultValue;
212+
}
169213
}

0 commit comments

Comments
 (0)
0