[DI] Select specific key from an array resolved env var
This commit is contained in:
parent
d9c38310f6
commit
42186a2bac
@ -41,6 +41,7 @@ class EnvVarProcessor implements EnvVarProcessorInterface
|
|||||||
'float' => 'float',
|
'float' => 'float',
|
||||||
'int' => 'int',
|
'int' => 'int',
|
||||||
'json' => 'array',
|
'json' => 'array',
|
||||||
|
'key' => 'bool|int|float|string|array',
|
||||||
'resolve' => 'string',
|
'resolve' => 'string',
|
||||||
'string' => 'string',
|
'string' => 'string',
|
||||||
);
|
);
|
||||||
@ -53,6 +54,25 @@ class EnvVarProcessor implements EnvVarProcessorInterface
|
|||||||
{
|
{
|
||||||
$i = strpos($name, ':');
|
$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 ('file' === $prefix) {
|
||||||
if (!is_scalar($file = $getEnv($name))) {
|
if (!is_scalar($file = $getEnv($name))) {
|
||||||
throw new RuntimeException(sprintf('Invalid file name: env var "%s" is non-scalar.', $name));
|
throw new RuntimeException(sprintf('Invalid file name: env var "%s" is non-scalar.', $name));
|
||||||
|
@ -38,6 +38,7 @@ class RegisterEnvVarProcessorsPassTest extends TestCase
|
|||||||
'float' => array('float'),
|
'float' => array('float'),
|
||||||
'int' => array('int'),
|
'int' => array('int'),
|
||||||
'json' => array('array'),
|
'json' => array('array'),
|
||||||
|
'key' => array('bool', 'int', 'float', 'string', 'array'),
|
||||||
'resolve' => array('string'),
|
'resolve' => array('string'),
|
||||||
'string' => array('string'),
|
'string' => array('string'),
|
||||||
);
|
);
|
||||||
|
@ -314,4 +314,110 @@ class EnvVarProcessorTest extends TestCase
|
|||||||
return 'foo';
|
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',
|
||||||
|
);
|
||||||
|
}));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user