Skip to content

Commit

Permalink
[Dotenv] Duplicate $_SERVER values in $_ENV if they don't exist
Browse files Browse the repository at this point in the history
Co-authored-by: Nicolas Grekas <nicolas.grekas@gmail.com>
  • Loading branch information
fancyweb and nicolas-grekas committed Oct 28, 2021
1 parent 87c8fd1 commit de87d7b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
6 changes: 5 additions & 1 deletion Dotenv.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
10 changes: 10 additions & 0 deletions Tests/DotenvTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}
}

0 comments on commit de87d7b

Please sign in to comment.