From 790dbad149e9b23f3a179497d8b39419fa5fa40e Mon Sep 17 00:00:00 2001 From: Joost van Driel Date: Sun, 19 May 2019 18:34:03 +0200 Subject: [PATCH] [Dotenv] Use default value when referenced variable is not set --- src/Symfony/Component/Dotenv/Dotenv.php | 10 ++++++++++ src/Symfony/Component/Dotenv/Tests/DotenvTest.php | 7 +++++++ 2 files changed, 17 insertions(+) diff --git a/src/Symfony/Component/Dotenv/Dotenv.php b/src/Symfony/Component/Dotenv/Dotenv.php index 2f699ce860..79b6663de8 100644 --- a/src/Symfony/Component/Dotenv/Dotenv.php +++ b/src/Symfony/Component/Dotenv/Dotenv.php @@ -427,6 +427,7 @@ final class Dotenv (?!\() # no opening parenthesis (?P\{)? # optional brace (?P'.self::VARNAME_REGEX.')? # var name + (?P:-[^\}]++)? # optional default value (?P\})? # optional closing brace /x'; @@ -456,6 +457,15 @@ final class Dotenv $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'])) { $value .= '}'; } diff --git a/src/Symfony/Component/Dotenv/Tests/DotenvTest.php b/src/Symfony/Component/Dotenv/Tests/DotenvTest.php index 63039d5660..3b48817cf5 100644 --- a/src/Symfony/Component/Dotenv/Tests/DotenvTest.php +++ b/src/Symfony/Component/Dotenv/Tests/DotenvTest.php @@ -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=$(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=\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) { @@ -159,6 +162,10 @@ class DotenvTest extends TestCase ['BAR=$REMOTE', ['BAR' => 'remote']], ['BAR=$SERVERVAR', ['BAR' => 'servervar']], ['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) {