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
|
|
|
/** |
16
|
|
|
* 配置管理类 |
17
|
|
|
* @package think |
|
|
|
|
18
|
|
|
*/ |
19
|
|
|
class Config |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* 配置参数 |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
protected $config = []; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* 配置文件目录 |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
protected $path; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* 配置文件后缀 |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
protected $ext; |
38
|
|
|
|
39
|
|
|
/** |
|
|
|
|
40
|
|
|
* 构造方法 |
41
|
|
|
* @access public |
42
|
|
|
*/ |
43
|
4 |
|
public function __construct(string $path = null, string $ext = '.php') |
44
|
|
|
{ |
45
|
4 |
|
$this->path = $path ?: ''; |
46
|
4 |
|
$this->ext = $ext; |
47
|
4 |
|
} |
48
|
|
|
|
49
|
2 |
|
public static function __make(App $app) |
|
|
|
|
50
|
|
|
{ |
51
|
2 |
|
$path = $app->getConfigPath(); |
52
|
2 |
|
$ext = $app->getConfigExt(); |
53
|
|
|
|
54
|
2 |
|
return new static($path, $ext); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* 加载配置文件(多种格式) |
59
|
|
|
* @access public |
60
|
|
|
* @param string $file 配置文件名 |
61
|
|
|
* @param string $name 一级配置名 |
62
|
|
|
* @return array |
63
|
|
|
*/ |
64
|
1 |
|
public function load(string $file, string $name = ''): array |
65
|
|
|
{ |
66
|
1 |
|
if (is_file($file)) { |
67
|
1 |
|
$filename = $file; |
68
|
|
|
} elseif (is_file($this->path . $file . $this->ext)) { |
69
|
|
|
$filename = $this->path . $file . $this->ext; |
70
|
|
|
} |
71
|
|
|
|
72
|
1 |
|
if (isset($filename)) { |
73
|
1 |
|
return $this->parse($filename, $name); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return $this->config; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* 解析配置文件 |
81
|
|
|
* @access public |
82
|
|
|
* @param string $file 配置文件名 |
83
|
|
|
* @param string $name 一级配置名 |
84
|
|
|
* @return array |
85
|
|
|
*/ |
86
|
1 |
|
protected function parse(string $file, string $name): array |
87
|
|
|
{ |
88
|
1 |
|
$type = pathinfo($file, PATHINFO_EXTENSION); |
89
|
1 |
|
$config = []; |
90
|
1 |
|
switch ($type) { |
91
|
1 |
|
case 'php': |
92
|
1 |
|
$config = include $file; |
93
|
1 |
|
break; |
94
|
|
|
case 'yml': |
95
|
|
|
case 'yaml': |
96
|
|
|
if (function_exists('yaml_parse_file')) { |
97
|
|
|
$config = yaml_parse_file($file); |
98
|
|
|
} |
99
|
|
|
break; |
100
|
|
|
case 'ini': |
101
|
|
|
$config = parse_ini_file($file, true, INI_SCANNER_TYPED) ?: []; |
102
|
|
|
break; |
103
|
|
|
case 'json': |
104
|
|
|
$config = json_decode(file_get_contents($file), true); |
105
|
|
|
break; |
106
|
|
|
} |
107
|
|
|
|
108
|
1 |
|
return is_array($config) ? $this->set($config, strtolower($name)) : []; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* 检测配置是否存在 |
113
|
|
|
* @access public |
114
|
|
|
* @param string $name 配置参数名(支持多级配置 .号分割) |
115
|
|
|
* @return bool |
116
|
|
|
*/ |
117
|
1 |
|
public function has(string $name): bool |
118
|
|
|
{ |
119
|
1 |
|
return !is_null($this->get($name)); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* 获取一级配置 |
124
|
|
|
* @access protected |
125
|
|
|
* @param string $name 一级配置名 |
126
|
|
|
* @return array |
127
|
|
|
*/ |
128
|
9 |
|
protected function pull(string $name): array |
129
|
|
|
{ |
130
|
9 |
|
$name = strtolower($name); |
131
|
|
|
|
132
|
9 |
|
return $this->config[$name] ?? []; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* 获取配置参数 为空则获取所有配置 |
137
|
|
|
* @access public |
138
|
|
|
* @param string $name 配置参数名(支持多级配置 .号分割) |
139
|
|
|
* @param mixed $default 默认值 |
140
|
|
|
* @return mixed |
141
|
|
|
*/ |
142
|
18 |
|
public function get(string $name = null, $default = null) |
143
|
|
|
{ |
144
|
|
|
// 无参数时获取所有 |
145
|
18 |
|
if (empty($name)) { |
146
|
|
|
return $this->config; |
147
|
|
|
} |
148
|
|
|
|
149
|
18 |
|
if (false === strpos($name, '.')) { |
150
|
9 |
|
return $this->pull($name); |
151
|
|
|
} |
152
|
|
|
|
153
|
17 |
|
$name = explode('.', $name); |
154
|
17 |
|
$name[0] = strtolower($name[0]); |
155
|
17 |
|
$config = $this->config; |
156
|
|
|
|
157
|
|
|
// 按.拆分成多维数组进行判断 |
158
|
17 |
|
foreach ($name as $val) { |
159
|
17 |
|
if (isset($config[$val])) { |
160
|
2 |
|
$config = $config[$val]; |
161
|
|
|
} else { |
162
|
16 |
|
return $default; |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
166
|
2 |
|
return $config; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* 设置配置参数 name为数组则为批量设置 |
171
|
|
|
* @access public |
172
|
|
|
* @param array $config 配置参数 |
173
|
|
|
* @param string $name 配置名 |
|
|
|
|
174
|
|
|
* @return array |
175
|
|
|
*/ |
176
|
2 |
|
public function set(array $config, string $name = null): array |
177
|
|
|
{ |
178
|
2 |
|
if (!empty($name)) { |
179
|
2 |
|
if (isset($this->config[$name])) { |
180
|
|
|
$result = array_merge($this->config[$name], $config); |
181
|
|
|
} else { |
182
|
2 |
|
$result = $config; |
183
|
|
|
} |
184
|
|
|
|
185
|
2 |
|
$this->config[$name] = $result; |
186
|
|
|
} else { |
187
|
|
|
$result = $this->config = array_merge($this->config, array_change_key_case($config)); |
188
|
|
|
} |
189
|
|
|
|
190
|
2 |
|
return $result; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
} |
194
|
|
|
|