| Total Complexity | 27 |
| Total Lines | 172 |
| Duplicated Lines | 0 % |
| Coverage | 71.67% |
| 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 | 11 | public function __construct(string $path = null, string $ext = '.php') |
|
| 46 | 11 | } |
|
| 47 | |||
| 48 | 9 | public static function __make(App $app) |
|
| 54 | } |
||
| 55 | |||
| 56 | /** |
||
| 57 | * 加载配置文件(多种格式) |
||
| 58 | * @access public |
||
| 59 | * @param string $file 配置文件名 |
||
| 60 | * @param string $name 一级配置名 |
||
| 61 | * @return array |
||
| 62 | */ |
||
| 63 | 1 | 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 | 1 | protected function parse(string $file, string $name): array |
|
| 86 | { |
||
| 87 | 1 | $type = pathinfo($file, PATHINFO_EXTENSION); |
|
| 88 | |||
| 89 | 1 | switch ($type) { |
|
| 90 | 1 | case 'php': |
|
|
1 ignored issue
–
show
|
|||
| 91 | 1 | $config = include $file; |
|
| 92 | 1 | break; |
|
| 93 | case 'yml': |
||
|
1 ignored issue
–
show
|
|||
| 94 | case 'yaml': |
||
|
1 ignored issue
–
show
|
|||
| 95 | if (function_exists('yaml_parse_file')) { |
||
|
1 ignored issue
–
show
|
|||
| 96 | $config = yaml_parse_file($file); |
||
| 97 | } |
||
|
1 ignored issue
–
show
|
|||
| 98 | break; |
||
| 99 | case 'ini': |
||
|
1 ignored issue
–
show
|
|||
| 100 | $config = parse_ini_file($file, true, INI_SCANNER_TYPED) ?: []; |
||
| 101 | break; |
||
| 102 | case 'json': |
||
|
1 ignored issue
–
show
|
|||
| 103 | $config = json_decode(file_get_contents($file), true); |
||
| 104 | break; |
||
| 105 | } |
||
| 106 | |||
| 107 | 1 | return isset($config) && is_array($config) ? $this->set($config, strtolower($name)) : []; |
|
| 108 | } |
||
| 109 | |||
| 110 | /** |
||
| 111 | * 检测配置是否存在 |
||
| 112 | * @access public |
||
| 113 | * @param string $name 配置参数名(支持多级配置 .号分割) |
||
| 114 | * @return bool |
||
| 115 | */ |
||
| 116 | 1 | public function has(string $name): bool |
|
| 117 | { |
||
| 118 | 1 | return !is_null($this->get($name)); |
|
| 119 | } |
||
| 120 | |||
| 121 | /** |
||
| 122 | * 获取一级配置 |
||
| 123 | * @access protected |
||
| 124 | * @param string $name 一级配置名 |
||
| 125 | * @return array |
||
| 126 | */ |
||
| 127 | 10 | protected function pull(string $name): array |
|
| 128 | { |
||
| 129 | 10 | $name = strtolower($name); |
|
| 130 | |||
| 131 | 10 | return $this->config[$name] ?? []; |
|
| 132 | } |
||
| 133 | |||
| 134 | /** |
||
| 135 | * 获取配置参数 为空则获取所有配置 |
||
| 136 | * @access public |
||
| 137 | * @param string $name 配置参数名(支持多级配置 .号分割) |
||
| 138 | * @param mixed $default 默认值 |
||
| 139 | * @return mixed |
||
| 140 | */ |
||
| 141 | 11 | public function get(string $name = null, $default = null) |
|
| 142 | { |
||
| 143 | // 无参数时获取所有 |
||
| 144 | 11 | if (empty($name)) { |
|
| 145 | return $this->config; |
||
| 146 | } |
||
| 147 | |||
| 148 | 11 | if (false === strpos($name, '.')) { |
|
| 149 | 10 | return $this->pull($name); |
|
| 150 | } |
||
| 151 | |||
| 152 | 10 | $name = explode('.', $name); |
|
| 153 | 10 | $name[0] = strtolower($name[0]); |
|
| 154 | 10 | $config = $this->config; |
|
| 155 | |||
| 156 | // 按.拆分成多维数组进行判断 |
||
| 157 | 10 | foreach ($name as $val) { |
|
| 158 | 10 | if (isset($config[$val])) { |
|
| 159 | 2 | $config = $config[$val]; |
|
| 160 | } else { |
||
| 161 | 9 | return $default; |
|
| 162 | } |
||
| 163 | } |
||
| 164 | |||
| 165 | 2 | return $config; |
|
| 166 | } |
||
| 167 | |||
| 168 | /** |
||
| 169 | * 设置配置参数 name为数组则为批量设置 |
||
| 170 | * @access public |
||
| 171 | * @param array $config 配置参数 |
||
| 172 | * @param string $name 配置名 |
||
| 173 | * @return array |
||
| 174 | */ |
||
| 175 | 2 | public function set(array $config, string $name = null): array |
|
| 190 | } |
||
| 191 | |||
| 192 | } |
||
| 193 |