Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 310b2eb

Browse files
[DI] Select specific key from an array resolved env var
1 parent d9c3831 commit 310b2eb

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

src/Symfony/Component/DependencyInjection/EnvVarProcessor.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public static function getProvidedTypes()
4141
'float' => 'float',
4242
'int' => 'int',
4343
'json' => 'array',
44+
'key' => 'string',
4445
'resolve' => 'string',
4546
'string' => 'string',
4647
);
@@ -53,6 +54,25 @@ public function getEnv($prefix, $name, \Closure $getEnv)
5354
{
5455
$i = strpos($name, ':');
5556

57+
if ('key' === $prefix) {
58+
if (false === $i) {
59+
throw new RuntimeException(sprintf('Invalid configuration: env var "key:%s" does not contain a key specifier.', $name));
60+
}
61+
62+
$next = substr($name, $i + 1);
63+
$key = substr($name, 0, $i);
64+
$array = $getEnv($next);
65+
66+
if (!is_array($array)) {
67+
throw new RuntimeException(sprintf('Resolved value of "%s" did not result in an array value.', $next));
68+
}
69+
if (!array_key_exists($key, $array)) {
70+
throw new RuntimeException(sprintf('Key "%s" not found in "%s" (resolved from "%s")', $key, json_encode($array), $next));
71+
}
72+
73+
return $array[$key];
74+
}
75+
5676
if ('file' === $prefix) {
5777
if (!is_scalar($file = $getEnv($name))) {
5878
throw new RuntimeException(sprintf('Invalid file name: env var "%s" is non-scalar.', $name));

0 commit comments

Comments
 (0)