diff --git a/src/Symfony/Component/DependencyInjection/Container.php b/src/Symfony/Component/DependencyInjection/Container.php index 838756d47edba..235b76ef44fd1 100644 --- a/src/Symfony/Component/DependencyInjection/Container.php +++ b/src/Symfony/Component/DependencyInjection/Container.php @@ -390,7 +390,15 @@ protected function getEnv(string $name) $prefix = 'string'; $localName = $name; } - $processor = $processors->has($prefix) ? $processors->get($prefix) : new EnvVarProcessor($this); + + if ($processors->has($prefix)) { + $processor = $processors->get($prefix); + } else { + $processor = new EnvVarProcessor($this); + if (false === $i) { + $prefix = ''; + } + } $this->resolving[$envName] = true; try { diff --git a/src/Symfony/Component/DependencyInjection/EnvVarProcessor.php b/src/Symfony/Component/DependencyInjection/EnvVarProcessor.php index aec6a79b228cc..818174b3970c7 100644 --- a/src/Symfony/Component/DependencyInjection/EnvVarProcessor.php +++ b/src/Symfony/Component/DependencyInjection/EnvVarProcessor.php @@ -126,6 +126,12 @@ public function getEnv(string $prefix, string $name, \Closure $getEnv) } } + $returnNull = false; + if ('' === $prefix) { + $returnNull = true; + $prefix = 'string'; + } + if (false !== $i || 'string' !== $prefix) { $env = $getEnv($name); } elseif (isset($_ENV[$name])) { @@ -177,6 +183,10 @@ public function getEnv(string $prefix, string $name, \Closure $getEnv) } if (null === $env) { + if ($returnNull) { + return null; + } + if (!isset($this->getProvidedTypes()[$prefix])) { throw new RuntimeException(sprintf('Unsupported env var prefix "%s".', $prefix)); } diff --git a/src/Symfony/Component/DependencyInjection/Tests/EnvVarProcessorTest.php b/src/Symfony/Component/DependencyInjection/Tests/EnvVarProcessorTest.php index 72cf270a632b6..7ff161c06cee3 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/EnvVarProcessorTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/EnvVarProcessorTest.php @@ -763,12 +763,13 @@ public static function provideGetEnvUrlPath() /** * @testWith ["", "string"] + * [null, ""] * [false, "bool"] * [true, "not"] * [0, "int"] * [0.0, "float"] */ - public function testGetEnvCastsNull($expected, string $prefix) + public function testGetEnvCastsNullBehavior($expected, string $prefix) { $processor = new EnvVarProcessor(new Container()); @@ -778,4 +779,17 @@ public function testGetEnvCastsNull($expected, string $prefix) }); })); } + + public function testGetEnvWithEmptyStringPrefixCastsToString() + { + $processor = new EnvVarProcessor(new Container()); + unset($_ENV['FOO']); + $_ENV['FOO'] = 4; + + try { + $this->assertSame('4', $processor->getEnv('', 'FOO', function () { $this->fail('Should not be called'); })); + } finally { + unset($_ENV['FOO']); + } + } }
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: