Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.8] Only use $_SERVER for env variables #27462

Merged
merged 2 commits into from
Feb 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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':
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am tempted to also remove handling for empty, since you can already do FOO="" or just FOO=, and phpdotenv will treat it as the empty string.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that removing the "empty" will be an useless BC.

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 @@ -922,53 +922,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