Completed
Push — 6.0 ( 96b69f...c11ada )
by liu
02:52
created

Config::load()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 4.8437

Importance

Changes 0
Metric Value
cc 4
eloc 7
nc 6
nop 2
dl 0
loc 13
ccs 5
cts 8
cp 0.625
crap 4.8437
rs 10
c 0
b 0
f 0
1
<?php
2
// +----------------------------------------------------------------------
1 ignored issue
show
Coding Style introduced by
You must use "/**" style comments for a file comment
Loading history...
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
 */
5 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @package tag in class comment
Loading history...
Coding Style introduced by
Missing @author tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
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
    /**
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...
39
     * 构造方法
40
     * @access public
41
     */
42 11
    public function __construct(string $path = null, string $ext = '.php')
43
    {
44 11
        $this->path = $path ?: '';
45 11
        $this->ext  = $ext;
46 11
    }
47
48 9
    public static function __make(App $app)
2 ignored issues
show
Coding Style introduced by
Missing doc comment for function __make()
Loading history...
Coding Style introduced by
Method name "Config::__make" is invalid; only PHP magic methods should be prefixed with a double underscore
Loading history...
Coding Style introduced by
Public method name "Config::__make" must not be prefixed with an underscore
Loading history...
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 1
    public function load(string $file, string $name = ''): array
64
    {
65 1
        if (is_file($file)) {
66 1
            $filename = $file;
67
        } elseif (is_file($this->path . $file . $this->ext)) {
68
            $filename = $this->path . $file . $this->ext;
69
        }
70
71 1
        if (isset($filename)) {
72 1
            return $this->parse($filename, $name);
73
        }
74
75
        return $this->config;
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
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
91 1
                $config = include $file;
92 1
                break;
93
            case 'yaml':
1 ignored issue
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
94
                if (function_exists('yaml_parse_file')) {
1 ignored issue
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
95
                    $config = yaml_parse_file($file);
96
                }
1 ignored issue
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
97
                break;
98
            case 'ini':
1 ignored issue
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
99
                $config = parse_ini_file($file, true, INI_SCANNER_TYPED) ?: [];
100
                break;
101
            case 'json':
1 ignored issue
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
102
                $config = json_decode(file_get_contents($file), true);
103
                break;
104
        }
105
106 1
        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 1
    public function has(string $name): bool
116
    {
117 1
        return !is_null($this->get($name));
118
    }
119
120
    /**
121
     * 获取一级配置
122
     * @access protected
123
     * @param  string $name 一级配置名
124
     * @return array
125
     */
126 9
    protected function pull(string $name): array
127
    {
128 9
        $name = strtolower($name);
129
130 9
        return $this->config[$name] ?? [];
131
    }
132
133
    /**
134
     * 获取配置参数 为空则获取所有配置
135
     * @access public
136
     * @param  string $name    配置参数名(支持多级配置 .号分割)
137
     * @param  mixed  $default 默认值
138
     * @return mixed
139
     */
140 11
    public function get(string $name = null, $default = null)
141
    {
142
        // 无参数时获取所有
143 11
        if (empty($name)) {
144
            return $this->config;
145
        }
146
147 11
        if (false === strpos($name, '.')) {
148 9
            return $this->pull($name);
149
        }
150
151 10
        $name    = explode('.', $name);
152 10
        $name[0] = strtolower($name[0]);
153 10
        $config  = $this->config;
154
155
        // 按.拆分成多维数组进行判断
156 10
        foreach ($name as $val) {
157 10
            if (isset($config[$val])) {
158 1
                $config = $config[$val];
159
            } else {
160 9
                return $default;
161
            }
162
        }
163
164 1
        return $config;
165
    }
166
167
    /**
168
     * 设置配置参数 name为数组则为批量设置
169
     * @access public
170
     * @param  array  $config 配置参数
171
     * @param  string $name 配置名
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 1 found
Loading history...
172
     * @return array
173
     */
174 1
    public function set(array $config, string $name = null): array
175
    {
176 1
        if (!empty($name)) {
177 1
            if (isset($this->config[$name])) {
178
                $result = array_merge($this->config[$name], $config);
179
            } else {
180 1
                $result = $config;
181
            }
182
183 1
            $this->config[$name] = $result;
184
        } else {
185
            $result = $this->config = array_merge($this->config, array_change_key_case($config));
186
        }
187
188 1
        return $result;
189
    }
190
191
}
192