Skip to content

Commit 11ac234

Browse files
committed
bug symfony#22500 [DotEnv] Don't override existing $_SERVER vars (Pierstoval, javiereguiluz)
This PR was merged into the 3.3-dev branch. Discussion ---------- [DotEnv] Don't override existing $_SERVER vars | Q | A | ------------- | --- | Branch? | master | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | symfony#22493 | License | MIT | Doc PR | ~ As of symfony#22493 I decided to make a PR in order to add a specific test just for this behavior and make sure we never touch `$_SERVER` predefined variables. Commits ------- e575831 Update a test value for consistency 0a95d89 Minor changes to make tests more readable 3bf7e49 [DotEnv] Don't override existing $_SERVER vars
2 parents 3471b58 + e575831 commit 11ac234

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

src/Symfony/Component/Dotenv/Dotenv.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public function load($path/*, ...$paths*/)
6767
public function populate($values)
6868
{
6969
foreach ($values as $name => $value) {
70-
if (isset($_ENV[$name]) || false !== getenv($name)) {
70+
if (isset($_ENV[$name]) || isset($_SERVER[$name]) || false !== getenv($name)) {
7171
continue;
7272
}
7373

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,4 +154,24 @@ public function testLoadDirectory()
154154
$dotenv = new Dotenv();
155155
$dotenv->load(__DIR__);
156156
}
157+
158+
public function testServerSuperglobalIsNotOverriden()
159+
{
160+
$originalValue = $_SERVER['argc'];
161+
162+
$dotenv = new DotEnv();
163+
$dotenv->populate(array('argc' => 'new_value'));
164+
165+
$this->assertSame($originalValue, $_SERVER['argc']);
166+
}
167+
168+
public function testEnvVarIsNotOverriden()
169+
{
170+
putenv('TEST_ENV_VAR=original_value');
171+
172+
$dotenv = new DotEnv();
173+
$dotenv->populate(array('TEST_ENV_VAR' => 'new_value'));
174+
175+
$this->assertSame('original_value', getenv('TEST_ENV_VAR'));
176+
}
157177
}

0 commit comments

Comments
 (0)