From 5101d188c4efe087380f904f995fd3859f76f26a Mon Sep 17 00:00:00 2001 From: Quentin Devos <4972091+Okhoshi@users.noreply.github.com> Date: Thu, 1 Jun 2023 16:41:36 +0200 Subject: [PATCH] [DependencyInjection] Fix support for `false` boolean env vars --- .../DependencyInjection/EnvVarProcessor.php | 2 +- .../Tests/EnvVarProcessorTest.php | 62 +++++++++++++++++++ 2 files changed, 63 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/DependencyInjection/EnvVarProcessor.php b/src/Symfony/Component/DependencyInjection/EnvVarProcessor.php index 5959d8892055..b7633c0fea8a 100644 --- a/src/Symfony/Component/DependencyInjection/EnvVarProcessor.php +++ b/src/Symfony/Component/DependencyInjection/EnvVarProcessor.php @@ -146,7 +146,7 @@ public function getEnv(string $prefix, string $name, \Closure $getEnv): mixed if (false !== $i || 'string' !== $prefix) { $env = $getEnv($name); } elseif ('' === ($env = $_ENV[$name] ?? (str_starts_with($name, 'HTTP_') ? null : ($_SERVER[$name] ?? null))) - || false === ($env = $env ?? getenv($name) ?? false) // null is a possible value because of thread safety issues + || (false !== $env && false === ($env = $env ?? getenv($name) ?? false)) // null is a possible value because of thread safety issues ) { foreach ($this->loadedVars as $vars) { if (false !== ($env = ($vars[$name] ?? $env)) && '' !== $env) { diff --git a/src/Symfony/Component/DependencyInjection/Tests/EnvVarProcessorTest.php b/src/Symfony/Component/DependencyInjection/Tests/EnvVarProcessorTest.php index 5e1833861e68..ac4ca68b10f2 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/EnvVarProcessorTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/EnvVarProcessorTest.php @@ -59,6 +59,67 @@ public static function validStrings() ]; } + /** + * @dataProvider validRealEnvValues + */ + public function testGetEnvRealEnv($value, $processed) + { + $_ENV['FOO'] = $value; + + $processor = new EnvVarProcessor(new Container()); + + $result = $processor->getEnv('string', 'FOO', function () { + $this->fail('Should not be called'); + }); + + $this->assertSame($processed, $result); + + unset($_ENV['FOO']); + } + + public static function validRealEnvValues() + { + return [ + ['hello', 'hello'], + [true, '1'], + [false, ''], + [1, '1'], + [0, '0'], + [1.1, '1.1'], + [10, '10'], + ]; + } + + public function testGetEnvRealEnvInvalid() + { + $_ENV['FOO'] = null; + $this->expectException(EnvNotFoundException::class); + $this->expectExceptionMessage('Environment variable not found: "FOO".'); + + $processor = new EnvVarProcessor(new Container()); + + $processor->getEnv('string', 'FOO', function () { + $this->fail('Should not be called'); + }); + + unset($_ENV['FOO']); + } + + public function testGetEnvRealEnvNonScalar() + { + $_ENV['FOO'] = []; + $this->expectException(RuntimeException::class); + $this->expectExceptionMessage('Non-scalar env var "FOO" cannot be cast to "string".'); + + $processor = new EnvVarProcessor(new Container()); + + $processor->getEnv('string', 'FOO', function () { + $this->fail('Should not be called'); + }); + + unset($_ENV['FOO']); + } + /** * @dataProvider validBools */ @@ -97,6 +158,7 @@ public static function validBools() ['true', true], ['false', false], ['null', false], + ['', false], ['1', true], ['0', false], ['1.1', true],