Skip to content

Commit 7b32f97

Browse files
committed
bug symfony#44932 [DependencyInjection] Fix nested env var with resolve processor (Laurent Moreau)
This PR was squashed before being merged into the 4.4 branch. Discussion ---------- [DependencyInjection] Fix nested env var with resolve processor | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix symfony#44756 | License | MIT | Doc PR | This PR fixes the issue raised by `@ro0NL`. (see Description) I added some tests but i'm not confident about how to check with real environment variables. This is my first PR, i hope i'm not doing it wrong. Commits ------- b2a61ee [DependencyInjection] Fix nested env var with resolve processor
2 parents 29843b3 + b2a61ee commit 7b32f97

File tree

2 files changed

+111
-2
lines changed

2 files changed

+111
-2
lines changed

src/Symfony/Component/DependencyInjection/EnvVarProcessor.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,11 +269,17 @@ public function getEnv($prefix, $name, \Closure $getEnv)
269269
}
270270

271271
if ('resolve' === $prefix) {
272-
return preg_replace_callback('/%%|%([^%\s]+)%/', function ($match) use ($name) {
272+
return preg_replace_callback('/%%|%([^%\s]+)%/', function ($match) use ($name, $getEnv) {
273273
if (!isset($match[1])) {
274274
return '%';
275275
}
276-
$value = $this->container->getParameter($match[1]);
276+
277+
if (str_starts_with($match[1], 'env(') && str_ends_with($match[1], ')') && 'env()' !== $match[1]) {
278+
$value = $getEnv(substr($match[1], 4, -1));
279+
} else {
280+
$value = $this->container->getParameter($match[1]);
281+
}
282+
277283
if (!is_scalar($value)) {
278284
throw new RuntimeException(sprintf('Parameter "%s" found when resolving env var "%s" must be scalar, "%s" given.', $match[1], $name, \gettype($value)));
279285
}

src/Symfony/Component/DependencyInjection/Tests/EnvVarProcessorTest.php

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,109 @@ public function testRequireFile()
490490
$this->assertEquals('foo', $result);
491491
}
492492

493+
/**
494+
* @dataProvider validResolve
495+
*/
496+
public function testGetEnvResolve($value, $processed)
497+
{
498+
$container = new ContainerBuilder();
499+
$container->setParameter('bar', $value);
500+
$container->compile();
501+
502+
$processor = new EnvVarProcessor($container);
503+
504+
$result = $processor->getEnv('resolve', 'foo', function () {
505+
return '%bar%';
506+
});
507+
508+
$this->assertSame($processed, $result);
509+
}
510+
511+
public function validResolve()
512+
{
513+
return [
514+
['string', 'string'],
515+
[1, '1'],
516+
[1.1, '1.1'],
517+
[true, '1'],
518+
[false, ''],
519+
];
520+
}
521+
522+
public function testGetEnvResolveNoMatch()
523+
{
524+
$processor = new EnvVarProcessor(new Container());
525+
526+
$result = $processor->getEnv('resolve', 'foo', function () {
527+
return '%%';
528+
});
529+
530+
$this->assertSame('%', $result);
531+
}
532+
533+
/**
534+
* @dataProvider notScalarResolve
535+
*/
536+
public function testGetEnvResolveNotScalar($value)
537+
{
538+
$this->expectException(RuntimeException::class);
539+
$this->expectExceptionMessage('Parameter "bar" found when resolving env var "foo" must be scalar');
540+
541+
$container = new ContainerBuilder();
542+
$container->setParameter('bar', $value);
543+
$container->compile();
544+
545+
$processor = new EnvVarProcessor($container);
546+
547+
$processor->getEnv('resolve', 'foo', function () {
548+
return '%bar%';
549+
});
550+
}
551+
552+
public function notScalarResolve()
553+
{
554+
return [
555+
[null],
556+
[[]],
557+
];
558+
}
559+
560+
public function testGetEnvResolveNestedEnv()
561+
{
562+
$container = new ContainerBuilder();
563+
$container->setParameter('env(BAR)', 'BAR in container');
564+
$container->compile();
565+
566+
$processor = new EnvVarProcessor($container);
567+
$getEnv = \Closure::fromCallable([$processor, 'getEnv']);
568+
569+
$result = $processor->getEnv('resolve', 'foo', function ($name) use ($getEnv) {
570+
return 'foo' === $name ? '%env(BAR)%' : $getEnv('string', $name, function () {});
571+
});
572+
573+
$this->assertSame('BAR in container', $result);
574+
}
575+
576+
public function testGetEnvResolveNestedRealEnv()
577+
{
578+
$_ENV['BAR'] = 'BAR in environment';
579+
580+
$container = new ContainerBuilder();
581+
$container->setParameter('env(BAR)', 'BAR in container');
582+
$container->compile();
583+
584+
$processor = new EnvVarProcessor($container);
585+
$getEnv = \Closure::fromCallable([$processor, 'getEnv']);
586+
587+
$result = $processor->getEnv('resolve', 'foo', function ($name) use ($getEnv) {
588+
return 'foo' === $name ? '%env(BAR)%' : $getEnv('string', $name, function () {});
589+
});
590+
591+
$this->assertSame('BAR in environment', $result);
592+
593+
unset($_ENV['BAR']);
594+
}
595+
493596
/**
494597
* @dataProvider validCsv
495598
*/

0 commit comments

Comments
 (0)