Skip to content

Commit 790dbad

Browse files
j92xabbuh
authored andcommitted
[Dotenv] Use default value when referenced variable is not set
1 parent 5bf7759 commit 790dbad

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

src/Symfony/Component/Dotenv/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
}

src/Symfony/Component/Dotenv/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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy