Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2070e30

Browse files
committed
feature #31546 [Dotenv] Use default value when referenced variable is not set (j92)
This PR was squashed before being merged into the 4.4 branch (closes #31546). Discussion ---------- [Dotenv] Use default value when referenced variable is not set | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | no | New feature? | yes <!-- please update src/**/CHANGELOG.md files --> | BC breaks? | no <!-- see https://symfony.com/bc --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tests pass? | yes <!-- please add some, will be required by reviewers --> | Fixed tickets | #... <!-- #-prefixed issue number(s), if any --> | License | MIT | Doc PR | symfony/symfony-docs#11956 <!-- required for new features --> In bash you have the option to define a default variable like this: ```bash FOO=${VARIABLE:-default} ``` When VARIABLE is not set ```bash FOO=${VARIABLE:-default} #FOO=default ``` When VARIABLE is set: ```bash VARIABLE=test FOO=${VARIABLE:-default} #FOO=test ``` If others find this also a good idea, I will write documentation and add the Doc PR. But first I would like some feedback to check if anyone agrees with this feature. Commits ------- 790dbad [Dotenv] Use default value when referenced variable is not set
2 parents 94a0719 + 790dbad commit 2070e30

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
@@ -432,6 +432,7 @@ private function resolveVariables(string $value)
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
435436
(?P<closing_brace>\})? # optional closing brace
436437
/x';
437438

@@ -461,6 +462,15 @@ private function resolveVariables(string $value)
461462
$value = (string) getenv($name);
462463
}
463464

465+
if ('' === $value && isset($matches['default_value'])) {
466+
$unsupportedChars = strpbrk($matches['default_value'], '\'"{$');
467+
if (false !== $unsupportedChars) {
468+
throw $this->createFormatException(sprintf('Unsupported character "%s" found in the default value of variable "$%s".', $unsupportedChars[0], $name));
469+
}
470+
471+
$value = substr($matches['default_value'], 2);
472+
}
473+
464474
if (!$matches['opening_brace'] && isset($matches['closing_brace'])) {
465475
$value .= '}';
466476
}

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)