1
|
|
|
<?php |
2
|
|
|
// +---------------------------------------------------------------------- |
|
|
|
|
3
|
|
|
// | ThinkPHP [ WE CAN DO IT JUST THINK ] |
4
|
|
|
// +---------------------------------------------------------------------- |
5
|
|
|
// | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved. |
6
|
|
|
// +---------------------------------------------------------------------- |
7
|
|
|
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) |
8
|
|
|
// +---------------------------------------------------------------------- |
9
|
|
|
// | Author: liu21st <[email protected]> |
10
|
|
|
// +---------------------------------------------------------------------- |
11
|
|
|
declare (strict_types = 1); |
12
|
|
|
|
13
|
|
|
namespace think; |
14
|
|
|
|
15
|
|
|
use ArrayAccess; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Env管理类 |
19
|
|
|
*/ |
|
|
|
|
20
|
|
|
class Env implements ArrayAccess |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* 环境变量数据 |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
protected $data = []; |
27
|
|
|
|
28
|
3 |
|
public function __construct() |
|
|
|
|
29
|
|
|
{ |
30
|
3 |
|
$this->data = $_ENV; |
31
|
3 |
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* 读取环境变量定义文件 |
35
|
|
|
* @access public |
36
|
|
|
* @param string $file 环境变量定义文件 |
|
|
|
|
37
|
|
|
* @return void |
38
|
|
|
*/ |
39
|
1 |
|
public function load(string $file): void |
40
|
|
|
{ |
41
|
1 |
|
$env = parse_ini_file($file, true) ?: []; |
42
|
1 |
|
$this->set($env); |
43
|
1 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* 获取环境变量值 |
47
|
|
|
* @access public |
48
|
|
|
* @param string $name 环境变量名 |
|
|
|
|
49
|
|
|
* @param mixed $default 默认值 |
|
|
|
|
50
|
|
|
* @return mixed |
51
|
|
|
*/ |
52
|
3 |
|
public function get(string $name = null, $default = null) |
53
|
|
|
{ |
54
|
3 |
|
if (is_null($name)) { |
55
|
1 |
|
return $this->data; |
56
|
|
|
} |
57
|
|
|
|
58
|
3 |
|
$name = strtoupper(str_replace('.', '_', $name)); |
59
|
|
|
|
60
|
3 |
|
if (isset($this->data[$name])) { |
61
|
2 |
|
return $this->data[$name]; |
62
|
|
|
} |
63
|
|
|
|
64
|
1 |
|
return $this->getEnv($name, $default); |
65
|
|
|
} |
66
|
|
|
|
67
|
1 |
|
protected function getEnv(string $name, $default = null) |
|
|
|
|
68
|
|
|
{ |
69
|
1 |
|
$result = getenv('PHP_' . $name); |
70
|
|
|
|
71
|
1 |
|
if (false === $result) { |
72
|
1 |
|
return $default; |
73
|
|
|
} |
74
|
|
|
|
75
|
1 |
|
if ('false' === $result) { |
76
|
1 |
|
$result = false; |
77
|
1 |
|
} elseif ('true' === $result) { |
78
|
1 |
|
$result = true; |
79
|
|
|
} |
80
|
|
|
|
81
|
1 |
|
if (!isset($this->data[$name])) { |
82
|
1 |
|
$this->data[$name] = $result; |
83
|
|
|
} |
84
|
|
|
|
85
|
1 |
|
return $result; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* 设置环境变量值 |
90
|
|
|
* @access public |
91
|
|
|
* @param string|array $env 环境变量 |
|
|
|
|
92
|
|
|
* @param mixed $value 值 |
|
|
|
|
93
|
|
|
* @return void |
94
|
|
|
*/ |
95
|
2 |
|
public function set($env, $value = null): void |
96
|
|
|
{ |
97
|
2 |
|
if (is_array($env)) { |
98
|
2 |
|
$env = array_change_key_case($env, CASE_UPPER); |
99
|
|
|
|
100
|
2 |
|
foreach ($env as $key => $val) { |
101
|
2 |
|
if (is_array($val)) { |
102
|
1 |
|
foreach ($val as $k => $v) { |
103
|
1 |
|
$this->data[$key . '_' . strtoupper($k)] = $v; |
104
|
|
|
} |
105
|
|
|
} else { |
106
|
2 |
|
$this->data[$key] = $val; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
} else { |
110
|
1 |
|
$name = strtoupper(str_replace('.', '_', $env)); |
111
|
|
|
|
112
|
1 |
|
$this->data[$name] = $value; |
113
|
|
|
} |
114
|
2 |
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* 检测是否存在环境变量 |
118
|
|
|
* @access public |
119
|
|
|
* @param string $name 参数名 |
|
|
|
|
120
|
|
|
* @return bool |
121
|
|
|
*/ |
122
|
|
|
public function has(string $name): bool |
123
|
|
|
{ |
124
|
|
|
return !is_null($this->get($name)); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* 设置环境变量 |
129
|
|
|
* @access public |
130
|
|
|
* @param string $name 参数名 |
|
|
|
|
131
|
|
|
* @param mixed $value 值 |
|
|
|
|
132
|
|
|
*/ |
|
|
|
|
133
|
1 |
|
public function __set(string $name, $value): void |
134
|
|
|
{ |
135
|
1 |
|
$this->set($name, $value); |
136
|
1 |
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* 获取环境变量 |
140
|
|
|
* @access public |
141
|
|
|
* @param string $name 参数名 |
|
|
|
|
142
|
|
|
* @return mixed |
143
|
|
|
*/ |
144
|
1 |
|
public function __get(string $name) |
145
|
|
|
{ |
146
|
1 |
|
return $this->get($name); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* 检测是否存在环境变量 |
151
|
|
|
* @access public |
152
|
|
|
* @param string $name 参数名 |
|
|
|
|
153
|
|
|
* @return bool |
154
|
|
|
*/ |
155
|
|
|
public function __isset(string $name): bool |
156
|
|
|
{ |
157
|
|
|
return $this->has($name); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
// ArrayAccess |
161
|
1 |
|
public function offsetSet($name, $value): void |
|
|
|
|
162
|
|
|
{ |
163
|
1 |
|
$this->set($name, $value); |
164
|
1 |
|
} |
165
|
|
|
|
166
|
|
|
public function offsetExists($name): bool |
|
|
|
|
167
|
|
|
{ |
168
|
|
|
return $this->__isset($name); |
169
|
|
|
} |
170
|
|
|
|
171
|
1 |
|
public function offsetUnset($name) |
|
|
|
|
172
|
|
|
{ |
173
|
1 |
|
throw new Exception('not support: unset'); |
174
|
|
|
} |
175
|
|
|
|
176
|
1 |
|
public function offsetGet($name) |
|
|
|
|
177
|
|
|
{ |
178
|
1 |
|
return $this->get($name); |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|