8000 [Dotenv] search variable values in ENV first then env file · symfony/symfony@79561a7 · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit 79561a7

Browse files
soufianZantarnicolas-grekas
authored andcommitted
[Dotenv] search variable values in ENV first then env file
1 parent c281b3b commit 79561a7

File tree

4 files changed

+33
-8
lines changed

4 files changed

+33
-8
lines changed

src/Symfony/Component/Dotenv/Dotenv.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,15 @@ private function resolveCommands($value)
332332

333333
$process = new Process('echo '.$matches[0]);
334334
$process->inheritEnvironmentVariables(true);
335-
$process->setEnv($this->values);
335+
336+
$env = [];
337+
foreach ($this->values as $name => $value) {
338+
if (!isset($_ENV[$name]) && !(isset($_SERVER[$name]) && 0 !== strpos($name, 'HTTP_'))) {
339+
$env[$name] = $value;
340+
}
341+
}
342+
$process->setEnv($env);
343+
336344
try {
337345
$process->mustRun();
338346
} catch (ProcessException $e) {
@@ -375,14 +383,14 @@ private function resolveVariables($value)
375383
}
376384

377385
$name = $matches['name'];
378-
if (isset($this->values[$name])) {
379-
$value = $this->values[$name];
386+
if (isset($_ENV[$name])) {
387+
$value = $_ENV[$name];
380388
} elseif (isset($_SERVER[$name]) && 0 !== strpos($name, 'HTTP_')) {
381389
$value = $_SERVER[$name];
382-
} elseif (isset($_ENV[$name])) {
383-
$value = $_ENV[$name];
390+
} elseif (isset($this->values[$name])) {
391+
$value = $this->values[$name];
384392
} else {
385-
$value = (string) getenv($name);
393+
$value = '';
386394
}
387395

388396
if (!$matches['opening_brace'] && isset($matches['closing_brace'])) {

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ public function testParse($data, $expected)
6363
public function getEnvData()
6464
{
6565
putenv('LOCAL=local');
66+
$_ENV['LOCAL'] = 'local';
6667
$_ENV['REMOTE'] = 'remote';
6768

6869
$tests = [
@@ -295,4 +296,20 @@ public function testOverridingEnvVarsWithNamesMemorizedInSpecialVar()
295296
$this->assertSame('baz1', getenv('BAZ'));
296297
$this->assertSame('/var/www', getenv('DOCUMENT_ROOT'));
297298
}
299+
300+
public function testGetVariablesValueFromEnvFirst()
301+
{
302+
$_ENV['APP_ENV'] = 'prod';
303+
$dotenv = new Dotenv 8000 (true);
304+
305+
$test = "APP_ENV=dev\nTEST1=foo1_\${APP_ENV}";
306+
$values = $dotenv->parse($test);
307+
$this->assertSame('foo1_prod', $values['TEST1']);
308+
309+
if ('\\' !== \DIRECTORY_SEPARATOR) {
310+
$test = "APP_ENV=dev\nTEST2=foo2_\$(php -r 'echo \$_SERVER[\"APP_ENV\"];')";
311+
$values = $dotenv->parse($test);
312+
$this->assertSame('foo2_prod', $values['TEST2']);
313+
}
314+
}
298315
}

src/Symfony/Component/Dotenv/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"php": "^5.5.9|>=7.0.8"
2020
},
2121
"require-dev": {
22-
"symfony/process": "~3.2|~4.0"
22+
"symfony/proces 9734 s": "^3.4.2|^4.0"
2323
},
2424
"autoload": {
2525
"psr-4": { "Symfony\\Component\\Dotenv\\": "" },

src/Symfony/Component/VarDumper/Dumper/CliDumper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class CliDumper extends AbstractDumper
2828
protected $maxStringWidth = 0;
2929
protected $styles = [
3030
// See http://en.wikipedia.org/wiki/ANSI_escape_code#graphics
31-
'default' => '38;5;208',
31+
'default' => '0;38;5;208',
3232
'num' => '1;38;5;38',
3333
'const' => '1;38;5;208',
3434
'str' => '1;38;5;113',

0 commit comments

Comments
 (0)
0