| Total Complexity | 46 |
| Total Lines | 299 |
| Duplicated Lines | 0 % |
| Coverage | 24.49% |
| 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 |
||
| 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) |
|
|
2 ignored issues
–
show
|
|||
| 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 |
||
| 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 |
||
| 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': |
||
|
1 ignored issue
–
show
|
|||
| 150 | $config = include $file; |
||
| 151 | break; |
||
| 152 | case 'yaml': |
||
|
1 ignored issue
–
show
|
|||
| 153 | if (function_exists('yaml_parse_file')) { |
||
|
1 ignored issue
–
show
|
|||
| 154 | $config = yaml_parse_file($file); |
||
| 155 | } |
||
|
1 ignored issue
–
show
|
|||
| 156 | break; |
||
| 157 | case 'ini': |
||
|
1 ignored issue
–
show
|
|||
| 158 | $config = parse_ini_file($file, true) ?: []; |
||
| 159 | break; |
||
| 160 | case 'json': |
||
|
1 ignored issue
–
show
|
|||
| 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 |
||
| 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 |
||
| 296 | } |
||
| 297 | } |
||
| 298 | |||
| 299 | /** |
||
| 300 | * 获取配置参数 |
||
| 301 | * @access public |
||
| 302 | * @param string $name 参数名 |
||
| 303 | * @return mixed |
||
| 304 | */ |
||
| 305 | public function __get($name) |
||
| 308 | } |
||
| 309 | |||
| 310 | /** |
||
| 311 | * 检测是否存在参数 |
||
| 312 | * @access public |
||
| 313 | * @param string $name 参数名 |
||
| 314 | * @return bool |
||
| 315 | */ |
||
| 316 | public function __isset($name) |
||
| 319 | } |
||
| 320 | |||
| 322 |