|
15 | 15 | use Symfony\Component\Console\Exception\LogicException;
|
16 | 16 | use Symfony\Component\Console\Input\InputArgument;
|
17 | 17 | use Symfony\Component\Console\Input\InputInterface;
|
| 18 | +use Symfony\Component\Console\Input\InputOption; |
18 | 19 | use Symfony\Component\Console\Output\OutputInterface;
|
19 | 20 | use Symfony\Component\Console\Style\SymfonyStyle;
|
20 | 21 | use Symfony\Component\DependencyInjection\Compiler\ValidateEnvPlaceholdersPass;
|
@@ -44,6 +45,7 @@ protected function configure()
|
44 | 45 | ->setDefinition([
|
45 | 46 | new InputArgument('name', InputArgument::OPTIONAL, 'The bundle name or the extension alias'),
|
46 | 47 | new InputArgument('path', InputArgument::OPTIONAL, 'The configuration option path'),
|
| 48 | + new InputOption('resolve-env', null, InputOption::VALUE_NONE, 'Resolve the value of environment variables'), |
47 | 49 | ])
|
48 | 50 | ->setDescription(self::$defaultDescription)
|
49 | 51 | ->setHelp(<<<'EOF'
|
@@ -105,7 +107,21 @@ protected function execute(InputInterface $input, OutputInterface $output): int
|
105 | 107 | throw new \LogicException(sprintf('The extension with alias "%s" does not have configuration.', $extensionAlias));
|
106 | 108 | }
|
107 | 109 |
|
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 | + } |
109 | 125 |
|
110 | 126 | if (null === $path = $input->getArgument('path')) {
|
111 | 127 | $io->title(
|
@@ -166,4 +182,32 @@ private function getConfigForPath(array $config, string $path, string $alias)
|
166 | 182 |
|
167 | 183 | return $config;
|
168 | 184 | }
|
| 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 | + } |
169 | 213 | }
|
0 commit comments