Skip to content

Commit

Permalink
Merge pull request #27462 from laravel/env-changes
Browse files Browse the repository at this point in the history
[5.8] Only use $_SERVER for env variables
  • Loading branch information
taylorotwell authored Feb 11, 2019
2 parents 1261c59 + aebb1d8 commit 4bd1d9c
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 49 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
20 changes: 19 additions & 1 deletion src/Illuminate/Foundation/Bootstrap/LoadEnvironmentVariables.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
Expand Down Expand Up @@ -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.
*
Expand Down
48 changes: 23 additions & 25 deletions src/Illuminate/Support/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')) {
/**
Expand Down Expand Up @@ -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);
});
}
}

Expand Down
38 changes: 16 additions & 22 deletions tests/Support/SupportHelpersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}
}

Expand Down

0 comments on commit 4bd1d9c

Please sign in to comment.