Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit bafde61

Browse filesBrowse files
soufianZantarnicolas-grekas
authored andcommitted
[Dotenv] search variable values in ENV first then env file
1 parent c281b3b commit bafde61
Copy full SHA for bafde61

File tree

3 files changed

+32
-7
lines changed
Filter options

3 files changed

+32
-7
lines changed

‎src/Symfony/Component/Dotenv/Dotenv.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Dotenv/Dotenv.php
+14-6Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Dotenv/Tests/DotenvTest.php
+17Lines 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(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/VarDumper/Dumper/CliDumper.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/VarDumper/Dumper/CliDumper.php
+1-1Lines 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)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.