[DependencyInjection] Fix nested env var with resolve processor
This commit is contained in:
parent
93ed7c3e38
commit
b2a61eec71
|
@ -269,11 +269,17 @@ class EnvVarProcessor implements EnvVarProcessorInterface
|
|||
}
|
||||
|
||||
if ('resolve' === $prefix) {
|
||||
return preg_replace_callback('/%%|%([^%\s]+)%/', function ($match) use ($name) {
|
||||
return preg_replace_callback('/%%|%([^%\s]+)%/', function ($match) use ($name, $getEnv) {
|
||||
if (!isset($match[1])) {
|
||||
return '%';
|
||||
}
|
||||
$value = $this->container->getParameter($match[1]);
|
||||
|
||||
if (str_starts_with($match[1], 'env(') && str_ends_with($match[1], ')') && 'env()' !== $match[1]) {
|
||||
$value = $getEnv(substr($match[1], 4, -1));
|
||||
} else {
|
||||
$value = $this->container->getParameter($match[1]);
|
||||
}
|
||||
|
||||
if (!is_scalar($value)) {
|
||||
throw new RuntimeException(sprintf('Parameter "%s" found when resolving env var "%s" must be scalar, "%s" given.', $match[1], $name, \gettype($value)));
|
||||
}
|
||||
|
|
|
@ -490,6 +490,109 @@ class EnvVarProcessorTest extends TestCase
|
|||
$this->assertEquals('foo', $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider validResolve
|
||||
*/
|
||||
public function testGetEnvResolve($value, $processed)
|
||||
{
|
||||
$container = new ContainerBuilder();
|
||||
$container->setParameter('bar', $value);
|
||||
$container->compile();
|
||||
|
||||
$processor = new EnvVarProcessor($container);
|
||||
|
||||
$result = $processor->getEnv('resolve', 'foo', function () {
|
||||
return '%bar%';
|
||||
});
|
||||
|
||||
$this->assertSame($processed, $result);
|
||||
}
|
||||
|
||||
public function validResolve()
|
||||
{
|
||||
return [
|
||||
['string', 'string'],
|
||||
[1, '1'],
|
||||
[1.1, '1.1'],
|
||||
[true, '1'],
|
||||
[false, ''],
|
||||
];
|
||||
}
|
||||
|
||||
public function testGetEnvResolveNoMatch()
|
||||
{
|
||||
$processor = new EnvVarProcessor(new Container());
|
||||
|
||||
$result = $processor->getEnv('resolve', 'foo', function () {
|
||||
return '%%';
|
||||
});
|
||||
|
||||
$this->assertSame('%', $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider notScalarResolve
|
||||
*/
|
||||
public function testGetEnvResolveNotScalar($value)
|
||||
{
|
||||
$this->expectException(RuntimeException::class);
|
||||
$this->expectExceptionMessage('Parameter "bar" found when resolving env var "foo" must be scalar');
|
||||
|
||||
$container = new ContainerBuilder();
|
||||
$container->setParameter('bar', $value);
|
||||
$container->compile();
|
||||
|
||||
$processor = new EnvVarProcessor($container);
|
||||
|
||||
$processor->getEnv('resolve', 'foo', function () {
|
||||
return '%bar%';
|
||||
});
|
||||
}
|
||||
|
||||
public function notScalarResolve()
|
||||
{
|
||||
return [
|
||||
[null],
|
||||
[[]],
|
||||
];
|
||||
}
|
||||
|
||||
public function testGetEnvResolveNestedEnv()
|
||||
{
|
||||
$container = new ContainerBuilder();
|
||||
$container->setParameter('env(BAR)', 'BAR in container');
|
||||
$container->compile();
|
||||
|
||||
$processor = new EnvVarProcessor($container);
|
||||
$getEnv = \Closure::fromCallable([$processor, 'getEnv']);
|
||||
|
||||
$result = $processor->getEnv('resolve', 'foo', function ($name) use ($getEnv) {
|
||||
return 'foo' === $name ? '%env(BAR)%' : $getEnv('string', $name, function () {});
|
||||
});
|
||||
|
||||
$this->assertSame('BAR in container', $result);
|
||||
}
|
||||
|
||||
public function testGetEnvResolveNestedRealEnv()
|
||||
{
|
||||
$_ENV['BAR'] = 'BAR in environment';
|
||||
|
||||
$container = new ContainerBuilder();
|
||||
$container->setParameter('env(BAR)', 'BAR in container');
|
||||
$container->compile();
|
||||
|
||||
$processor = new EnvVarProcessor($container);
|
||||
$getEnv = \Closure::fromCallable([$processor, 'getEnv']);
|
||||
|
||||
$result = $processor->getEnv('resolve', 'foo', function ($name) use ($getEnv) {
|
||||
return 'foo' === $name ? '%env(BAR)%' : $getEnv('string', $name, function () {});
|
||||
});
|
||||
|
||||
$this->assertSame('BAR in environment', $result);
|
||||
|
||||
unset($_ENV['BAR']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider validCsv
|
||||
*/
|
||||
|
|
Reference in New Issue