Navigation Menu

Skip to content

Commit

Permalink
bug #22500 [DotEnv] Don't override existing $_SERVER vars (Pierstoval…
Browse files Browse the repository at this point in the history
…, 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 | #22493
| License       | MIT
| Doc PR        | ~

As of #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
  • Loading branch information
fabpot committed Apr 25, 2017
2 parents 3471b58 + e575831 commit 11ac234
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Symfony/Component/Dotenv/Dotenv.php
Expand Up @@ -67,7 +67,7 @@ public function load($path/*, ...$paths*/)
public function populate($values)
{
foreach ($values as $name => $value) {
if (isset($_ENV[$name]) || false !== getenv($name)) {
if (isset($_ENV[$name]) || isset($_SERVER[$name]) || false !== getenv($name)) {
continue;
}

Expand Down
20 changes: 20 additions & 0 deletions src/Symfony/Component/Dotenv/Tests/DotenvTest.php
Expand Up @@ -154,4 +154,24 @@ public function testLoadDirectory()
$dotenv = new Dotenv();
$dotenv->load(__DIR__);
}

public function testServerSuperglobalIsNotOverriden()
{
$originalValue = $_SERVER['argc'];

$dotenv = new DotEnv();
$dotenv->populate(array('argc' => 'new_value'));

$this->assertSame($originalValue, $_SERVER['argc']);
}

public function testEnvVarIsNotOverriden()
{
putenv('TEST_ENV_VAR=original_value');

$dotenv = new DotEnv();
$dotenv->populate(array('TEST_ENV_VAR' => 'new_value'));

$this->assertSame('original_value', getenv('TEST_ENV_VAR'));
}
}

0 comments on commit 11ac234

Please sign in to comment.