Passed
Push — 8.0 ( 245181...10c471 )
by liu
02:39
created

Config::pull()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
// +----------------------------------------------------------------------
3
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
4
// +----------------------------------------------------------------------
5
// | Copyright (c) 2006~2023 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
use Closure;
16
17
/**
18
 * 配置管理类
19
 * @package think
20
 */
21
class Config
22
{
23
    /**
24
     * 配置参数
25
     * @var array
26
     */
27
    protected $config = [];
28
29
    /**
30
     * 注册配置获取器
31
     * @var Closure
32
     */
33
    protected $hook;
34
35
    /**
36
     * 构造方法
37
     * @access public
38
     */
39 42
    public function __construct(protected string $path = '', protected string $ext = '.php')
40
    {
41 42
    }
42
43 36
    public static function __make(App $app)
44
    {
45 36
        $path = $app->getConfigPath();
46 36
        $ext  = $app->getConfigExt();
47
48 36
        return new static($path, $ext);
49
    }
50
51
    /**
52
     * 加载配置文件(多种格式)
53
     * @access public
54
     * @param  string $file 配置文件名
55
     * @param  string $name 一级配置名
56
     * @return array
57
     */
58 3
    public function load(string $file, string $name = ''): array
59
    {
60 3
        if (is_file($file)) {
61 3
            $filename = $file;
62
        } elseif (is_file($this->path . $file . $this->ext)) {
63
            $filename = $this->path . $file . $this->ext;
64
        }
65
66 3
        if (isset($filename)) {
67 3
            return $this->parse($filename, $name);
68
        }
69
70
        return $this->config;
71
    }
72
73
    /**
74
     * 解析配置文件
75
     * @access public
76
     * @param  string $file 配置文件名
77
     * @param  string $name 一级配置名
78
     * @return array
79
     */
80 3
    protected function parse(string $file, string $name): array
81
    {
82 3
        $type   = pathinfo($file, PATHINFO_EXTENSION);
83 3
        $config = [];
84 3
        $config = match ($type) {
85 3
            'php' => include $file,
86 3
            'yml', 'yaml' => function_exists('yaml_parse_file') ? yaml_parse_file($file) : [],
87 3
            'ini'         => parse_ini_file($file, true, INI_SCANNER_TYPED) ?: [],
88 3
            'json'        => json_decode(file_get_contents($file), true),
89 3
            default       => [],
90 3
        };
91
92 3
        return is_array($config) ? $this->set($config, strtolower($name)) : [];
0 ignored issues
show
introduced by
The condition is_array($config) is always true.
Loading history...
93
    }
94
95
    /**
96
     * 检测配置是否存在
97
     * @access public
98
     * @param  string $name 配置参数名(支持多级配置 .号分割)
99
     * @return bool
100
     */
101 3
    public function has(string $name): bool
102
    {
103 3
        if (!str_contains($name, '.') && !isset($this->config[strtolower($name)])) {
104
            return false;
105
        }
106
107 3
        return !is_null($this->get($name));
108
    }
109
110
    /**
111
     * 获取一级配置
112
     * @access protected
113
     * @param  string $name 一级配置名
114
     * @return array
115
     */
116 57
    protected function pull(string $name): array
117
    {
118 57
        return $this->config[$name] ?? [];
119
    }
120
121
    /**
122
     * 注册配置获取器
123
     * @access public
124
     * @param  Closure $callback
125
     * @return void
126
     */
127
    public function hook(Closure $callback)
128
    {
129
        $this->hook = $callback;
130
    }
131
132
    /**
133
     * 获取配置参数 为空则获取所有配置
134
     * @access public
135
     * @param  string $name    配置参数名(支持多级配置 .号分割)
136
     * @param  mixed  $default 默认值
137
     * @return mixed
138
     */
139 84
    public function get(?string $name = null, $default = null)
140
    {
141
        // 无参数时获取所有
142 84
        if (empty($name)) {
143
            return $this->config;
144
        }
145
146 84
        if (!str_contains($name, '.')) {
147 57
            $name   = strtolower($name);
148 57
            $result = $this->pull($name);
149 57
            return $this->hook ? $this->lazy($name, $result, []) : $result;
150
        }
151
152 78
        $item    = explode('.', $name);
153 78
        $item[0] = strtolower($item[0]);
154 78
        $config  = $this->config;
155
156 78
        foreach ($item as $val) {
157 78
            if (isset($config[$val])) {
158 6
                $config = $config[$val];
159
            } else {
160 75
                return $this->hook ? $this->lazy($name, null, $default) : $default;
161
            }
162
        }
163
164 6
        return $this->hook ? $this->lazy($name, $config, $default) : $config;
165
    }
166
167
    /**
168
     * 通过获取器加载配置
169
     * @access public
170
     * @param  string  $name 配置参数
171
     * @param  mixed   $value 配置值
172
     * @param  mixed   $default 默认值
173
     * @return mixed
174
     */
175
    protected function lazy(?string $name, $value = null, $default = null)
176
    {
177
        // 通过获取器返回
178
        $result = call_user_func_array($this->hook, [$name, $value]);
179
        if (is_null($result)) {
180
            return $default;
181
        }
182
        return $result;
183
    }
184
185
    /**
186
     * 设置配置参数 name为数组则为批量设置
187
     * @access public
188
     * @param  array  $config 配置参数
189
     * @param  string $name 配置名
190
     * @return array
191
     */
192 6
    public function set(array $config, ?string $name = null): array
193
    {
194 6
        if (empty($name)) {
195
            $this->config = array_merge($this->config, array_change_key_case($config));
196
            return $this->config;
197
        }
198
199 6
        if (isset($this->config[$name])) {
200
            $result = array_merge($this->config[$name], $config);
201
        } else {
202 6
            $result = $config;
203
        }
204
205 6
        $this->config[$name] = $result;
206
207 6
        return $result;
208
    }
209
}
210