Completed
Pull Request — 6.0 (#1983)
by guanguans
05:16
created

Config   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 178
Duplicated Lines 0 %

Test Coverage

Coverage 71.43%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 62
dl 0
loc 178
ccs 45
cts 63
cp 0.7143
rs 10
c 1
b 0
f 0
wmc 27

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __make() 0 6 1
A __construct() 0 4 2
A load() 0 13 4
B parse() 0 26 10
A set() 0 15 3
A get() 0 25 5
A pull() 0 5 1
A has() 0 3 1
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
0 ignored issues
show
Coding Style introduced by
Package name "think" is not valid; consider "Think" instead
Loading history...
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
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $path should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $ext should have a doc-comment as per coding-style.
Loading history...
40
     * 构造方法
41
     * @access public
42
     */
43 11
    public function __construct(string $path = null, string $ext = '.php')
44
    {
45 11
        $this->path = $path ?: '';
46 11
        $this->ext  = $ext;
47 11
    }
48
49 9
    public static function __make(App $app)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function __make()
Loading history...
50
    {
51 9
        $path = $app->getConfigPath();
52 9
        $ext  = $app->getConfigExt();
53
54 9
        return new static($path, $ext);
55
    }
56
57
    /**
58
     * 加载配置文件(多种格式)
59
     * @access public
60
     * @param  string $file 配置文件名
61
     * @param  string $name 一级配置名
62
     * @param  bool   $set 是否设置
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 1 found
Loading history...
63
     * @return array
64
     */
65 1
    public function load(string $file, string $name = '', bool $set = true): array
66
    {
67 1
        if (is_file($file)) {
68 1
            $filename = $file;
69
        } elseif (is_file($this->path . $file . $this->ext)) {
70
            $filename = $this->path . $file . $this->ext;
71
        }
72
73 1
        if (isset($filename)) {
74 1
            return $this->parse($filename, $name, $set);
75
        }
76
77
        return $this->config;
78
    }
79
80
    /**
81
     * 解析配置文件
82
     * @access public
83
     * @param  string $file 配置文件名
84
     * @param  string $name 一级配置名
85
     * @param  bool   $set 是否设置
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 1 found
Loading history...
86
     * @return array
87
     */
88 1
    protected function parse(string $file, string $name, bool $set = true): array
89
    {
90 1
        $type   = pathinfo($file, PATHINFO_EXTENSION);
91 1
        $config = [];
92 1
        switch ($type) {
93 1
            case 'php':
94 1
                $config = include $file;
95 1
                break;
96
            case 'yml':
97
            case 'yaml':
98
                if (function_exists('yaml_parse_file')) {
99
                    $config = yaml_parse_file($file);
100
                }
101
                break;
102
            case 'ini':
103
                $config = parse_ini_file($file, true, INI_SCANNER_TYPED) ?: [];
104
                break;
105
            case 'json':
106
                $config = json_decode(file_get_contents($file), true);
107
                break;
108
        }
109
110 1
        if ($set) {
111 1
            return is_array($config) ? $this->set($config, strtolower($name)) : [];
112
        } else {
113
            return $config;
114
        }
115
    }
116
117
    /**
118
     * 检测配置是否存在
119
     * @access public
120
     * @param  string $name 配置参数名(支持多级配置 .号分割)
121
     * @return bool
122
     */
123 1
    public function has(string $name): bool
124
    {
125 1
        return !is_null($this->get($name));
126
    }
127
128
    /**
129
     * 获取一级配置
130
     * @access protected
131
     * @param  string $name 一级配置名
132
     * @return array
133
     */
134 10
    protected function pull(string $name): array
135
    {
136 10
        $name = strtolower($name);
137
138 10
        return $this->config[$name] ?? [];
139
    }
140
141
    /**
142
     * 获取配置参数 为空则获取所有配置
143
     * @access public
144
     * @param  string $name    配置参数名(支持多级配置 .号分割)
145
     * @param  mixed  $default 默认值
146
     * @return mixed
147
     */
148 16
    public function get(string $name = null, $default = null)
149
    {
150
        // 无参数时获取所有
151 16
        if (empty($name)) {
152
            return $this->config;
153
        }
154
155 16
        if (false === strpos($name, '.')) {
156 10
            return $this->pull($name);
157
        }
158
159 15
        $name    = explode('.', $name);
160 15
        $name[0] = strtolower($name[0]);
161 15
        $config  = $this->config;
162
163
        // 按.拆分成多维数组进行判断
164 15
        foreach ($name as $val) {
165 15
            if (isset($config[$val])) {
166 2
                $config = $config[$val];
167
            } else {
168 14
                return $default;
169
            }
170
        }
171
172 2
        return $config;
173
    }
174
175
    /**
176
     * 设置配置参数 name为数组则为批量设置
177
     * @access public
178
     * @param  array  $config 配置参数
179
     * @param  string $name 配置名
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 1 found
Loading history...
180
     * @return array
181
     */
182 2
    public function set(array $config, string $name = null): array
183
    {
184 2
        if (!empty($name)) {
185 2
            if (isset($this->config[$name])) {
186
                $result = array_merge($this->config[$name], $config);
187
            } else {
188 2
                $result = $config;
189
            }
190
191 2
            $this->config[$name] = $result;
192
        } else {
193
            $result = $this->config = array_merge($this->config, array_change_key_case($config));
194
        }
195
196 2
        return $result;
197
    }
198
199
}
200