Issues (33)

libs/Config.php (1 issue)

1
<?php
2
/**
3
 * 配置
4
 *
5
 * @author mybsdc <[email protected]>
6
 * @date 2019/3/3
7
 * @time 16:41
8
 */
9
10
namespace Luolongfei\Lib;
11
12
class Config
13
{
14
    /**
15
     * @var Config
16
     */
17
    protected static $instance;
18
19
    /**
20
     * @var array 配置
21
     */
22
    protected $allConfig;
23
24
    public function __construct()
25
    {
26
        $this->allConfig = require ROOT_PATH . '/config.php';
0 ignored issues
show
The constant Luolongfei\Lib\ROOT_PATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
27
    }
28
29
    /**
30
     * 获取配置
31
     *
32
     * @param string $key
33
     * @param string $default 默认值
34
     *
35
     * @return array|mixed|null
36
     */
37
    public function get($key = '', $default = null)
38
    {
39
        $allConfig = $this->allConfig;
40
41
        if (strlen($key)) {
42
            if (strpos($key, '.')) {
43
                $keys = explode('.', $key);
44
                $val = $allConfig;
45
                foreach ($keys as $k) {
46
                    if (!isset($val[$k])) {
47
                        return $default; // 任一下标不存在就返回默认值
48
                    }
49
50
                    $val = $val[$k];
51
                }
52
53
                return $val;
54
            } else {
55
                if (isset($allConfig[$key])) {
56
                    return $allConfig[$key];
57
                }
58
59
                return $default;
60
            }
61
        }
62
63
        return $allConfig;
64
    }
65
66
    public static function instance()
67
    {
68
        if (!self::$instance instanceof self) {
69
            self::$instance = new self();
70
        }
71
72
        return self::$instance;
73
    }
74
}