Passed
Push — master ( c1e721...8ec156 )
by 世昌
02:05
created

Config::get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 2
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
namespace suda\application\config;
3
4
use suda\application\config\Config;
5
use suda\component\arrayobject\ArrayDotAccess;
6
7
/**
8
 * 文件配置类
9
 */
10
class Config extends ConfigLoader
11
{
12
    /**
13
     * 配置数组
14
     *
15
     * @var array
16
     */
17
    public $config;
18
19
    public function __construct(array $config = []) {
20
        $this->config = $config;
21
    }
22
23
    public function load(string $path, array $extra =[])
24
    {
25
        $data = $this->loadConfig($path, $extra);
26
        if ($data) {
27
            return $this->assign($data);
28
        }
29
    }
30
31
    public function exist(string $path):bool
32
    {
33
        return $this->resolve($path) !== null;
34
    }
35
36
    public function assign(array $config)
37
    {
38
        return $this->config=array_merge($this->config, $config);
39
    }
40
41
    public function get(string $name=null, $default=null)
42
    {
43
        if (is_null($name)) {
44
            return $this->config;
45
        }
46
        return ArrayDotAccess::get($this->config, $name, $default);
47
    }
48
49
    public function set(string $name, $value, $combine=null)
50
    {
51
        return ArrayDotAccess::set($this->config, $name, $value, $combine);
52
    }
53
54
    public function has(string $name)
55
    {
56
        return ArrayDotAccess::exist($this->config, $name);
57
    }
58
59
    public function parseValue(string $content, array $extra =[]):string
60
    {
61
        return preg_replace_callback('/\$\{(.+?)\}/', function ($matchs) use ($extra) {
62
            $name = $matchs[1];
63
            if (($value = ArrayDotAccess::get($extra, $name, null))!==null) {
64
            } elseif (defined($name)) {
65
                $value = constant($name);
66
            } else {
67
                $value = $this->get($name, $matchs[0]);
68
            }
69
            return is_string($value)?trim(json_encode($value), '"'):$value;
70
        }, $content);
71
    }
72
}
73