diff --git a/src/Symfony/Component/Dotenv/Dotenv.php b/src/Symfony/Component/Dotenv/Dotenv.php index 845d8cefec..d9c7da5341 100644 --- a/src/Symfony/Component/Dotenv/Dotenv.php +++ b/src/Symfony/Component/Dotenv/Dotenv.php @@ -432,6 +432,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'; @@ -461,6 +462,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 beda74838d..7b5f188db8 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) {