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; |
|
|
|
|
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
|
|
|
|