[Dotenv] Use default value when referenced variable is not set
This commit is contained in:
parent
5bf7759f3d
commit
790dbad149
@ -427,6 +427,7 @@ final class Dotenv
|
|||||||
(?!\() # no opening parenthesis
|
(?!\() # no opening parenthesis
|
||||||
(?P<opening_brace>\{)? # optional brace
|
(?P<opening_brace>\{)? # optional brace
|
||||||
(?P<name>'.self::VARNAME_REGEX.')? # var name
|
(?P<name>'.self::VARNAME_REGEX.')? # var name
|
||||||
|
(?P<default_value>:-[^\}]++)? # optional default value
|
||||||
(?P<closing_brace>\})? # optional closing brace
|
(?P<closing_brace>\})? # optional closing brace
|
||||||
/x';
|
/x';
|
||||||
|
|
||||||
@ -456,6 +457,15 @@ final class Dotenv
|
|||||||
$value = (string) getenv($name);
|
$value = (string) getenv($name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ('' === $value && isset($matches['default_value'])) {
|
||||||
|
$unsupportedChars = strpbrk($matches['default_value'], '\'"{$');
|
||||||
|
if (false !== $unsupportedChars) {
|
||||||
|
throw $this->createFormatException(sprintf('Unsupported character "%s" found in the default value of variable "$%s".', $unsupportedChars[0], $name));
|
||||||
|
}
|
||||||
|
|
||||||
|
$value = substr($matches['default_value'], 2);
|
||||||
|
}
|
||||||
|
|
||||||
if (!$matches['opening_brace'] && isset($matches['closing_brace'])) {
|
if (!$matches['opening_brace'] && isset($matches['closing_brace'])) {
|
||||||
$value .= '}';
|
$value .= '}';
|
||||||
}
|
}
|
||||||
|
@ -48,6 +48,9 @@ class DotenvTest extends TestCase
|
|||||||
['FOO!', "Missing = in the environment variable declaration in \".env\" at line 1.\n...FOO!...\n ^ line 1 offset 3"],
|
['FOO!', "Missing = in the environment variable declaration in \".env\" at line 1.\n...FOO!...\n ^ line 1 offset 3"],
|
||||||
['FOO=$(echo foo', "Missing closing parenthesis. in \".env\" at line 1.\n...FOO=$(echo foo...\n ^ line 1 offset 14"],
|
['FOO=$(echo foo', "Missing closing parenthesis. in \".env\" at line 1.\n...FOO=$(echo foo...\n ^ line 1 offset 14"],
|
||||||
['FOO=$(echo foo'."\n", "Missing closing parenthesis. in \".env\" at line 1.\n...FOO=$(echo foo\\n...\n ^ line 1 offset 14"],
|
['FOO=$(echo foo'."\n", "Missing closing parenthesis. in \".env\" at line 1.\n...FOO=$(echo foo\\n...\n ^ line 1 offset 14"],
|
||||||
|
["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"],
|
||||||
|
["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"],
|
||||||
|
["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"],
|
||||||
];
|
];
|
||||||
|
|
||||||
if ('\\' !== \DIRECTORY_SEPARATOR) {
|
if ('\\' !== \DIRECTORY_SEPARATOR) {
|
||||||
@ -159,6 +162,10 @@ class DotenvTest extends TestCase
|
|||||||
['BAR=$REMOTE', ['BAR' => 'remote']],
|
['BAR=$REMOTE', ['BAR' => 'remote']],
|
||||||
['BAR=$SERVERVAR', ['BAR' => 'servervar']],
|
['BAR=$SERVERVAR', ['BAR' => 'servervar']],
|
||||||
['FOO=$NOTDEFINED', ['FOO' => '']],
|
['FOO=$NOTDEFINED', ['FOO' => '']],
|
||||||
|
["FOO=BAR\nBAR=\${FOO:-TEST}", ['FOO' => 'BAR', 'BAR' => 'BAR']],
|
||||||
|
["FOO=BAR\nBAR=\${NOTDEFINED:-TEST}", ['FOO' => 'BAR', 'BAR' => 'TEST']],
|
||||||
|
["FOO=\nBAR=\${FOO:-TEST}", ['FOO' => '', 'BAR' => 'TEST']],
|
||||||
|
["FOO=\nBAR=\$FOO:-TEST}", ['FOO' => '', 'BAR' => 'TEST}']],
|
||||||
];
|
];
|
||||||
|
|
||||||
if ('\\' !== \DIRECTORY_SEPARATOR) {
|
if ('\\' !== \DIRECTORY_SEPARATOR) {
|
||||||
|
Reference in New Issue
Block a user