Passed
Push — master ( 3ad46d...488ad8 )
by 世昌
01:49
created

Config::set()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 3
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
namespace nebula\application\config;
3
4
use nebula\application\config\Config;
5
use nebula\component\arrayobject\ArrayDotAccess;
6
7
/**
8
 * 文件配置类
9
 */
10
class Config extends ConfigLoader
11
{
12
    public $config=[];
13
14
    public function load(string $path, array $extra =[])
15
    {
16
        $data = $this->loadConfig($path, $extra);
17
        if ($data) {
18
            return $this->assign($data);
19
        }
20
    }
21
22
    public function exist(string $path):bool
23
    {
24
        return static::resolve($path) !== null;
0 ignored issues
show
Bug Best Practice introduced by
The method nebula\application\config\ConfigLoader::resolve() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

24
        return static::/** @scrutinizer ignore-call */ resolve($path) !== null;
Loading history...
25
    }
26
27
    public function assign(array $config)
28
    {
29
        return $this->config=array_merge($this->config, $config);
30
    }
31
32
    public function get(string $name=null, $default=null)
33
    {
34
        if (is_null($name)) {
35
            return $this->config;
36
        }
37
        return ArrayDotAccess::get($this->config, $name, $default);
38
    }
39
40
    public function set(string $name, $value, $combine=null)
41
    {
42
        return ArrayDotAccess::set($this->config, $name, $value, $combine);
43
    }
44
45
    public function has(string $name)
46
    {
47
        return ArrayDotAccess::exist($this->config, $name);
48
    }
49
50
    public function parseValue(string $content, array $extra =[]):string
51
    {
52
        return preg_replace_callback('/\$\{(.+?)\}/', function ($matchs) use ($extra) {
53
            $name = $matchs[1];
54
            if (($value = ArrayDotAccess::get($extra, $name, null))!==null) {
55
            } elseif (defined($name)) {
56
                $value = constant($name);
57
            } else {
58
                $value = $this->get($name, $matchs[0]);
59
            }
60
            return is_string($value)?trim(json_encode($value), '"'):$value;
61
        }, $content);
62
    }
63
}
64