Skip to content

Commit 4bd1d9c

Browse files
authored
Merge pull request #27462 from laravel/env-changes
[5.8] Only use $_SERVER for env variables
2 parents 1261c59 + aebb1d8 commit 4bd1d9c

File tree

4 files changed

+59
-49
lines changed

4 files changed

+59
-49
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
"symfony/routing": "^4.2",
4141
"symfony/var-dumper": "^4.2",
4242
"tijsverkoyen/css-to-inline-styles": "^2.2.1",
43-
"vlucas/phpdotenv": "^3.0"
43+
"vlucas/phpdotenv": "^3.3"
4444
},
4545
"replace": {
4646
"illuminate/auth": "self.version",

src/Illuminate/Foundation/Bootstrap/LoadEnvironmentVariables.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
namespace Illuminate\Foundation\Bootstrap;
44

55
use Dotenv\Dotenv;
6+
use Dotenv\Environment\DotenvFactory;
67
use Dotenv\Exception\InvalidFileException;
78
use Symfony\Component\Console\Input\ArgvInput;
89
use Illuminate\Contracts\Foundation\Application;
10+
use Dotenv\Environment\Adapter\ServerConstAdapter;
911
use Symfony\Component\Console\Output\ConsoleOutput;
1012

1113
class LoadEnvironmentVariables
@@ -25,7 +27,7 @@ public function bootstrap(Application $app)
2527
$this->checkForSpecificEnvironmentFile($app);
2628

2729
try {
28-
Dotenv::create($app->environmentPath(), $app->environmentFile())->safeLoad();
30+
$this->createDotenv($app)->safeLoad();
2931
} catch (InvalidFileException $e) {
3032
$this->writeErrorAndDie($e);
3133
}
@@ -74,6 +76,22 @@ protected function setEnvironmentFilePath($app, $file)
7476
return false;
7577
}
7678

79+
/**
80+
* Create a Dotenv instance.
81+
*
82+
* @param \Illuminate\Contracts\Foundation\Application $app
83+
*
84+
* @return \Dotenv\Dotenv
85+
*/
86+
protected function createDotenv($app)
87+
{
88+
return Dotenv::create(
89+
$app->environmentPath(),
90+
$app->environmentFile(),
91+
new DotenvFactory([new ServerConstAdapter])
92+
);
93+
}
94+
7795
/**
7896
* Write the error information to the screen and exit.
7997
*

src/Illuminate/Support/helpers.php

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Illuminate\Support\Collection;
77
use Illuminate\Contracts\Support\Htmlable;
88
use Illuminate\Support\HigherOrderTapProxy;
9+
use Dotenv\Environment\Adapter\ServerConstAdapter;
910

1011
if (! function_exists('append_config')) {
1112
/**
@@ -635,32 +636,29 @@ function ends_with($haystack, $needles)
635636
*/
636637
function env($key, $default = null)
637638
{
638-
$value = getenv($key);
639-
640-
if ($value === false) {
641-
return value($default);
642-
}
643-
644-
switch (strtolower($value)) {
645-
case 'true':
646-
case '(true)':
647-
return true;
648-
case 'false':
649-
case '(false)':
650-
return false;
651-
case 'empty':
652-
case '(empty)':
653-
return '';
654-
case 'null':
655-
case '(null)':
656-
return;
657-
}
658-
659-
if (($valueLength = strlen($value)) > 1 && $value[0] === '"' && $value[$valueLength - 1] === '"') {
660-
return substr($value, 1, -1);
661-
}
639+
return (new ServerConstAdapter)
640+
->get($key)
641+
->map(function ($value) {
642+
switch (strtolower($value)) {
643+
case 'true':
644+
case '(true)':
645+
return true;
646+
case 'false':
647+
case '(false)':
648+
return false;
649+
case 'empty':
650+
case '(empty)':
651+
return '';
652+
case 'null':
653+
case '(null)':
654+
return;
655+
}
662656

663-
return $value;
657+
return $value;
658+
})
659+
->getOrCall(function () use ($default) {
660+
return value($default);
661+
});
664662
}
665663
}
666664

tests/Support/SupportHelpersTest.php

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -920,53 +920,47 @@ public function testWith()
920920

921921
public function testEnv()
922922
{
923-
putenv('foo=bar');
924-
$this->assertEquals('bar', env('foo'));
925-
}
926-
927-
public function testEnvWithQuotes()
928-
{
929-
putenv('foo="bar"');
930-
$this->assertEquals('bar', env('foo'));
923+
$_SERVER['foo'] = 'bar';
924+
$this->assertSame('bar', env('foo'));
931925
}
932926

933927
public function testEnvTrue()
934928
{
935-
putenv('foo=true');
929+
$_SERVER['foo'] = 'true';
936930
$this->assertTrue(env('foo'));
937931

938-
putenv('foo=(true)');
932+
$_SERVER['foo'] = '(true)';
939933
$this->assertTrue(env('foo'));
940934
}
941935

942936
public function testEnvFalse()
943937
{
944-
putenv('foo=false');
938+
$_SERVER['foo'] = 'false';
945939
$this->assertFalse(env('foo'));
946940

947-
putenv('foo=(false)');
941+
$_SERVER['foo'] = '(false)';
948942
$this->assertFalse(env('foo'));
949943
}
950944

951945
public function testEnvEmpty()
952946
{
953-
putenv('foo=');
954-
$this->assertEquals('', env('foo'));
947+
$_SERVER['foo'] = '';
948+
$this->assertSame('', env('foo'));
955949

956-
putenv('foo=empty');
957-
$this->assertEquals('', env('foo'));
950+
$_SERVER['foo'] = 'empty';
951+
$this->assertSame('', env('foo'));
958952

959-
putenv('foo=(empty)');
960-
$this->assertEquals('', env('foo'));
953+
$_SERVER['foo'] = '(empty)';
954+
$this->assertSame('', env('foo'));
961955
}
962956

963957
public function testEnvNull()
964958
{
965-
putenv('foo=null');
966-
$this->assertEquals('', env('foo'));
959+
$_SERVER['foo'] = 'null';
960+
$this->assertNull(env('foo'));
967961

968-
putenv('foo=(null)');
969-
$this->assertEquals('', env('foo'));
962+
$_SERVER['foo'] = '(null)';
963+
$this->assertNull(env('foo'));
970964
}
971965
}
972966

0 commit comments

Comments
 (0)