diff --git a/composer.json b/composer.json index 62948a7f2ac2..8b27f4d4660b 100644 --- a/composer.json +++ b/composer.json @@ -40,7 +40,7 @@ "symfony/routing": "^4.2", "symfony/var-dumper": "^4.2", "tijsverkoyen/css-to-inline-styles": "^2.2.1", - "vlucas/phpdotenv": "^3.0" + "vlucas/phpdotenv": "^3.3" }, "replace": { "illuminate/auth": "self.version", diff --git a/src/Illuminate/Foundation/Bootstrap/LoadEnvironmentVariables.php b/src/Illuminate/Foundation/Bootstrap/LoadEnvironmentVariables.php index 7297bda94e56..672a18f0a7c5 100644 --- a/src/Illuminate/Foundation/Bootstrap/LoadEnvironmentVariables.php +++ b/src/Illuminate/Foundation/Bootstrap/LoadEnvironmentVariables.php @@ -3,9 +3,11 @@ namespace Illuminate\Foundation\Bootstrap; use Dotenv\Dotenv; +use Dotenv\Environment\DotenvFactory; use Dotenv\Exception\InvalidFileException; use Symfony\Component\Console\Input\ArgvInput; use Illuminate\Contracts\Foundation\Application; +use Dotenv\Environment\Adapter\ServerConstAdapter; use Symfony\Component\Console\Output\ConsoleOutput; class LoadEnvironmentVariables @@ -25,7 +27,7 @@ public function bootstrap(Application $app) $this->checkForSpecificEnvironmentFile($app); try { - Dotenv::create($app->environmentPath(), $app->environmentFile())->safeLoad(); + $this->createDotenv($app)->safeLoad(); } catch (InvalidFileException $e) { $this->writeErrorAndDie($e); } @@ -74,6 +76,22 @@ protected function setEnvironmentFilePath($app, $file) return false; } + /** + * Create a Dotenv instance. + * + * @param \Illuminate\Contracts\Foundation\Application $app + * + * @return \Dotenv\Dotenv + */ + protected function createDotenv($app) + { + return Dotenv::create( + $app->environmentPath(), + $app->environmentFile(), + new DotenvFactory([new ServerConstAdapter]) + ); + } + /** * Write the error information to the screen and exit. * diff --git a/src/Illuminate/Support/helpers.php b/src/Illuminate/Support/helpers.php index 7bd92ec68cdb..3636ebfb1ee4 100755 --- a/src/Illuminate/Support/helpers.php +++ b/src/Illuminate/Support/helpers.php @@ -6,6 +6,7 @@ use Illuminate\Support\Collection; use Illuminate\Contracts\Support\Htmlable; use Illuminate\Support\HigherOrderTapProxy; +use Dotenv\Environment\Adapter\ServerConstAdapter; if (! function_exists('append_config')) { /** @@ -635,32 +636,29 @@ function ends_with($haystack, $needles) */ function env($key, $default = null) { - $value = getenv($key); - - if ($value === false) { - return value($default); - } - - switch (strtolower($value)) { - case 'true': - case '(true)': - return true; - case 'false': - case '(false)': - return false; - case 'empty': - case '(empty)': - return ''; - case 'null': - case '(null)': - return; - } - - if (($valueLength = strlen($value)) > 1 && $value[0] === '"' && $value[$valueLength - 1] === '"') { - return substr($value, 1, -1); - } + return (new ServerConstAdapter) + ->get($key) + ->map(function ($value) { + switch (strtolower($value)) { + case 'true': + case '(true)': + return true; + case 'false': + case '(false)': + return false; + case 'empty': + case '(empty)': + return ''; + case 'null': + case '(null)': + return; + } - return $value; + return $value; + }) + ->getOrCall(function () use ($default) { + return value($default); + }); } } diff --git a/tests/Support/SupportHelpersTest.php b/tests/Support/SupportHelpersTest.php index 719efd8d5835..ee5cdbb51bdb 100755 --- a/tests/Support/SupportHelpersTest.php +++ b/tests/Support/SupportHelpersTest.php @@ -920,53 +920,47 @@ public function testWith() public function testEnv() { - putenv('foo=bar'); - $this->assertEquals('bar', env('foo')); - } - - public function testEnvWithQuotes() - { - putenv('foo="bar"'); - $this->assertEquals('bar', env('foo')); + $_SERVER['foo'] = 'bar'; + $this->assertSame('bar', env('foo')); } public function testEnvTrue() { - putenv('foo=true'); + $_SERVER['foo'] = 'true'; $this->assertTrue(env('foo')); - putenv('foo=(true)'); + $_SERVER['foo'] = '(true)'; $this->assertTrue(env('foo')); } public function testEnvFalse() { - putenv('foo=false'); + $_SERVER['foo'] = 'false'; $this->assertFalse(env('foo')); - putenv('foo=(false)'); + $_SERVER['foo'] = '(false)'; $this->assertFalse(env('foo')); } public function testEnvEmpty() { - putenv('foo='); - $this->assertEquals('', env('foo')); + $_SERVER['foo'] = ''; + $this->assertSame('', env('foo')); - putenv('foo=empty'); - $this->assertEquals('', env('foo')); + $_SERVER['foo'] = 'empty'; + $this->assertSame('', env('foo')); - putenv('foo=(empty)'); - $this->assertEquals('', env('foo')); + $_SERVER['foo'] = '(empty)'; + $this->assertSame('', env('foo')); } public function testEnvNull() { - putenv('foo=null'); - $this->assertEquals('', env('foo')); + $_SERVER['foo'] = 'null'; + $this->assertNull(env('foo')); - putenv('foo=(null)'); - $this->assertEquals('', env('foo')); + $_SERVER['foo'] = '(null)'; + $this->assertNull(env('foo')); } }