Skip to content

Commit 84d1af2

Browse files
committed
[Dotenv] Fix bootEnv() override with .env.local.php when the env key already exists
1 parent 0c5aee5 commit 84d1af2

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

Dotenv.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ public function bootEnv(string $path, string $defaultEnv = 'dev', array $testEnv
151151
$env = is_file($p) ? include $p : null;
152152
$k = $this->envKey;
153153

154-
if (\is_array($env) && (!isset($env[$k]) || ($_SERVER[$k] ?? $_ENV[$k] ?? $env[$k]) === $env[$k])) {
154+
if (\is_array($env) && ($overrideExistingVars || !isset($env[$k]) || ($_SERVER[$k] ?? $_ENV[$k] ?? $env[$k]) === $env[$k])) {
155155
$this->populate($env, $overrideExistingVars);
156156
} else {
157157
$this->loadEnv($path, $k, $defaultEnv, $testEnvs, $overrideExistingVars);

Tests/DotenvTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,13 @@ public function testBootEnv()
587587
$this->assertSame('BAR', $_SERVER['FOO']);
588588
$this->assertSame('1', $_SERVER['TEST_APP_DEBUG']);
589589
$this->assertSame('localphpNEW_VALUE', $_SERVER['EXISTING_KEY']);
590+
591+
$resetContext();
592+
$_SERVER['TEST_APP_ENV'] = 'ccc';
593+
(new Dotenv('TEST_APP_ENV', 'TEST_APP_DEBUG'))->bootEnv($path, 'dev', ['test'], true);
594+
$this->assertSame('BAR', $_SERVER['FOO']);
595+
$this->assertSame('1', $_SERVER['TEST_APP_DEBUG']);
596+
$this->assertSame('localphpNEW_VALUE', $_SERVER['EXISTING_KEY']);
590597
unlink($path.'.local.php');
591598

592599
$resetContext();

0 commit comments

Comments
 (0)