|
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 Yaconf; |
|
|
|
|
|
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* 配置管理类 |
|
19
|
|
|
*/ |
|
|
|
|
|
|
20
|
|
|
class Config |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* 配置参数 |
|
24
|
|
|
* @var array |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $config = []; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* 配置文件目录 |
|
30
|
|
|
* @var string |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $path; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* 配置文件后缀 |
|
36
|
|
|
* @var string |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $ext; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* 是否支持Yaconf |
|
42
|
|
|
* @var bool|string |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $yaconf; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
|
|
|
|
|
47
|
|
|
* 构造方法 |
|
48
|
|
|
* @access public |
|
49
|
|
|
*/ |
|
50
|
9 |
|
public function __construct(string $path = null, string $ext = '.php') |
|
51
|
|
|
{ |
|
52
|
9 |
|
$this->path = $path ?: ''; |
|
53
|
9 |
|
$this->ext = $ext; |
|
54
|
9 |
|
$this->yaconf = class_exists('Yaconf'); |
|
55
|
9 |
|
} |
|
56
|
|
|
|
|
57
|
9 |
|
public static function __make(App $app) |
|
|
|
|
|
|
58
|
|
|
{ |
|
59
|
9 |
|
$path = $app->getConfigPath(); |
|
60
|
9 |
|
$ext = $app->getConfigExt(); |
|
61
|
|
|
|
|
62
|
9 |
|
return new static($path, $ext); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* 设置开启Yaconf 或者指定配置文件名 |
|
67
|
|
|
* @access public |
|
68
|
|
|
* @param bool|string $yaconf 是否使用Yaconf |
|
69
|
|
|
* @return void |
|
70
|
|
|
*/ |
|
71
|
|
|
public function setYaconf($yaconf): void |
|
72
|
|
|
{ |
|
73
|
|
|
$this->yaconf = $yaconf; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* 获取实际的yaconf配置参数名 |
|
78
|
|
|
* @access protected |
|
79
|
|
|
* @param string $name 配置参数名 |
|
80
|
|
|
* @return string |
|
81
|
|
|
*/ |
|
82
|
|
|
protected function getYaconfName(string $name) |
|
83
|
|
|
{ |
|
84
|
|
|
if ($this->yaconf && is_string($this->yaconf)) { |
|
85
|
|
|
return $this->yaconf . '.' . $name; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
return $name; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* 获取yaconf配置 |
|
93
|
|
|
* @access public |
|
94
|
|
|
* @param string $name 配置参数名 |
|
|
|
|
|
|
95
|
|
|
* @param mixed $default 默认值 |
|
|
|
|
|
|
96
|
|
|
* @return mixed |
|
97
|
|
|
*/ |
|
98
|
|
|
public function yaconf(string $name, $default = null) |
|
99
|
|
|
{ |
|
100
|
|
|
if ($this->yaconf) { |
|
101
|
|
|
$yaconfName = $this->getYaconfName($name); |
|
102
|
|
|
|
|
103
|
|
|
if (Yaconf::has($yaconfName)) { |
|
104
|
|
|
return Yaconf::get($yaconfName); |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
return $default; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* 加载配置文件(多种格式) |
|
113
|
|
|
* @access public |
|
114
|
|
|
* @param string $file 配置文件名 |
|
115
|
|
|
* @param string $name 一级配置名 |
|
116
|
|
|
* @return array |
|
117
|
|
|
*/ |
|
118
|
|
|
public function load(string $file, string $name = ''): array |
|
119
|
|
|
{ |
|
120
|
|
|
if (is_file($file)) { |
|
121
|
|
|
$filename = $file; |
|
122
|
|
|
} elseif (is_file($this->path . $file . $this->ext)) { |
|
123
|
|
|
$filename = $this->path . $file . $this->ext; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
if (isset($filename)) { |
|
127
|
|
|
return $this->parse($filename, $name); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
if ($this->yaconf && Yaconf::has($file)) { |
|
131
|
|
|
return $this->set(Yaconf::get($file), $name); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
return $this->config; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* 解析配置文件 |
|
139
|
|
|
* @access public |
|
140
|
|
|
* @param string $file 配置文件名 |
|
141
|
|
|
* @param string $name 一级配置名 |
|
142
|
|
|
* @return array |
|
143
|
|
|
*/ |
|
144
|
|
|
protected function parse(string $file, string $name): array |
|
145
|
|
|
{ |
|
146
|
|
|
$type = pathinfo($file, PATHINFO_EXTENSION); |
|
147
|
|
|
|
|
148
|
|
|
switch ($type) { |
|
149
|
|
|
case 'php': |
|
|
|
|
|
|
150
|
|
|
$config = include $file; |
|
151
|
|
|
break; |
|
152
|
|
|
case 'yaml': |
|
|
|
|
|
|
153
|
|
|
if (function_exists('yaml_parse_file')) { |
|
|
|
|
|
|
154
|
|
|
$config = yaml_parse_file($file); |
|
155
|
|
|
} |
|
|
|
|
|
|
156
|
|
|
break; |
|
157
|
|
|
case 'ini': |
|
|
|
|
|
|
158
|
|
|
$config = parse_ini_file($file, true) ?: []; |
|
159
|
|
|
break; |
|
160
|
|
|
case 'json': |
|
|
|
|
|
|
161
|
|
|
$config = json_decode(file_get_contents($file), true); |
|
162
|
|
|
break; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
return isset($config) && is_array($config) ? $this->set($config, strtolower($name)) : []; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* 检测配置是否存在 |
|
170
|
|
|
* @access public |
|
171
|
|
|
* @param string $name 配置参数名(支持多级配置 .号分割) |
|
172
|
|
|
* @return bool |
|
173
|
|
|
*/ |
|
174
|
|
|
public function has(string $name): bool |
|
175
|
|
|
{ |
|
176
|
|
|
return !is_null($this->get($name)); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* 获取一级配置 |
|
181
|
|
|
* @access protected |
|
182
|
|
|
* @param string $name 一级配置名 |
|
183
|
|
|
* @return array |
|
184
|
|
|
*/ |
|
185
|
8 |
|
protected function pull(string $name): array |
|
186
|
|
|
{ |
|
187
|
8 |
|
$name = strtolower($name); |
|
188
|
|
|
|
|
189
|
8 |
|
if ($this->yaconf) { |
|
190
|
|
|
$yaconfName = $this->getYaconfName($name); |
|
191
|
|
|
|
|
192
|
|
|
if (Yaconf::has($yaconfName)) { |
|
193
|
|
|
$config = Yaconf::get($yaconfName); |
|
194
|
|
|
return isset($this->config[$name]) ? array_merge($this->config[$name], $config) : $config; |
|
195
|
|
|
} |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
8 |
|
return $this->config[$name] ?? []; |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* 获取配置参数 为空则获取所有配置 |
|
203
|
|
|
* @access public |
|
204
|
|
|
* @param string $name 配置参数名(支持多级配置 .号分割) |
|
205
|
|
|
* @param mixed $default 默认值 |
|
206
|
|
|
* @return mixed |
|
207
|
|
|
*/ |
|
208
|
9 |
|
public function get(string $name = null, $default = null) |
|
209
|
|
|
{ |
|
210
|
|
|
// 无参数时获取所有 |
|
211
|
9 |
|
if (empty($name)) { |
|
212
|
|
|
return $this->config; |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
9 |
|
if (false === strpos($name, '.')) { |
|
216
|
8 |
|
return $this->pull($name); |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
7 |
|
if ($this->yaconf) { |
|
220
|
|
|
$yaconfName = $this->getYaconfName($name); |
|
221
|
|
|
|
|
222
|
|
|
if (Yaconf::has($yaconfName)) { |
|
223
|
|
|
return Yaconf::get($yaconfName); |
|
224
|
|
|
} |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
7 |
|
$name = explode('.', $name); |
|
228
|
7 |
|
$name[0] = strtolower($name[0]); |
|
229
|
7 |
|
$config = $this->config; |
|
230
|
|
|
|
|
231
|
|
|
// 按.拆分成多维数组进行判断 |
|
232
|
7 |
|
foreach ($name as $val) { |
|
233
|
7 |
|
if (isset($config[$val])) { |
|
234
|
|
|
$config = $config[$val]; |
|
235
|
|
|
} else { |
|
236
|
7 |
|
return $default; |
|
237
|
|
|
} |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
return $config; |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
/** |
|
244
|
|
|
* 设置配置参数 name为数组则为批量设置 |
|
245
|
|
|
* @access public |
|
246
|
|
|
* @param array $config 配置参数 |
|
247
|
|
|
* @param string $name 配置名 |
|
|
|
|
|
|
248
|
|
|
* @return array |
|
249
|
|
|
*/ |
|
250
|
|
|
public function set(array $config, string $name = null): array |
|
251
|
|
|
{ |
|
252
|
|
|
if (!empty($name)) { |
|
253
|
|
|
if (isset($this->config[$name])) { |
|
254
|
|
|
$result = array_merge($this->config[$name], $config); |
|
255
|
|
|
} else { |
|
256
|
|
|
$result = $config; |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
|
|
$this->config[$name] = $result; |
|
260
|
|
|
} else { |
|
261
|
|
|
$result = $this->config = array_merge($this->config, array_change_key_case($config)); |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
return $result; |
|
265
|
|
|
} |
|
266
|
|
|
|
|
267
|
|
|
/** |
|
268
|
|
|
* 移除配置 |
|
269
|
|
|
* @access public |
|
270
|
|
|
* @param string $name 配置参数名(支持三级配置 .号分割) |
|
271
|
|
|
* @return void |
|
272
|
|
|
*/ |
|
273
|
|
|
public function remove(string $name): void |
|
274
|
|
|
{ |
|
275
|
|
|
$name = explode('.', $name, 3); |
|
276
|
|
|
|
|
277
|
|
|
if (count($name) == 2) { |
|
278
|
|
|
unset($this->config[strtolower($name[0])][$name[1]]); |
|
279
|
|
|
} else { |
|
280
|
|
|
unset($this->config[strtolower($name[0])][$name[1]][$name[2]]); |
|
281
|
|
|
} |
|
282
|
|
|
} |
|
283
|
|
|
|
|
284
|
|
|
/** |
|
285
|
|
|
* 重置配置参数 |
|
286
|
|
|
* @access public |
|
287
|
|
|
* @param string $name 配置名 |
|
288
|
|
|
* @return void |
|
289
|
|
|
*/ |
|
290
|
|
|
public function reset(string $name = ''): void |
|
291
|
|
|
{ |
|
292
|
|
|
if ('' === $name) { |
|
293
|
|
|
$this->config = []; |
|
294
|
|
|
} else { |
|
295
|
|
|
$this->config[$name] = []; |
|
296
|
|
|
} |
|
297
|
|
|
} |
|
298
|
|
|
|
|
299
|
|
|
/** |
|
300
|
|
|
* 获取配置参数 |
|
301
|
|
|
* @access public |
|
302
|
|
|
* @param string $name 参数名 |
|
303
|
|
|
* @return mixed |
|
304
|
|
|
*/ |
|
305
|
|
|
public function __get($name) |
|
306
|
|
|
{ |
|
307
|
|
|
return $this->get($name); |
|
308
|
|
|
} |
|
309
|
|
|
|
|
310
|
|
|
/** |
|
311
|
|
|
* 检测是否存在参数 |
|
312
|
|
|
* @access public |
|
313
|
|
|
* @param string $name 参数名 |
|
314
|
|
|
* @return bool |
|
315
|
|
|
*/ |
|
316
|
|
|
public function __isset($name) |
|
317
|
|
|
{ |
|
318
|
|
|
return $this->has($name); |
|
319
|
|
|
} |
|
320
|
|
|
|
|
321
|
|
|
} |
|
322
|
|
|
|