Skip to content

[Dotenv] search variable values in ENV first then env file #32943

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[Dotenv] search variable values in ENV first then env file
  • Loading branch information
soufianZantar authored and nicolas-grekas committed Oct 11, 2019
commit 30180417823e5f41c69cbb1fd6eee07e71c13c6c
38 changes: 25 additions & 13 deletions src/Symfony/Component/Dotenv/Dotenv.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,8 @@ private function lexValue()
throw $this->createFormatException('Whitespace are not supported before the value');
}

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

do {
Expand Down Expand Up @@ -224,8 +226,8 @@ private function lexValue()
++$this->cursor;
$value = str_replace(['\\"', '\r', '\n'], ['"', "\r", "\n"], $value);
$resolvedValue = $value;
$resolvedValue = $this->resolveVariables($resolvedValue);
$resolvedValue = $this->resolveCommands($resolvedValue);
$resolvedValue = $this->resolveVariables($resolvedValue, $loadedVars);
$resolvedValue = $this->resolveCommands($resolvedValue, $loadedVars);
$resolvedValue = str_replace('\\\\', '\\', $resolvedValue);
$v .= $resolvedValue;
} else {
Expand All @@ -247,8 +249,8 @@ private function lexValue()
}
$value = rtrim($value);
$resolvedValue = $value;
$resolvedValue = $this->resolveVariables($resolvedValue);
$resolvedValue = $this->resolveCommands($resolvedValue);
$resolvedValue = $this->resolveVariables($resolvedValue, $loadedVars);
$resolvedValue = $this->resolveCommands($resolvedValue, $loadedVars);
$resolvedValue = str_replace('\\\\', '\\', $resolvedValue);

if ($resolvedValue === $value && preg_match('/\s+/', $value)) {
Expand Down Expand Up @@ -301,7 +303,7 @@ private function skipEmptyLines()
}
}

private function resolveCommands($value)
private function resolveCommands($value, $loadedVars)
{
if (false === strpos($value, '$')) {
return $value;
Expand All @@ -317,7 +319,7 @@ private function resolveCommands($value)
)
/x';

return preg_replace_callback($regex, function ($matches) {
return preg_replace_callback($regex, function ($matches) use ($loadedVars) {
if ('\\' === $matches[1]) {
return substr($matches[0], 1);
}
Expand All @@ -332,7 +334,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($loadedVars[$name]) || (!isset($_ENV[$name]) && !(isset($_SERVER[$name]) && 0 !== strpos($name, 'HTTP_')))) {
$env[$name] = $value;
}
}
$process->setEnv($env);

try {
$process->mustRun();
} catch (ProcessException $e) {
Expand All @@ -343,7 +353,7 @@ private function resolveCommands($value)
}, $value);
}

private function resolveVariables($value)
private function resolveVariables($value, array $loadedVars)
{
if (false === strpos($value, '$')) {
return $value;
Expand All @@ -359,7 +369,7 @@ private function resolveVariables($value)
(?P<closing_brace>\})? # optional closing brace
/x';

$value = preg_replace_callback($regex, function ($matches) {
$value = preg_replace_callback($regex, function ($matches) use ($loadedVars) {
// odd number of backslashes means the $ character is escaped
if (1 === \strlen($matches['backslashes']) % 2) {
return substr($matches[0], 1);
Expand All @@ -375,14 +385,16 @@ private function resolveVariables($value)
}

$name = $matches['name'];
if (isset($this->values[$name])) {
if (isset($loadedVars[$name]) && isset($this->values[$name])) {
$value = $this->values[$name];
} elseif (isset($_SERVER[$name]) && 0 !== strpos($name, 'HTTP_')) {
$value = $_SERVER[$name];
} elseif (isset($_ENV[$name])) {
$value = $_ENV[$name];
} elseif (isset($_SERVER[$name]) && 0 !== strpos($name, 'HTTP_')) {
$value = $_SERVER[$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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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