Skip to content

[DI] Select specific key from an array resolved env var #27157

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
May 21, 2018
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
20 changes: 20 additions & 0 deletions src/Symfony/Component/DependencyInjection/EnvVarProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public static function getProvidedTypes()
'float' => 'float',
'int' => 'int',
'json' => 'array',
'key' => 'bool|int|float|string|array',
'resolve' => 'string',
'string' => 'string',
);
Expand All @@ -53,6 +54,25 @@ public function getEnv($prefix, $name, \Closure $getEnv)
{
$i = strpos($name, ':');

if ('key' === $prefix) {
if (false === $i) {
throw new RuntimeException(sprintf('Invalid configuration: env var "key:%s" does not contain a key specifier.', $name));
}

$next = substr($name, $i + 1);
$key = substr($name, 0, $i);
$array = $getEnv($next);

if (!is_array($array)) {
throw new RuntimeException(sprintf('Resolved value of "%s" did not result in an array value.', $next));
}
if (!array_key_exists($key, $array)) {
throw new RuntimeException(sprintf('Key "%s" not found in "%s" (resolved from "%s")', $key, json_encode($array), $next));
}

return $array[$key];
}

if ('file' === $prefix) {
if (!is_scalar($file = $getEnv($name))) {
throw new RuntimeException(sprintf('Invalid file name: env var "%s" is non-scalar.', $name));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public function testSimpleProcessor()
'float' => array('float'),
'int' => array('int'),
'json' => array('array'),
'key' => array('bool', 'int', 'float', 'string', 'array'),
'resolve' => array('string'),
'string' => array('string'),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -314,4 +314,110 @@ public function testGetEnvUnknown()
return 'foo';
});
}

/**
* @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
* @expectedExceptionMessage Invalid configuration: env var "key:foo" does not contain a key specifier.
*/
public function testGetEnvKeyInvalidKey()
{
$processor = new EnvVarProcessor(new Container());

$processor->getEnv('key', 'foo', function ($name) {
$this->fail('Should not get here');
});
}

/**
* @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
* @expectedExceptionMessage Resolved value of "foo" did not result in an array value.
* @dataProvider noArrayValues
*/
public function testGetEnvKeyNoArrayResult($value)
{
$processor = new EnvVarProcessor(new Container());

$processor->getEnv('key', 'index:foo', function ($name) use ($value) {
$this->assertSame('foo', $name);

return $value;
});
}

public function noArrayValues()
{
return array(
array(null),
array('string'),
array(1),
array(true),
);
}

/**
* @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
* @expectedExceptionMessage Key "index" not found in
* @dataProvider invalidArrayValues
*/
public function testGetEnvKeyArrayKeyNotFound($value)
{
$processor = new EnvVarProcessor(new Container());

$processor->getEnv('key', 'index:foo', function ($name) use ($value) {
$this->assertSame('foo', $name);

return $value;
});
}

public function invalidArrayValues()
{
return array(
array(array()),
array(array('index2' => 'value')),
array(array('index', 'index2')),
);
}

/**
* @dataProvider arrayValues
*/
public function testGetEnvKey($value)
{
$processor = new EnvVarProcessor(new Container());

$this->assertSame($value['index'], $processor->getEnv('key', 'index:foo', function ($name) use ($value) {
$this->assertSame('foo', $name);

return $value;
}));
}

public function arrayValues()
{
return array(
array(array('index' => 'password')),
array(array('index' => 'true')),
array(array('index' => false)),
array(array('index' => '1')),
array(array('index' => 1)),
array(array('index' => '1.1')),
array(array('index' => 1.1)),
array(array('index' => array())),
array(array('index' => array('val1', 'val2'))),
);
}

public function testGetEnvKeyChained()
{
$processor = new EnvVarProcessor(new Container());

$this->assertSame('password', $processor->getEnv('key', 'index:file:foo', function ($name) {
$this->assertSame('file:foo', $name);

return array(
'index' => 'password',
);
}));
}
}