forked from easy-swoole/easyswoole
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfig.php
112 lines (93 loc) · 2.41 KB
/
Config.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php
/**
* Created by PhpStorm.
* User: yf
* Date: 2018/5/28
* Time: 下午5:46
*/
namespace EasySwoole\EasySwoole;
use EasySwoole\Component\Singleton;
use EasySwoole\Config\AbstractConfig;
use EasySwoole\Config\TableConfig;
class Config
{
private $conf;
use Singleton;
public function __construct(?AbstractConfig $config = null)
{
if($config == null){
$config = new TableConfig();
}
$this->conf = $config;
}
function storageHandler(AbstractConfig $config = null):AbstractConfig
{
if($config){
$this->conf = $config;
}
return $this->conf;
}
/**
* 获取配置项
* @param string $keyPath 配置项名称 支持点语法
* @return array|mixed|null
*/
public function getConf($keyPath = '')
{
if ($keyPath == '') {
return $this->toArray();
}
return $this->conf->getConf($keyPath);
}
public function setConf($keyPath, $data): bool
{
return $this->conf->setConf($keyPath, $data);
}
public function toArray(): array
{
return $this->conf->getConf();
}
public function load(array $conf): bool
{
return $this->conf->load($conf);
}
public function merge(array $conf):bool
{
return $this->conf->merge($conf);
}
/**
* 载入一个文件的配置项
* @param string $filePath 配置文件路径
* @param bool $merge 是否将内容合并入主配置
* @author : evalor <[email protected]>
*/
public function loadFile($filePath, $merge = false)
{
if (is_file($filePath)) {
$confData = require_once $filePath;
if (is_array($confData) && !empty($confData)) {
$basename = strtolower(basename($filePath, '.php'));
if (!$merge) {
$this->conf->setConf($basename,$confData);
} else {
$this->conf->merge($confData);
}
}
}
}
public function loadEnv(string $file)
{
if(file_exists($file)){
$data = require $file;
if(is_array($data)){
$this->load($data);
}
}else{
throw new \Exception("config file : {$file} is miss");
}
}
public function clear():bool
{
return $this->conf->clear();
}
}