Skip to content

Commit f8b4f43

Browse files
Merge branch '3.4' into 4.3
* 3.4: Add plus character `+` to legal mime subtype [Dotenv] search variable values in ENV first then env file [VarDumper] fix resetting the "bold" state in CliDumper SCA: added missing break in a loop
2 parents 1785b18 + d678630 commit f8b4f43

File tree

3 files changed

+43
-14
lines changed

3 files changed

+43
-14
lines changed

Dotenv.php

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,8 @@ private function lexValue()
257257
throw $this->createFormatException('Whitespace are not supported before the value');
258258
}
259259

260+
$loadedVars = array_flip(explode(',', isset($_SERVER['SYMFONY_DOTENV_VARS']) ? $_SERVER['SYMFONY_DOTENV_VARS'] : (isset($_ENV['SYMFONY_DOTENV_VARS']) ? $_ENV['SYMFONY_DOTENV_VARS'] : '')));
261+
unset($loadedVars['']);
260262
$v = '';
261263

262264
do {
@@ -295,8 +297,8 @@ private function lexValue()
295297
++$this->cursor;
296298
$value = str_replace(['\\"', '\r', '\n'], ['"', "\r", "\n"], $value);
297299
$resolvedValue = $value;
298-
$resolvedValue = $this->resolveVariables($resolvedValue);
299-
$resolvedValue = $this->resolveCommands($resolvedValue);
300+
$resolvedValue = $this->resolveVariables($resolvedValue, $loadedVars);
301+
$resolvedValue = $this->resolveCommands($resolvedValue, $loadedVars);
300302
$resolvedValue = str_replace('\\\\', '\\', $resolvedValue);
301303
$v .= $resolvedValue;
302304
} else {
@@ -318,8 +320,8 @@ private function lexValue()
318320
}
319321
$value = rtrim($value);
320322
$resolvedValue = $value;
321-
$resolvedValue = $this->resolveVariables($resolvedValue);
322-
$resolvedValue = $this->resolveCommands($resolvedValue);
323+
$resolvedValue = $this->resolveVariables($resolvedValue, $loadedVars);
324+
$resolvedValue = $this->resolveCommands($resolvedValue, $loadedVars);
323325
$resolvedValue = str_replace('\\\\', '\\', $resolvedValue);
324326

325327
if ($resolvedValue === $value && preg_match('/\s+/', $value)) {
@@ -372,7 +374,7 @@ private function skipEmptyLines()
372374
}
373375
}
374376

375-
private function resolveCommands($value)
377+
private function resolveCommands($value, $loadedVars)
376378
{
377379
if (false === strpos($value, '$')) {
378380
return $value;
@@ -388,7 +390,7 @@ private function resolveCommands($value)
388390
)
389391
/x';
390392

391-
return preg_replace_callback($regex, function ($matches) {
393+
return preg_replace_callback($regex, function ($matches) use ($loadedVars) {
392394
if ('\\' === $matches[1]) {
393395
return substr($matches[0], 1);
394396
}
@@ -403,7 +405,15 @@ private function resolveCommands($value)
403405

404406
$process = method_exists(Process::class, 'fromShellCommandline') ? Process::fromShellCommandline('echo '.$matches[0]) : new Process('echo '.$matches[0]);
405407
$process->inheritEnvironmentVariables(true);
406-
$process->setEnv($this->values);
408+
409+
$env = [];
410+
foreach ($this->values as $name => $value) {
411+
if (isset($loadedVars[$name]) || (!isset($_ENV[$name]) && !(isset($_SERVER[$name]) && 0 !== strpos($name, 'HTTP_')))) {
412+
$env[$name] = $value;
413+
}
414+
}
415+
$process->setEnv($env);
416+
407417
try {
408418
$process->mustRun();
409419
} catch (ProcessException $e) {
@@ -414,7 +424,7 @@ private function resolveCommands($value)
414424
}, $value);
415425
}
416426

417-
private function resolveVariables($value)
427+
private function resolveVariables($value, array $loadedVars)
418428
{
419429
if (false === strpos($value, '$')) {
420430
return $value;
@@ -430,7 +440,7 @@ private function resolveVariables($value)
430440
(?P<closing_brace>\})? # optional closing brace
431441
/x';
432442

433-
$value = preg_replace_callback($regex, function ($matches) {
443+
$value = preg_replace_callback($regex, function ($matches) use ($loadedVars) {
434444
// odd number of backslashes means the $ character is escaped
435445
if (1 === \strlen($matches['backslashes']) % 2) {
436446
return substr($matches[0], 1);
@@ -446,14 +456,16 @@ private function resolveVariables($value)
446456
}
447457

448458
$name = $matches['name'];
449-
if (isset($this->values[$name])) {
459+
if (isset($loadedVars[$name]) && isset($this->values[$name])) {
450460
$value = $this->values[$name];
451-
} elseif (isset($_SERVER[$name]) && 0 !== strpos($name, 'HTTP_')) {
452-
$value = $_SERVER[$name];
453461
} elseif (isset($_ENV[$name])) {
454462
$value = $_ENV[$name];
463+
} elseif (isset($_SERVER[$name]) && 0 !== strpos($name, 'HTTP_')) {
464+
$value = $_SERVER[$name];
465+
} elseif (isset($this->values[$name])) {
466+
$value = $this->values[$name];
455467
} else {
456-
$value = (string) getenv($name);
468+
$value = '';
457469
}
458470

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

Tests/DotenvTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ public function testParse($data, $expected)
6969
public function getEnvData()
7070
{
7171
putenv('LOCAL=local');
72+
$_ENV['LOCAL'] = 'local';
7273
$_ENV['REMOTE'] = 'remote';
7374
$_SERVER['SERVERVAR'] = 'servervar';
7475

@@ -413,6 +414,22 @@ public function testOverridingEnvVarsWithNamesMemorizedInSpecialVar()
413414
$this->assertSame('/var/www', getenv('DOCUMENT_ROOT'));
414415
}
415416

417+
public function testGetVariablesValueFromEnvFirst()
418+
{
419+
$_ENV['APP_ENV'] = 'prod';
420+
$dotenv = new Dotenv(true);
421+
422+
$test = "APP_ENV=dev\nTEST1=foo1_\${APP_ENV}";
423+
$values = $dotenv->parse($test);
424+
$this->assertSame('foo1_prod', $values['TEST1']);
425+
426+
if ('\\' !== \DIRECTORY_SEPARATOR) {
427+
$test = "APP_ENV=dev\nTEST2=foo2_\$(php -r 'echo \$_SERVER[\"APP_ENV\"];')";
428+
$values = $dotenv->parse($test);
429+
$this->assertSame('foo2_prod', $values['TEST2']);
430+
}
431+
}
432+
416433
/**
417434
* @group legacy
418435
* @expectedDeprecation The default value of "$usePutenv" argument of "%s" will be changed from "true" to "false" in Symfony 5.0. You should define its value explicitly.

composer.json

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

0 commit comments

Comments
 (0)