diff --git a/composer.json b/composer.json index 430ecf05..e1126ec5 100644 --- a/composer.json +++ b/composer.json @@ -11,7 +11,8 @@ } ], "require": { - "php": ">=5.3.9" + "php": ">=5.3.9", + "symfony/polyfill-ctype": "^1.9" }, "require-dev": { "phpunit/phpunit": "^4.8.35 || ^5.0" diff --git a/src/Loader.php b/src/Loader.php index 52c377fc..9e8183ba 100644 --- a/src/Loader.php +++ b/src/Loader.php @@ -2,7 +2,6 @@ namespace Dotenv; -use Dotenv\Exception\InvalidFileException; use Dotenv\Exception\InvalidPathException; /** @@ -235,42 +234,7 @@ protected function sanitiseVariableValue($name, $value) return array($name, $value); } - if ($this->beginsWithAQuote($value)) { // value starts with a quote - $quote = $value[0]; - $regexPattern = sprintf( - '/^ - %1$s # match a quote at the start of the value - ( # capturing sub-pattern used - (?: # we do not need to capture this - [^%1$s\\\\]* # any character other than a quote or backslash - |\\\\\\\\ # or two backslashes together - |\\\\%1$s # or an escaped quote e.g \" - )* # as many characters that match the previous rules - ) # end of the capturing sub-pattern - %1$s # and the closing quote - .*$ # and discard any string after the closing quote - /mx', - $quote - ); - $value = preg_replace($regexPattern, '$1', $value); - $value = str_replace("\\$quote", $quote, $value); - $value = str_replace('\\\\', '\\', $value); - } else { - $parts = explode(' #', $value, 2); - $value = trim($parts[0]); - - // Unquoted values cannot contain whitespace - if (preg_match('/\s+/', $value) > 0) { - // Check if value is a comment (usually triggered when empty value with comment) - if (preg_match('/^#/', $value) > 0) { - $value = ''; - } else { - throw new InvalidFileException('Dotenv values containing spaces must be surrounded by quotes.'); - } - } - } - - return array($name, trim($value)); + return array($name, Parser::parseValue($value)); } /** @@ -314,21 +278,7 @@ function ($matchedPatterns) use ($loader) { */ protected function sanitiseVariableName($name, $value) { - $name = trim(str_replace(array('export ', '\'', '"'), '', $name)); - - return array($name, $value); - } - - /** - * Determine if the given string begins with a quote. - * - * @param string $value - * - * @return bool - */ - protected function beginsWithAQuote($value) - { - return isset($value[0]) && ($value[0] === '"' || $value[0] === '\''); + return array(Parser::parseName($name), $value); } /** diff --git a/src/Parser.php b/src/Parser.php new file mode 100644 index 00000000..963e4d38 --- /dev/null +++ b/src/Parser.php @@ -0,0 +1,88 @@ +assertEmpty(getenv('QNULL')); $this->assertSame('pgsql:host=localhost;dbname=test', getenv('QEQUALS')); $this->assertSame('test some escaped characters like a quote (") or maybe a backslash (\\)', getenv('QESCAPED')); + $this->assertSame('iiiiviiiixiiiiviiii\\n', getenv('QSLASH1')); + $this->assertSame('iiiiviiiixiiiiviiii\\n', getenv('QSLASH2')); } public function testLargeDotenvLoadsEnvironmentVars() diff --git a/tests/fixtures/env/quoted.env b/tests/fixtures/env/quoted.env index 2f3ae904..cd8ab578 100644 --- a/tests/fixtures/env/quoted.env +++ b/tests/fixtures/env/quoted.env @@ -7,3 +7,5 @@ QNULL="" QWHITESPACE = "no space" QESCAPED="test some escaped characters like a quote (\") or maybe a backslash (\\)" +QSLASH1="iiiiviiiixiiiiviiii\n" +QSLASH2="iiiiviiiixiiiiviiii\\n"