Skip to content

Commit 99187bb

Browse files
committed
env 方法支持获取 true/false/empty/null 值
1 parent 1a2ab0a commit 99187bb

File tree

2 files changed

+56
-4
lines changed

2 files changed

+56
-4
lines changed

src/helpers.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,8 @@ function mkdir_p($path, $mode = 0755)
191191
/**
192192
* Gets the value of an environment variable.
193193
*
194+
* @see https://github.com/laravel/framework/blob/master/src/Illuminate/Support/helpers.php env function
195+
*
194196
* @param string $key
195197
* @param mixed $default
196198
* @return mixed
@@ -199,7 +201,30 @@ function env($key, $default = null)
199201
{
200202
$value = getenv($key);
201203

202-
return $value === false ? $default : $value;
204+
if ($value === false) {
205+
return $default;
206+
}
207+
208+
switch (strtolower($value)) {
209+
case 'true':
210+
case '(true)':
211+
return true;
212+
case 'false':
213+
case '(false)':
214+
return false;
215+
case 'empty':
216+
case '(empty)':
217+
return '';
218+
case 'null':
219+
case '(null)':
220+
return;
221+
}
222+
223+
if (($valueLength = strlen($value)) > 1 && $value[0] === '"' && $value[$valueLength - 1] === '"') {
224+
return substr($value, 1, -1);
225+
}
226+
227+
return $value;
203228
}
204229
}
205230

tests/HelperTest.php

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,37 @@ public function testIsJson()
5858

5959
public function testEnv()
6060
{
61-
$this->assertEquals('localhost', env('SOLI_DATABASE_HOST', 'localhost'));
61+
$this->assertEquals(null, env('NOT_EXISTS_ENV_KEY'));
62+
$this->assertEquals('default value', env('NOT_EXISTS_ENV_KEY', 'default value'));
6263

63-
putenv("SOLI_DATABASE_HOST=192.168.56.102");
64-
$this->assertEquals('192.168.56.102', env('SOLI_DATABASE_HOST'));
64+
putenv('ENV_HELLO_WORLD=true');
65+
$this->assertEquals(true, env('ENV_HELLO_WORLD'));
66+
putenv('ENV_HELLO_WORLD=(true)');
67+
$this->assertEquals(true, env('ENV_HELLO_WORLD'));
68+
69+
putenv('ENV_HELLO_WORLD=false');
70+
$this->assertEquals(false, env('ENV_HELLO_WORLD'));
71+
putenv('ENV_HELLO_WORLD=(false)');
72+
$this->assertEquals(false, env('ENV_HELLO_WORLD'));
73+
74+
putenv('ENV_HELLO_WORLD=empty');
75+
$this->assertEquals('', env('ENV_HELLO_WORLD'));
76+
putenv('ENV_HELLO_WORLD=(empty)');
77+
$this->assertEquals('', env('ENV_HELLO_WORLD'));
78+
79+
putenv('ENV_HELLO_WORLD=null');
80+
$this->assertEquals(null, env('ENV_HELLO_WORLD'));
81+
putenv('ENV_HELLO_WORLD=(null)');
82+
$this->assertEquals(null, env('ENV_HELLO_WORLD'));
83+
84+
putenv('ENV_HELLO_WORLD="hello"');
85+
$this->assertEquals('hello', env('ENV_HELLO_WORLD'));
86+
87+
putenv('ENV_HELLO_WORLD=hello');
88+
$this->assertEquals('hello', env('ENV_HELLO_WORLD'));
89+
90+
putenv('ENV_HELLO_WORLD');
91+
$this->assertEquals(null, env('ENV_HELLO_WORLD'));
6592
}
6693

6794
public function testEnvFile()

0 commit comments

Comments
 (0)