bug #29131 [Dotenv] dont use getenv() to read SYMFONY_DOTENV_VARS (nicolas-grekas)

This PR was merged into the 3.4 branch.

Discussion
----------

[Dotenv] dont use getenv() to read SYMFONY_DOTENV_VARS

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

Since using it is not thread safe, let's not rely on it (already done in other places.)

Commits
-------

21a909a189 [Dotenv] dont use getenv() to read SYMFONY_DOTENV_VARS
This commit is contained in:
Nicolas Grekas 2018-11-08 22:48:38 +01:00
commit d74a4eb2fa
2 changed files with 9 additions and 8 deletions

View File

@ -66,8 +66,8 @@ final class Dotenv
*/
public function populate($values)
{
$loadedVars = array_flip(explode(',', getenv('SYMFONY_DOTENV_VARS')));
unset($loadedVars['']);
$updateLoadedVars = false;
$loadedVars = array_flip(explode(',', isset($_SERVER['SYMFONY_DOTENV_VARS']) ? $_SERVER['SYMFONY_DOTENV_VARS'] : (isset($_ENV['SYMFONY_DOTENV_VARS']) ? $_ENV['SYMFONY_DOTENV_VARS'] : '')));
foreach ($values as $name => $value) {
$notHttpName = 0 !== strpos($name, 'HTTP_');
@ -82,14 +82,15 @@ final class Dotenv
$_SERVER[$name] = $value;
}
$loadedVars[$name] = true;
if (!isset($loadedVars[$name])) {
$loadedVars[$name] = $updateLoadedVars = true;
}
}
if ($loadedVars) {
if ($updateLoadedVars) {
unset($loadedVars['']);
$loadedVars = implode(',', array_keys($loadedVars));
putenv("SYMFONY_DOTENV_VARS=$loadedVars");
$_ENV['SYMFONY_DOTENV_VARS'] = $loadedVars;
$_SERVER['SYMFONY_DOTENV_VARS'] = $loadedVars;
putenv('SYMFONY_DOTENV_VARS='.$_ENV['SYMFONY_DOTENV_VARS'] = $_SERVER['SYMFONY_DOTENV_VARS'] = $loadedVars);
}
}

View File

@ -268,7 +268,7 @@ class DotenvTest extends TestCase
public function testOverridingEnvVarsWithNamesMemorizedInSpecialVar()
{
putenv('SYMFONY_DOTENV_VARS=FOO,BAR,BAZ');
putenv('SYMFONY_DOTENV_VARS='.$_SERVER['SYMFONY_DOTENV_VARS'] = 'FOO,BAR,BAZ');
putenv('FOO=foo');
putenv('BAR=bar');