|
1
|
|
|
<?php |
|
2
|
|
|
namespace Fyuze\Config; |
|
3
|
|
|
|
|
4
|
|
|
use SplFileInfo; |
|
5
|
|
|
use GlobIterator; |
|
6
|
|
|
use InvalidArgumentException; |
|
7
|
|
|
use Fyuze\Config\Parsers\PHP; |
|
8
|
|
|
|
|
9
|
|
|
class Config |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* The configuration path |
|
13
|
|
|
* |
|
14
|
|
|
* @var string |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $path; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Application env |
|
20
|
|
|
* |
|
21
|
|
|
* @var string |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $env; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Loaded configurations |
|
27
|
|
|
* |
|
28
|
|
|
* @var array |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $configs; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var array |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $types = [ |
|
36
|
|
|
PHP::class |
|
37
|
|
|
]; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param string $path |
|
41
|
|
|
* @param string $env |
|
42
|
|
|
*/ |
|
43
|
12 |
|
public function __construct($path, $env) |
|
44
|
|
|
{ |
|
45
|
12 |
|
$this->path = $path; |
|
46
|
12 |
|
$this->env = $env; |
|
47
|
12 |
|
$this->load(); |
|
48
|
|
|
|
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param $key |
|
53
|
|
|
* @param null $default |
|
|
|
|
|
|
54
|
|
|
* @return mixed |
|
55
|
|
|
* @throws InvalidArgumentException |
|
56
|
|
|
*/ |
|
57
|
10 |
|
public function get($key, $default = null) |
|
58
|
|
|
{ |
|
59
|
10 |
|
if (array_key_exists($key, $this->configs)) { |
|
60
|
8 |
|
return $this->configs[$key]; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
7 |
|
$configs = $this->configs; |
|
64
|
|
|
|
|
65
|
7 |
|
foreach (explode('.', $key) as $value) { |
|
66
|
|
|
|
|
67
|
7 |
|
$configs = array_key_exists($value, $configs) ? $configs[$value] : $default; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
7 |
|
return $configs; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param $key |
|
75
|
|
|
* @param $value |
|
76
|
|
|
* @return mixed |
|
77
|
|
|
*/ |
|
78
|
3 |
|
public function set($key, $value) |
|
79
|
|
|
{ |
|
80
|
3 |
|
$configs = &$this->configs; |
|
81
|
|
|
|
|
82
|
3 |
|
foreach (explode('.', $key) as $segment) { |
|
83
|
3 |
|
$configs = &$configs[$segment]; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
3 |
|
return $this->configs[$key] = $configs = $value; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* |
|
91
|
|
|
*/ |
|
92
|
12 |
|
protected function load() |
|
93
|
|
|
{ |
|
94
|
12 |
|
if (!is_dir($this->path)) { |
|
95
|
1 |
|
throw new InvalidArgumentException(sprintf('The path you defined is not a valid directory: %s', $this->path)); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
12 |
|
foreach (new GlobIterator($this->path . '/*.*') as $file) { |
|
99
|
|
|
|
|
100
|
12 |
|
if($config = $this->getType($file)) { |
|
|
|
|
|
|
101
|
12 |
|
$this->configs[$config[0]] = $config[1]; |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @param SplFileInfo $file |
|
108
|
|
|
* @return array |
|
109
|
|
|
*/ |
|
110
|
12 |
|
protected function getType(SplFileInfo $file) |
|
111
|
|
|
{ |
|
112
|
12 |
|
$extension = $file->getExtension(); |
|
113
|
12 |
|
$name = $file->getBasename(".$extension"); |
|
114
|
|
|
|
|
115
|
12 |
|
$type = array_filter($this->types, function ($n) use ($extension) { |
|
116
|
12 |
|
return in_array($extension, $n::$extensions); |
|
117
|
12 |
|
}); |
|
118
|
|
|
|
|
119
|
12 |
|
if (!$class = reset($type)) { |
|
120
|
12 |
|
return false; |
|
|
|
|
|
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
12 |
|
return [$name, (new $class)->parse($file)]; |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|