@@ -9,11 +9,11 @@ discard block |
||
9 | 9 | */ |
10 | 10 | abstract class ConfigLoader |
11 | 11 | { |
12 | - public function loadConfig(string $path, array $extra =[]):?array |
|
12 | + public function loadConfig(string $path, array $extra = []): ?array |
|
13 | 13 | { |
14 | - $data=null; |
|
14 | + $data = null; |
|
15 | 15 | if (!file_exists($path)) { |
16 | - $path =$this->resolve($path); |
|
16 | + $path = $this->resolve($path); |
|
17 | 17 | } |
18 | 18 | if ($path) { |
19 | 19 | $ext = strtolower(pathinfo($path, PATHINFO_EXTENSION)); |
@@ -27,25 +27,25 @@ discard block |
||
27 | 27 | return $data; |
28 | 28 | } |
29 | 29 | |
30 | - protected function loadJson(string $path, array $extra =[]):array |
|
30 | + protected function loadJson(string $path, array $extra = []):array |
|
31 | 31 | { |
32 | 32 | $content = file_get_contents($path); |
33 | - $content =$this->parseValue($content, $extra); |
|
33 | + $content = $this->parseValue($content, $extra); |
|
34 | 34 | $data = json_decode($content, true); |
35 | - if (json_last_error()!==JSON_ERROR_NONE) { |
|
35 | + if (json_last_error() !== JSON_ERROR_NONE) { |
|
36 | 36 | throw new JSONException(json_last_error()); |
37 | 37 | } |
38 | 38 | return $data; |
39 | 39 | } |
40 | 40 | |
41 | - protected function loadIni(string $path, array $extra =[]):array |
|
41 | + protected function loadIni(string $path, array $extra = []):array |
|
42 | 42 | { |
43 | 43 | $content = file_get_contents($path); |
44 | - $content =$this->parseValue($content, $extra); |
|
44 | + $content = $this->parseValue($content, $extra); |
|
45 | 45 | return \parse_ini_string($content, true); |
46 | 46 | } |
47 | 47 | |
48 | - protected function loadYaml(string $path, array $extra =[]):array |
|
48 | + protected function loadYaml(string $path, array $extra = []):array |
|
49 | 49 | { |
50 | 50 | if (function_exists('yaml_parse')) { |
51 | 51 | $name = 'yaml_parse'; |
@@ -55,25 +55,25 @@ discard block |
||
55 | 55 | throw new YamlException("parse yaml config error : missing yaml extension or spyc", 1); |
56 | 56 | } |
57 | 57 | $content = file_get_contents($path); |
58 | - $content =$this->parseValue($content, $extra); |
|
58 | + $content = $this->parseValue($content, $extra); |
|
59 | 59 | return \call_user_func_array($name, [$content]); |
60 | 60 | } |
61 | 61 | |
62 | - abstract public function parseValue(string $content, array $extra =[]):string; |
|
62 | + abstract public function parseValue(string $content, array $extra = []):string; |
|
63 | 63 | |
64 | - public function resolve(string $path):?string |
|
64 | + public function resolve(string $path): ?string |
|
65 | 65 | { |
66 | 66 | if (file_exists($path)) { |
67 | 67 | return $path; |
68 | 68 | } |
69 | 69 | $basepath = pathinfo($path, PATHINFO_FILENAME); |
70 | - if (file_exists($conf = $basepath.'.yml') || file_exists($conf = $basepath.'.yaml')) { |
|
70 | + if (file_exists($conf = $basepath.'.yml') || file_exists($conf = $basepath.'.yaml')) { |
|
71 | 71 | if (function_exists('yaml_parse') || class_exists('Spyc')) { |
72 | 72 | return $conf; |
73 | 73 | } |
74 | 74 | } |
75 | - foreach (['.json','.php','.ini'] as $ext) { |
|
76 | - if (file_exists($conf=$basepath.$ext)) { |
|
75 | + foreach (['.json', '.php', '.ini'] as $ext) { |
|
76 | + if (file_exists($conf = $basepath.$ext)) { |
|
77 | 77 | return $conf; |
78 | 78 | } |
79 | 79 | } |
@@ -9,9 +9,9 @@ discard block |
||
9 | 9 | */ |
10 | 10 | class Config extends ConfigLoader |
11 | 11 | { |
12 | - public $config=[]; |
|
12 | + public $config = []; |
|
13 | 13 | |
14 | - public function load(string $path, array $extra =[]) |
|
14 | + public function load(string $path, array $extra = []) |
|
15 | 15 | { |
16 | 16 | $data = $this->loadConfig($path, $extra); |
17 | 17 | if ($data) { |
@@ -26,10 +26,10 @@ discard block |
||
26 | 26 | |
27 | 27 | public function assign(array $config) |
28 | 28 | { |
29 | - return $this->config=array_merge($this->config, $config); |
|
29 | + return $this->config = array_merge($this->config, $config); |
|
30 | 30 | } |
31 | 31 | |
32 | - public function get(string $name=null, $default=null) |
|
32 | + public function get(string $name = null, $default = null) |
|
33 | 33 | { |
34 | 34 | if (is_null($name)) { |
35 | 35 | return $this->config; |
@@ -37,7 +37,7 @@ discard block |
||
37 | 37 | return ArrayDotAccess::get($this->config, $name, $default); |
38 | 38 | } |
39 | 39 | |
40 | - public function set(string $name, $value, $combine=null) |
|
40 | + public function set(string $name, $value, $combine = null) |
|
41 | 41 | { |
42 | 42 | return ArrayDotAccess::set($this->config, $name, $value, $combine); |
43 | 43 | } |
@@ -47,17 +47,17 @@ discard block |
||
47 | 47 | return ArrayDotAccess::exist($this->config, $name); |
48 | 48 | } |
49 | 49 | |
50 | - public function parseValue(string $content, array $extra =[]):string |
|
50 | + public function parseValue(string $content, array $extra = []):string |
|
51 | 51 | { |
52 | - return preg_replace_callback('/\$\{(.+?)\}/', function ($matchs) use ($extra) { |
|
52 | + return preg_replace_callback('/\$\{(.+?)\}/', function($matchs) use ($extra) { |
|
53 | 53 | $name = $matchs[1]; |
54 | - if (($value = ArrayDotAccess::get($extra, $name, null))!==null) { |
|
54 | + if (($value = ArrayDotAccess::get($extra, $name, null)) !== null) { |
|
55 | 55 | } elseif (defined($name)) { |
56 | 56 | $value = constant($name); |
57 | 57 | } else { |
58 | 58 | $value = $this->get($name, $matchs[0]); |
59 | 59 | } |
60 | - return is_string($value)?trim(json_encode($value), '"'):$value; |
|
60 | + return is_string($value) ?trim(json_encode($value), '"') : $value; |
|
61 | 61 | }, $content); |
62 | 62 | } |
63 | 63 | } |