Skip to content

Commit

Permalink
[Dotenv] search variable values in ENV first then env file
Browse files Browse the repository at this point in the history
  • Loading branch information
soufianZantar authored and nicolas-grekas committed Oct 8, 2019
1 parent c281b3b commit 79561a7
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 8 deletions.
20 changes: 14 additions & 6 deletions src/Symfony/Component/Dotenv/Dotenv.php
Expand Up @@ -332,7 +332,15 @@ private function resolveCommands($value)

$process = new Process('echo '.$matches[0]);
$process->inheritEnvironmentVariables(true);
$process->setEnv($this->values);

$env = [];
foreach ($this->values as $name => $value) {
if (!isset($_ENV[$name]) && !(isset($_SERVER[$name]) && 0 !== strpos($name, 'HTTP_'))) {
$env[$name] = $value;
}
}
$process->setEnv($env);

try {
$process->mustRun();
} catch (ProcessException $e) {
Expand Down Expand Up @@ -375,14 +383,14 @@ private function resolveVariables($value)
}

$name = $matches['name'];
if (isset($this->values[$name])) {
$value = $this->values[$name];
if (isset($_ENV[$name])) {
$value = $_ENV[$name];
} elseif (isset($_SERVER[$name]) && 0 !== strpos($name, 'HTTP_')) {
$value = $_SERVER[$name];
} elseif (isset($_ENV[$name])) {
$value = $_ENV[$name];
} elseif (isset($this->values[$name])) {
$value = $this->values[$name];
} else {
$value = (string) getenv($name);
$value = '';
}

if (!$matches['opening_brace'] && isset($matches['closing_brace'])) {
Expand Down
17 changes: 17 additions & 0 deletions src/Symfony/Component/Dotenv/Tests/DotenvTest.php
Expand Up @@ -63,6 +63,7 @@ public function testParse($data, $expected)
public function getEnvData()
{
putenv('LOCAL=local');
$_ENV['LOCAL'] = 'local';
$_ENV['REMOTE'] = 'remote';

$tests = [
Expand Down Expand Up @@ -295,4 +296,20 @@ public function testOverridingEnvVarsWithNamesMemorizedInSpecialVar()
$this->assertSame('baz1', getenv('BAZ'));
$this->assertSame('/var/www', getenv('DOCUMENT_ROOT'));
}

public function testGetVariablesValueFromEnvFirst()
{
$_ENV['APP_ENV'] = 'prod';
$dotenv = new Dotenv(true);

$test = "APP_ENV=dev\nTEST1=foo1_\${APP_ENV}";
$values = $dotenv->parse($test);
$this->assertSame('foo1_prod', $values['TEST1']);

if ('\\' !== \DIRECTORY_SEPARATOR) {
$test = "APP_ENV=dev\nTEST2=foo2_\$(php -r 'echo \$_SERVER[\"APP_ENV\"];')";
$values = $dotenv->parse($test);
$this->assertSame('foo2_prod', $values['TEST2']);
}
}
}
2 changes: 1 addition & 1 deletion src/Symfony/Component/Dotenv/composer.json
Expand Up @@ -19,7 +19,7 @@
"php": "^5.5.9|>=7.0.8"
},
"require-dev": {
"symfony/process": "~3.2|~4.0"
"symfony/process": "^3.4.2|^4.0"
},
"autoload": {
"psr-4": { "Symfony\\Component\\Dotenv\\": "" },
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/VarDumper/Dumper/CliDumper.php
Expand Up @@ -28,7 +28,7 @@ class CliDumper extends AbstractDumper
protected $maxStringWidth = 0;
protected $styles = [
// See http://en.wikipedia.org/wiki/ANSI_escape_code#graphics
'default' => '38;5;208',
'default' => '0;38;5;208',
'num' => '1;38;5;38',
'const' => '1;38;5;208',
'str' => '1;38;5;113',
Expand Down

0 comments on commit 79561a7

Please sign in to comment.