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 | https://github.com/symfony/symfony-docs/pull/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
-------
790dbad149
[Dotenv] Use default value when referenced variable is not set
This commit is contained in:
commit
2070e30018
@ -432,6 +432,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';
|
||||||
|
|
||||||
@ -461,6 +462,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