From ea391f32c8ac85103b11635cc8dc3b0252826b81 Mon Sep 17 00:00:00 2001 From: Guillermo Pages Date: Sun, 11 Sep 2022 17:46:48 +0200 Subject: [PATCH] feat: support quotes trailing spaces --- src/DotEnv.php | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/src/DotEnv.php b/src/DotEnv.php index a77068d..6dcfb14 100644 --- a/src/DotEnv.php +++ b/src/DotEnv.php @@ -21,19 +21,39 @@ class DotEnv { ? [$parts[0], implode('=', array_slice($parts, 1))] : $parts ); - $vars[$varname] = $value; + $trimmedValue = trim($value); + $vars[trim($varname)] = self::removeOuterQuotes($trimmedValue); return $vars; }, []); return $envvars; } - public static function MUTATE_SERVER_addEnvVars(array $variables) { - $_SERVER = array_merge($_SERVER, $variables); + public static function removeOuterQuotes(string $string, $quote = "'"): string { + $lastCharIndex = strlen($string) - 1; + $stringWithoutOuterQuotes = strpos($string, $quote) === 0 && strpos($string, $quote, -1) === $lastCharIndex + ? substr($string, 1, $lastCharIndex) + : $string; + + if ($quote === '"') { + return $stringWithoutOuterQuotes; + } + + $noQuotesRemoved = strlen($stringWithoutOuterQuotes) === strlen($string); + + if ($noQuotesRemoved && $quote === "'") { + return self::removeOuterQuotes($string, '"'); + } + + return $stringWithoutOuterQuotes; + } + + public static function MUTATE_ENV_addEnvVars(array $variables) { + $_ENV = array_merge($_ENV, $variables); } public static function load(string $rootDir) { $envvars = self::loadFromFile($rootDir . DIRECTORY_SEPARATOR . '.env'); - self::MUTATE_SERVER_addEnvVars($envvars); + self::MUTATE_ENV_addEnvVars($envvars); } } \ No newline at end of file