Total Complexity | 46 |
Total Lines | 299 |
Duplicated Lines | 0 % |
Coverage | 24.24% |
Changes | 0 |
Complex classes like Config often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Config, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | class Config |
||
1 ignored issue
–
show
|
|||
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) |
|
2 ignored issues
–
show
|
|||
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 |
||
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) |
||
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 |
||
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': |
||
1 ignored issue
–
show
|
|||
147 | $config = include $file; |
||
148 | break; |
||
149 | case 'yaml': |
||
1 ignored issue
–
show
|
|||
150 | if (function_exists('yaml_parse_file')) { |
||
1 ignored issue
–
show
|
|||
151 | $config = yaml_parse_file($file); |
||
152 | } |
||
1 ignored issue
–
show
|
|||
153 | break; |
||
154 | case 'ini': |
||
1 ignored issue
–
show
|
|||
155 | $config = parse_ini_file($file, true) ?: []; |
||
156 | break; |
||
157 | case 'json': |
||
1 ignored issue
–
show
|
|||
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 |
||
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 |
||
293 | } |
||
294 | } |
||
295 | |||
296 | /** |
||
297 | * 获取配置参数 |
||
298 | * @access public |
||
299 | * @param string $name 参数名 |
||
300 | * @return mixed |
||
301 | */ |
||
302 | public function __get($name) |
||
305 | } |
||
306 | |||
307 | /** |
||
308 | * 检测是否存在参数 |
||
309 | * @access public |
||
310 | * @param string $name 参数名 |
||
311 | * @return bool |
||
312 | */ |
||
313 | public function __isset($name) |
||
319 |