Skip to content

Commit 4406aae

Browse files
j92xabbuh
authored andcommitted
[Dotenv] Use default value when referenced variable is not set
1 parent c53195d commit 4406aae

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

Dotenv.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,7 @@ private function resolveVariables($value)
427427
(?!\() # no opening parenthesis
428428
(?P<opening_brace>\{)? # optional brace
429429
(?P<name>'.self::VARNAME_REGEX.')? # var name
430+
(?P<default_value>:-[^\}]++)? # optional default value
430431
(?P<closing_brace>\})? # optional closing brace
431432
/x';
432433

@@ -456,6 +457,15 @@ private function resolveVariables($value)
456457
$value = (string) getenv($name);
457458
}
458459

460+
if ('' === $value && isset($matches['default_value'])) {
461+
$unsupportedChars = strpbrk($matches['default_value'], '\'"{$');
462+
if (false !== $unsupportedChars) {
463+
throw $this->createFormatException(sprintf('Unsupported character "%s" found in the default value of variable "$%s".', $unsupportedChars[0], $name));
464+
}
465+
466+
$value = substr($matches['default_value'], 2);
467+
}
468+
459469
if (!$matches['opening_brace'] && isset($matches['closing_brace'])) {
460470
$value .= '}';
461471
}

Tests/DotenvTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ public function getEnvDataWithFormatErrors()
4848
['FOO!', "Missing = in the environment variable declaration in \".env\" at line 1.\n...FOO!...\n ^ line 1 offset 3"],
4949
['FOO=$(echo foo', "Missing closing parenthesis. in \".env\" at line 1.\n...FOO=$(echo foo...\n ^ line 1 offset 14"],
5050
['FOO=$(echo foo'."\n", "Missing closing parenthesis. in \".env\" at line 1.\n...FOO=$(echo foo\\n...\n ^ line 1 offset 14"],
51+
["FOO=\nBAR=\${FOO:-\'a{a}a}", "Unsupported character \"'\" found in the default value of variable \"\$FOO\". in \".env\" at line 2.\n...\\nBAR=\${FOO:-\'a{a}a}...\n ^ line 2 offset 24"],
52+
["FOO=\nBAR=\${FOO:-a\$a}", "Unsupported character \"\$\" found in the default value of variable \"\$FOO\". in \".env\" at line 2.\n...FOO=\\nBAR=\${FOO:-a\$a}...\n ^ line 2 offset 20"],
53+
["FOO=\nBAR=\${FOO:-a\"a}", "Unclosed braces on variable expansion in \".env\" at line 2.\n...FOO=\\nBAR=\${FOO:-a\"a}...\n ^ line 2 offset 17"],
5154
];
5255

5356
if ('\\' !== \DIRECTORY_SEPARATOR) {
@@ -159,6 +162,10 @@ public function getEnvData()
159162
['BAR=$REMOTE', ['BAR' => 'remote']],
160163
['BAR=$SERVERVAR', ['BAR' => 'servervar']],
161164
['FOO=$NOTDEFINED', ['FOO' => '']],
165+
["FOO=BAR\nBAR=\${FOO:-TEST}", ['FOO' => 'BAR', 'BAR' => 'BAR']],
166+
["FOO=BAR\nBAR=\${NOTDEFINED:-TEST}", ['FOO' => 'BAR', 'BAR' => 'TEST']],
167+
["FOO=\nBAR=\${FOO:-TEST}", ['FOO' => '', 'BAR' => 'TEST']],
168+
["FOO=\nBAR=\$FOO:-TEST}", ['FOO' => '', 'BAR' => 'TEST}']],
162169
];
163170

164171
if ('\\' !== \DIRECTORY_SEPARATOR) {

0 commit comments

Comments
 (0)