Skip to content

Commit ea43247

Browse files
xabbuhnicolas-grekas
authored andcommitted
[Dotenv] support setting default env var values
1 parent 343a4bf commit ea43247

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

Dotenv.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ private function resolveVariables(string $value): string
432432
(?!\() # no opening parenthesis
433433
(?P<opening_brace>\{)? # optional brace
434434
(?P<name>'.self::VARNAME_REGEX.')? # var name
435-
(?P<default_value>:-[^\}]++)? # optional default value
435+
(?P<default_value>:[-=][^\}]++)? # optional default value
436436
(?P<closing_brace>\})? # optional closing brace
437437
/x';
438438

@@ -469,6 +469,10 @@ private function resolveVariables(string $value): string
469469
}
470470

471471
$value = substr($matches['default_value'], 2);
472+
473+
if ('=' === $matches['default_value'][1]) {
474+
$this->values[$name] = $value;
475+
}
472476
}
473477

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

Tests/DotenvTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,10 @@ public function getEnvData()
166166
["FOO=BAR\nBAR=\${NOTDEFINED:-TEST}", ['FOO' => 'BAR', 'BAR' => 'TEST']],
167167
["FOO=\nBAR=\${FOO:-TEST}", ['FOO' => '', 'BAR' => 'TEST']],
168168
["FOO=\nBAR=\$FOO:-TEST}", ['FOO' => '', 'BAR' => 'TEST}']],
169+
["FOO=BAR\nBAR=\${FOO:=TEST}", ['FOO' => 'BAR', 'BAR' => 'BAR']],
170+
["FOO=BAR\nBAR=\${NOTDEFINED:=TEST}", ['FOO' => 'BAR', 'NOTDEFINED' => 'TEST', 'BAR' => 'TEST']],
171+
["FOO=\nBAR=\${FOO:=TEST}", ['FOO' => 'TEST', 'BAR' => 'TEST']],
172+
["FOO=\nBAR=\$FOO:=TEST}", ['FOO' => 'TEST', 'BAR' => 'TEST}']],
169173
];
170174

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

0 commit comments

Comments
 (0)