Passed
Push — master ( 5d2787...c547da )
by 世昌
03:12
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 suda\framework\server;
3
4
use suda\framework\server\config\PathResolver;
5
use suda\framework\server\config\ContentLoader;
6
use suda\component\arrayobject\ArrayDotAccess;
7
8
/**
9
 * 服务器参数处理
10
 */
11
class Config
12
{
13
14
    /**
15
     * 配置数组
16
     *
17
     * @var array
18
     */
19
    public $config;
20
21
    /**
22
     * 静态实例
23
     *
24
     * @var self
25
     */
26
    protected static $instance;
27
28
29
    protected function __construct()
30
    {
31
        $this->config = [];
32
    }
33
34
    /**
35
     * 返回实例
36
     *
37
     * @return self
38
     */
39
    public static function instance()
40
    {
41
        if (isset(static::$instance)) {
42
            return static::$instance;
43
        }
44
        return static::$instance = new static;
45
    }
46
47
    /**
48
     * 加载配置
49
     *
50
     * @param string $path
51
     * @param array $extra
52
     * @return self
53
     */
54
    public function load(string $path, array $extra = null)
55
    {
56
        $data = $this->loadConfig($path, $extra ?? $this->config);
57
        if ($data) {
58
            $this->assign($data);
59
        }
60
        return $this;
61
    }
62
63
    public function exist(string $path):bool
64
    {
65
        return $this->resolve($path) !== null;
0 ignored issues
show
Bug introduced by
The method resolve() does not exist on suda\framework\server\Config. ( Ignorable by Annotation )

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

65
        return $this->/** @scrutinizer ignore-call */ resolve($path) !== null;

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
66
    }
67
68
    public function assign(array $config)
69
    {
70
        return $this->config = array_merge($this->config, $config);
71
    }
72
73
    public function get(string $name = null, $default = null)
74
    {
75
        if (null === $name) {
76
            return $this->config;
77
        }
78
        return ArrayDotAccess::get($this->config, $name, $default);
79
    }
80
81
    public function set(string $name, $value, $combine = null)
82
    {
83
        return ArrayDotAccess::set($this->config, $name, $value, $combine);
84
    }
85
86
    public function has(string $name)
87
    {
88
        return ArrayDotAccess::exist($this->config, $name);
89
    }
90
91
    public function loadConfig(string $path, array $extra = []):?array
92
    {
93
        $data = null;
94
        if (!file_exists($path)) {
95
            $path = PathResolver::resolve($path);
96
        }
97
        if ($path) {
98
            $ext = strtolower(pathinfo($path, PATHINFO_EXTENSION));
99
            switch ($ext) {
100
                case 'yml': case 'yaml': $data = ContentLoader::loadYaml($path, $extra); break;
101
                case 'php': $data = include $path; break;
102
                case 'ini': $data = ContentLoader::loadIni($path, $extra); break;
103
                case 'json': default: $data = ContentLoader::loadJson($path, $extra);
104
            }
105
        }
106
        return $data;
107
    }
108
}
109