Completed
Push — 6.0 ( 538476...746f13 )
by liu
03:13
created

Config::set()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.1406

Importance

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