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