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 2f699ce860faa..79b6663de8ef0 100644 --- a/src/Symfony/Component/Dotenv/Dotenv.php +++ b/src/Symfony/Component/Dotenv/Dotenv.php @@ -427,6 +427,7 @@ private function resolveVariables($value) (?!\() # 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 @@ private function resolveVariables($value) $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 63039d5660286..3b48817cf5997 100644 --- a/src/Symfony/Component/Dotenv/Tests/DotenvTest.php +++ b/src/Symfony/Component/Dotenv/Tests/DotenvTest.php @@ -48,6 +48,9 @@ public function getEnvDataWithFormatErrors() ['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 @@ public function getEnvData() ['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) { pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy