8000 [DependencyInjection] Fix nested env var with resolve processor by Laulibrius · Pull Request #44932 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[DependencyInjection] Fix nested env var with resolve processor #44932

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
Jan 7, 2022
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
10 changes: 8 additions & 2 deletions src/Symfony/Component/DependencyInjection/EnvVarProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -269,11 +269,17 @@ public function getEnv($prefix, $name, \Closure $getEnv)
}

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)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,109 @@ public function testRequireFile()
$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('r 5975 esolve', '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
*/
Expand Down
0