diff --git a/Dotenv.php b/Dotenv.php index 57128b4..8180f4b 100644 --- a/Dotenv.php +++ b/Dotenv.php @@ -136,8 +136,12 @@ public function populate(array $values, bool $overrideExistingVars = false): voi foreach ($values as $name => $value) { $notHttpName = 0 !== strpos($name, 'HTTP_'); + if (isset($_SERVER[$name]) && $notHttpName && !isset($_ENV[$name])) { + $_ENV[$name] = $_SERVER[$name]; + } + // don't check existence with getenv() because of thread safety issues - if (!isset($loadedVars[$name]) && (!$overrideExistingVars && (isset($_ENV[$name]) || (isset($_SERVER[$name]) && $notHttpName)))) { + if (!isset($loadedVars[$name]) && !$overrideExistingVars && isset($_ENV[$name])) { continue; } diff --git a/Tests/DotenvTest.php b/Tests/DotenvTest.php index 4428658..62494e4 100644 --- a/Tests/DotenvTest.php +++ b/Tests/DotenvTest.php @@ -493,4 +493,14 @@ public function testDoNotUsePutenv() $this->assertSame('no', $_ENV['TEST_USE_PUTENV']); $this->assertFalse(getenv('TEST_USE_PUTENV')); } + + public function testSERVERVarsDuplicationInENV() + { + unset($_ENV['SYMFONY_DOTENV_VARS'], $_SERVER['SYMFONY_DOTENV_VARS'], $_ENV['FOO']); + $_SERVER['FOO'] = 'CCC'; + + (new Dotenv(false))->populate(['FOO' => 'BAR']); + + $this->assertSame('CCC', $_ENV['FOO']); + } }