Skip to content

Commit

Permalink
[Dotenv] Fix bootEnv() override with .env.local.php when the env key …
Browse files Browse the repository at this point in the history
…already exists
  • Loading branch information
fancyweb committed Jan 26, 2022
1 parent 0c5aee5 commit 84d1af2
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Dotenv.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public function bootEnv(string $path, string $defaultEnv = 'dev', array $testEnv
$env = is_file($p) ? include $p : null;
$k = $this->envKey;

if (\is_array($env) && (!isset($env[$k]) || ($_SERVER[$k] ?? $_ENV[$k] ?? $env[$k]) === $env[$k])) {
if (\is_array($env) && ($overrideExistingVars || !isset($env[$k]) || ($_SERVER[$k] ?? $_ENV[$k] ?? $env[$k]) === $env[$k])) {
$this->populate($env, $overrideExistingVars);
} else {
$this->loadEnv($path, $k, $defaultEnv, $testEnvs, $overrideExistingVars);
Expand Down
7 changes: 7 additions & 0 deletions Tests/DotenvTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,13 @@ public function testBootEnv()
$this->assertSame('BAR', $_SERVER['FOO']);
$this->assertSame('1', $_SERVER['TEST_APP_DEBUG']);
$this->assertSame('localphpNEW_VALUE', $_SERVER['EXISTING_KEY']);

$resetContext();
$_SERVER['TEST_APP_ENV'] = 'ccc';
(new Dotenv('TEST_APP_ENV', 'TEST_APP_DEBUG'))->bootEnv($path, 'dev', ['test'], true);
$this->assertSame('BAR', $_SERVER['FOO']);
$this->assertSame('1', $_SERVER['TEST_APP_DEBUG']);
$this->assertSame('localphpNEW_VALUE', $_SERVER['EXISTING_KEY']);
unlink($path.'.local.php');

$resetContext();
Expand Down

0 comments on commit 84d1af2

Please sign in to comment.