| Total Complexity | 28 |
| Total Lines | 193 |
| Duplicated Lines | 0 % |
| Coverage | 33.87% |
| Changes | 0 | ||
| 1 | <?php |
||
| 18 | class Config |
||
| 19 | { |
||
| 20 | /** |
||
| 21 | * 配置参数 |
||
| 22 | * @var array |
||
| 23 | */ |
||
| 24 | protected $config = []; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * 配置文件目录 |
||
| 28 | * @var string |
||
| 29 | */ |
||
| 30 | protected $path; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * 配置文件后缀 |
||
| 34 | * @var string |
||
| 35 | */ |
||
| 36 | protected $ext; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * 构造方法 |
||
| 40 | * @access public |
||
| 41 | */ |
||
| 42 | 9 | public function __construct(string $path = null, string $ext = '.php') |
|
| 43 | { |
||
| 44 | 9 | $this->path = $path ?: ''; |
|
| 45 | 9 | $this->ext = $ext; |
|
| 46 | 9 | } |
|
| 47 | |||
| 48 | 9 | public static function __make(App $app) |
|
|
2 ignored issues
–
show
|
|||
| 49 | { |
||
| 50 | 9 | $path = $app->getConfigPath(); |
|
| 51 | 9 | $ext = $app->getConfigExt(); |
|
| 52 | |||
| 53 | 9 | return new static($path, $ext); |
|
| 54 | } |
||
| 55 | |||
| 56 | /** |
||
| 57 | * 加载配置文件(多种格式) |
||
| 58 | * @access public |
||
| 59 | * @param string $file 配置文件名 |
||
| 60 | * @param string $name 一级配置名 |
||
| 61 | * @return array |
||
| 62 | */ |
||
| 63 | public function load(string $file, string $name = ''): array |
||
| 76 | } |
||
| 77 | |||
| 78 | /** |
||
| 79 | * 解析配置文件 |
||
| 80 | * @access public |
||
| 81 | * @param string $file 配置文件名 |
||
| 82 | * @param string $name 一级配置名 |
||
| 83 | * @return array |
||
| 84 | */ |
||
| 85 | protected function parse(string $file, string $name): array |
||
| 86 | { |
||
| 87 | $type = pathinfo($file, PATHINFO_EXTENSION); |
||
| 88 | |||
| 89 | switch ($type) { |
||
| 90 | case 'php': |
||
|
1 ignored issue
–
show
|
|||
| 91 | $config = include $file; |
||
| 92 | break; |
||
| 93 | case 'yaml': |
||
|
1 ignored issue
–
show
|
|||
| 94 | if (function_exists('yaml_parse_file')) { |
||
|
1 ignored issue
–
show
|
|||
| 95 | $config = yaml_parse_file($file); |
||
| 96 | } |
||
|
1 ignored issue
–
show
|
|||
| 97 | break; |
||
| 98 | case 'ini': |
||
|
1 ignored issue
–
show
|
|||
| 99 | $config = parse_ini_file($file, true, INI_SCANNER_TYPED) ?: []; |
||
| 100 | break; |
||
| 101 | case 'json': |
||
|
1 ignored issue
–
show
|
|||
| 102 | $config = json_decode(file_get_contents($file), true); |
||
| 103 | break; |
||
| 104 | } |
||
| 105 | |||
| 106 | return isset($config) && is_array($config) ? $this->set($config, strtolower($name)) : []; |
||
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * 检测配置是否存在 |
||
| 111 | * @access public |
||
| 112 | * @param string $name 配置参数名(支持多级配置 .号分割) |
||
| 113 | * @return bool |
||
| 114 | */ |
||
| 115 | public function has(string $name): bool |
||
| 116 | { |
||
| 117 | return !is_null($this->get($name)); |
||
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * 获取一级配置 |
||
| 122 | * @access protected |
||
| 123 | * @param string $name 一级配置名 |
||
| 124 | * @return array |
||
| 125 | */ |
||
| 126 | 8 | protected function pull(string $name): array |
|
| 127 | { |
||
| 128 | 8 | $name = strtolower($name); |
|
| 129 | |||
| 130 | 8 | return $this->config[$name] ?? []; |
|
| 131 | } |
||
| 132 | |||
| 133 | /** |
||
| 134 | * 获取配置参数 为空则获取所有配置 |
||
| 135 | * @access public |
||
| 136 | * @param string $name 配置参数名(支持多级配置 .号分割) |
||
| 137 | * @param mixed $default 默认值 |
||
| 138 | * @return mixed |
||
| 139 | */ |
||
| 140 | 9 | public function get(string $name = null, $default = null) |
|
| 141 | { |
||
| 142 | // 无参数时获取所有 |
||
| 143 | 9 | if (empty($name)) { |
|
| 144 | return $this->config; |
||
| 145 | } |
||
| 146 | |||
| 147 | 9 | if (false === strpos($name, '.')) { |
|
| 148 | 8 | return $this->pull($name); |
|
| 149 | } |
||
| 150 | |||
| 151 | 8 | $name = explode('.', $name); |
|
| 152 | 8 | $name[0] = strtolower($name[0]); |
|
| 153 | 8 | $config = $this->config; |
|
| 154 | |||
| 155 | // 按.拆分成多维数组进行判断 |
||
| 156 | 8 | foreach ($name as $val) { |
|
| 157 | 8 | if (isset($config[$val])) { |
|
| 158 | $config = $config[$val]; |
||
| 159 | } else { |
||
| 160 | 8 | return $default; |
|
| 161 | } |
||
| 162 | } |
||
| 163 | |||
| 164 | return $config; |
||
| 165 | } |
||
| 166 | |||
| 167 | /** |
||
| 168 | * 设置配置参数 name为数组则为批量设置 |
||
| 169 | * @access public |
||
| 170 | * @param array $config 配置参数 |
||
| 171 | * @param string $name 配置名 |
||
| 172 | * @return array |
||
| 173 | */ |
||
| 174 | public function set(array $config, string $name = null): array |
||
| 189 | } |
||
| 190 | |||
| 191 | /** |
||
| 192 | * 获取配置参数 |
||
| 193 | * @access public |
||
| 194 | * @param string $name 参数名 |
||
| 195 | * @return mixed |
||
| 196 | */ |
||
| 197 | public function __get($name) |
||
| 200 | } |
||
| 201 | |||
| 202 | /** |
||
| 203 | * 检测是否存在参数 |
||
| 204 | * @access public |
||
| 205 | * @param string $name 参数名 |
||
| 206 | * @return bool |
||
| 207 | */ |
||
| 208 | public function __isset($name) |
||
| 211 | } |
||
| 212 | |||
| 213 | } |
||
| 214 |