|
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
|
|
|
class Env implements ArrayAccess |
|
|
|
|
|
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* 环境变量数据 |
|
21
|
|
|
* @var array |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $data = []; |
|
24
|
|
|
|
|
25
|
3 |
|
public function __construct() |
|
|
|
|
|
|
26
|
|
|
{ |
|
27
|
3 |
|
$this->data = $_ENV; |
|
28
|
3 |
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* 读取环境变量定义文件 |
|
32
|
|
|
* @access public |
|
33
|
|
|
* @param string $file 环境变量定义文件 |
|
|
|
|
|
|
34
|
|
|
* @return void |
|
35
|
|
|
*/ |
|
36
|
1 |
|
public function load(string $file): void |
|
37
|
|
|
{ |
|
38
|
1 |
|
$env = parse_ini_file($file, true) ?: []; |
|
39
|
1 |
|
$this->set($env); |
|
40
|
1 |
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* 获取环境变量值 |
|
44
|
|
|
* @access public |
|
45
|
|
|
* @param string $name 环境变量名 |
|
|
|
|
|
|
46
|
|
|
* @param mixed $default 默认值 |
|
|
|
|
|
|
47
|
|
|
* @return mixed |
|
48
|
|
|
*/ |
|
49
|
3 |
|
public function get(string $name = null, $default = null) |
|
50
|
|
|
{ |
|
51
|
3 |
|
if (is_null($name)) { |
|
52
|
1 |
|
return $this->data; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
3 |
|
$name = strtoupper(str_replace('.', '_', $name)); |
|
56
|
|
|
|
|
57
|
3 |
|
if (isset($this->data[$name])) { |
|
58
|
2 |
|
return $this->data[$name]; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
1 |
|
return $this->getEnv($name, $default); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
1 |
|
protected function getEnv(string $name, $default = null) |
|
|
|
|
|
|
65
|
|
|
{ |
|
66
|
1 |
|
$result = getenv('PHP_' . $name); |
|
67
|
|
|
|
|
68
|
1 |
|
if (false === $result) { |
|
69
|
1 |
|
return $default; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
1 |
|
if ('false' === $result) { |
|
73
|
1 |
|
$result = false; |
|
74
|
1 |
|
} elseif ('true' === $result) { |
|
75
|
1 |
|
$result = true; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
1 |
|
if (!isset($this->data[$name])) { |
|
79
|
1 |
|
$this->data[$name] = $result; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
1 |
|
return $result; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* 设置环境变量值 |
|
87
|
|
|
* @access public |
|
88
|
|
|
* @param string|array $env 环境变量 |
|
|
|
|
|
|
89
|
|
|
* @param mixed $value 值 |
|
|
|
|
|
|
90
|
|
|
* @return void |
|
91
|
|
|
*/ |
|
92
|
2 |
|
public function set($env, $value = null): void |
|
93
|
|
|
{ |
|
94
|
2 |
|
if (is_array($env)) { |
|
95
|
2 |
|
$env = array_change_key_case($env, CASE_UPPER); |
|
96
|
|
|
|
|
97
|
2 |
|
foreach ($env as $key => $val) { |
|
98
|
2 |
|
if (is_array($val)) { |
|
99
|
1 |
|
foreach ($val as $k => $v) { |
|
100
|
1 |
|
$this->data[$key . '_' . strtoupper($k)] = $v; |
|
101
|
|
|
} |
|
102
|
|
|
} else { |
|
103
|
2 |
|
$this->data[$key] = $val; |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
} else { |
|
107
|
1 |
|
$name = strtoupper(str_replace('.', '_', $env)); |
|
108
|
|
|
|
|
109
|
1 |
|
$this->data[$name] = $value; |
|
110
|
|
|
} |
|
111
|
2 |
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* 设置环境变量 |
|
115
|
|
|
* @access public |
|
116
|
|
|
* @param string $name 参数名 |
|
|
|
|
|
|
117
|
|
|
* @param mixed $value 值 |
|
|
|
|
|
|
118
|
|
|
*/ |
|
|
|
|
|
|
119
|
1 |
|
public function __set(string $name, $value): void |
|
120
|
|
|
{ |
|
121
|
1 |
|
$this->set($name, $value); |
|
122
|
1 |
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* 获取环境变量 |
|
126
|
|
|
* @access public |
|
127
|
|
|
* @param string $name 参数名 |
|
|
|
|
|
|
128
|
|
|
* @return mixed |
|
129
|
|
|
*/ |
|
130
|
1 |
|
public function __get(string $name) |
|
131
|
|
|
{ |
|
132
|
1 |
|
return $this->get($name); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* 检测是否存在环境变量 |
|
137
|
|
|
* @access public |
|
138
|
|
|
* @param string $name 参数名 |
|
|
|
|
|
|
139
|
|
|
* @return bool |
|
140
|
|
|
*/ |
|
141
|
|
|
public function __isset(string $name): bool |
|
142
|
|
|
{ |
|
143
|
|
|
return !is_null($this->get($name)); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
// ArrayAccess |
|
147
|
1 |
|
public function offsetSet($name, $value): void |
|
|
|
|
|
|
148
|
|
|
{ |
|
149
|
1 |
|
$this->set($name, $value); |
|
150
|
1 |
|
} |
|
151
|
|
|
|
|
152
|
|
|
public function offsetExists($name): bool |
|
|
|
|
|
|
153
|
|
|
{ |
|
154
|
|
|
return $this->__isset($name); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
1 |
|
public function offsetUnset($name) |
|
|
|
|
|
|
158
|
|
|
{ |
|
159
|
1 |
|
throw new Exception('not support: unset'); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
1 |
|
public function offsetGet($name) |
|
|
|
|
|
|
163
|
|
|
{ |
|
164
|
1 |
|
return $this->get($name); |
|
165
|
|
|
} |
|
166
|
|
|
} |
|
167
|
|
|
|