8000 bug #43798 [Dotenv] Duplicate $_SERVER values in $_ENV if they don't … · symfony/symfony@1d0102e · GitHub
[go: up one dir, main page]

Skip to content

Commit 1d0102e

Browse files
bug #43798 [Dotenv] Duplicate $_SERVER values in $_ENV if they don't exist (fancyweb)
This PR was merged into the 4.4 branch. Discussion ---------- [Dotenv] Duplicate $_SERVER values in $_ENV if they don't exist | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | - | License | MIT | Doc PR | symfony/symfony-docs#... <!-- required for new features --> Ref symfony/recipes#1014 `@nicolas`-grekas found the right approach and the patch. `$_SERVER` values that don't exist in `$_ENV` should be duplicated in `$_ENV` so both superglobals are coherent. Commits ------- 08291b1 [Dotenv] Duplicate $_SERVER values in $_ENV if they don't exist
2 parents 2a98147 + 08291b1 commit 1d0102e

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

src/Symfony/Component/Dotenv/Dotenv.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,12 @@ public function populate(array $values, bool $overrideExistingVars = false): voi
136136

137137
foreach ($values as $name => $value) {
138138
$notHttpName = 0 !== strpos($name, 'HTTP_');
139+
if (isset($_SERVER[$name]) && $notHttpName && !isset($_ENV[$name])) {
140+
$_ENV[$name] = $_SERVER[$name];
141+
}
142+
139143
// don't check existence with getenv() because of thread safety issues
140-
if (!isset($loadedVars[$name]) && (!$overrideExistingVars && (isset($_ENV[$name]) || (isset($_SERVER[$name]) && $notHttpName)))) {
144+
if (!isset($loadedVars[$name]) && !$overrideExistingVars && isset($_ENV[$name])) {
141145
continue;
142146
}
143147

src/Symfony/Component/Dotenv/Tests/DotenvTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,4 +493,14 @@ public function testDoNotUsePutenv()
493493
$this->assertSame('no', $_ENV['TEST_USE_PUTENV']);
494494
$this->assertFalse(getenv('TEST_USE_PUTENV'));
495495
}
496+
497+
public function testSERVERVarsDuplicationInENV()
498+
{
499+
unset($_ENV['SYMFONY_DOTENV_VARS'], $_SERVER['SYMFONY_DOTENV_VARS'], $_ENV['FOO']);
500+
$_SERVER['FOO'] = 'CCC';
501+
502+
(new Dotenv(false))->populate(['FOO' => 'BAR']);
503+
504+
$this->assertSame('CCC', $_ENV['FOO']);
505+
}
496506
}

0 commit comments

Comments
 (0)
0