Skip to content

Commit 1a2ab0a

Browse files
committed
添加环境变量 env/env_file 相关函数
1 parent 7870b13 commit 1a2ab0a

File tree

3 files changed

+92
-0
lines changed

3 files changed

+92
-0
lines changed

README.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ Table of Contents
2222
* [is_json](#is_json)
2323
* [文件目录](#文件目录)
2424
* [mkdir_p](#mkdir_p)
25+
* [环境变量](#环境变量)
26+
* [env](#env)
27+
* [env_file](#env_file)
2528

2629
## 字符串
2730

@@ -90,3 +93,44 @@ Table of Contents
9093

9194
mkdir_p('/path/a/b/c');
9295
mkdir_p('/path/a/b/c', 0777);
96+
97+
## 环境变量
98+
99+
### env
100+
101+
`env` 获取环境变量,允许指定默认值:
102+
103+
// 当没有 MYSQL_HOST 这个环境变量时,返回默认的 localhost
104+
env('MYSQL_HOST', 'localhost');
105+
106+
### env_file
107+
108+
`env_file` 获取环境配置文件名,默认为 `.env`,如果定义了 `APP_ENV`
109+
环境变量,则返回对应的环境文件名。
110+
111+
如,创建 test.php,文件内容为:
112+
113+
<?php
114+
include __DIR__ . "/src/helpers.php";
115+
echo env_file();
116+
117+
默认执行 `php test.php`,将输出 `.env`
118+
119+
php test.php
120+
// 输出
121+
.env
122+
123+
如果执行 `APP_ENV=prod php test.php`,从命令行指定环境变量 `APP_ENV=prod` 将输出 `.env.prod`
124+
125+
APP_ENV=prod php test.php
126+
// 输出
127+
.env.prod
128+
129+
可配合 [phpdotenv] 加载对应环境配置文件的内容,假如环境配置文件放在项目根目录
130+
BASE_PATH 下:
131+
132+
(new Dotenv(BASE_PATH, env_file()))->load();
133+
134+
加载后便可以使用 `env` 方法获取每一个环境变量的值,便于分离环境配置和项目代码。
135+
136+
[phpdotenv]: https://github.com/vlucas/phpdotenv

src/helpers.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,3 +186,35 @@ function mkdir_p($path, $mode = 0755)
186186
return (is_dir($path) or mkdir($path, $mode, true));
187187
}
188188
}
189+
190+
if (!function_exists('env')) { // @codeCoverageIgnore
191+
/**
192+
* Gets the value of an environment variable.
193+
*
194+
* @param string $key
195+
* @param mixed $default
196+
* @return mixed
197+
*/
198+
function env($key, $default = null)
199+
{
200+
$value = getenv($key);
201+
202+
return $value === false ? $default : $value;
203+
}
204+
}
205+
206+
if (!function_exists('env_file')) { // @codeCoverageIgnore
207+
/**
208+
* Get environment file.
209+
*
210+
* @param string $envFile
211+
* @return string
212+
*/
213+
function env_file($envFile = '.env')
214+
{
215+
if (getenv('APP_ENV')) {
216+
return $envFile . '.' . getenv('APP_ENV');
217+
}
218+
return $envFile;
219+
}
220+
}

tests/HelperTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,20 @@ public function testIsJson()
5555
$this->assertFalse(is_json('{data:123}'));
5656
$this->assertFalse(is_json(null));
5757
}
58+
59+
public function testEnv()
60+
{
61+
$this->assertEquals('localhost', env('SOLI_DATABASE_HOST', 'localhost'));
62+
63+
putenv("SOLI_DATABASE_HOST=192.168.56.102");
64+
$this->assertEquals('192.168.56.102', env('SOLI_DATABASE_HOST'));
65+
}
66+
67+
public function testEnvFile()
68+
{
69+
$this->assertEquals('.env', env_file());
70+
71+
putenv("APP_ENV=prod");
72+
$this->assertEquals('.env.prod', env_file());
73+
}
5874
}

0 commit comments

Comments
 (0)