|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Bavix\Config; |
|
4
|
|
|
|
|
5
|
|
|
use Bavix\Exceptions; |
|
6
|
|
|
use Bavix\Exceptions\NotFound; |
|
7
|
|
|
use Bavix\Helpers\File; |
|
8
|
|
|
use Bavix\SDK\FileLoader; |
|
9
|
|
|
use Bavix\SDK\Path; |
|
10
|
|
|
use Bavix\Slice\Slice; |
|
11
|
|
|
|
|
12
|
|
|
class Config |
|
13
|
|
|
{ |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @var string |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $root; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var FileLoader\DataInterface[] |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $loaders; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var Slice[] |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $slices; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var array |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $extensions; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var array|Slice |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $parameters; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Config constructor. |
|
42
|
|
|
* |
|
43
|
|
|
* @param string $root |
|
44
|
|
|
* |
|
45
|
|
|
* @throws Exceptions\Invalid |
|
46
|
|
|
* @throws Exceptions\PermissionDenied |
|
47
|
|
|
*/ |
|
48
|
|
|
public function __construct($root) |
|
49
|
|
|
{ |
|
50
|
|
|
$this->root = Path::slash($root); |
|
51
|
|
|
$this->extensions = FileLoader::extensions(); |
|
52
|
|
|
|
|
53
|
|
|
try { |
|
54
|
|
|
$this->parameters = $this->loader('_bavix')->asSlice(); |
|
55
|
|
|
} catch (NotFound\Path $path) { |
|
56
|
|
|
// file `_bavix` not found |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @param string $name |
|
62
|
|
|
* |
|
63
|
|
|
* @return FileLoader\DataInterface |
|
64
|
|
|
* |
|
65
|
|
|
* @throws NotFound\Path |
|
66
|
|
|
* @throws Exceptions\Invalid |
|
67
|
|
|
* @throws Exceptions\PermissionDenied |
|
68
|
|
|
*/ |
|
69
|
|
|
protected function loader($name) |
|
70
|
|
|
{ |
|
71
|
|
|
if (false !== \strpos('.', $name)) { |
|
72
|
|
|
throw new Exceptions\Invalid($name); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
if (isset($this->loaders[$name])) { |
|
76
|
|
|
return $this->loaders[$name]; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
foreach ($this->extensions as $extension) { |
|
80
|
|
|
try { |
|
81
|
|
|
$path = $this->buildPath($name, $extension); |
|
82
|
|
|
$this->loaders[$name] = FileLoader::load($path); |
|
83
|
|
|
|
|
84
|
|
|
return $this->loaders[$name]; |
|
85
|
|
|
} catch (NotFound\Path $argumentException) { |
|
86
|
|
|
continue; |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
throw new NotFound\Path($this->root . $name); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @param string $name |
|
95
|
|
|
* @param string $extension |
|
96
|
|
|
* |
|
97
|
|
|
* @return string |
|
98
|
|
|
*/ |
|
99
|
|
|
protected function buildPath($name, $extension) |
|
100
|
|
|
{ |
|
101
|
|
|
return $this->root . $name . '.' . $extension; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @param string $name |
|
106
|
|
|
* |
|
107
|
|
|
* @return Slice |
|
108
|
|
|
* |
|
109
|
|
|
* @throws NotFound\Path |
|
110
|
|
|
* @throws Exceptions\Invalid |
|
111
|
|
|
* @throws Exceptions\PermissionDenied |
|
112
|
|
|
*/ |
|
113
|
|
|
public function get($name) |
|
114
|
|
|
{ |
|
115
|
|
|
if (empty($this->slices[$name])) { |
|
116
|
|
|
$this->slices[$name] = $this->loader($name)->asSlice( |
|
117
|
|
|
$this->parameters |
|
118
|
|
|
); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
return $this->slices[$name]; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* @param string $name |
|
126
|
|
|
* @param Slice|array $data |
|
127
|
|
|
* |
|
128
|
|
|
* @return bool |
|
129
|
|
|
* |
|
130
|
|
|
* @throws Exceptions\Invalid |
|
131
|
|
|
* @throws Exceptions\PermissionDenied |
|
132
|
|
|
*/ |
|
133
|
|
|
public function save($name, $data) |
|
134
|
|
|
{ |
|
135
|
|
|
try { |
|
136
|
|
|
return $this->loader($name)->save($data); |
|
137
|
|
|
} catch (NotFound\Path $exception) { |
|
138
|
|
|
File::touch($this->buildPath($name, $this->extensions[0])); |
|
139
|
|
|
return $this->save($name, $data); |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* @param string $name |
|
145
|
|
|
* |
|
146
|
|
|
* @return bool |
|
147
|
|
|
* |
|
148
|
|
|
* @throws NotFound\Path |
|
149
|
|
|
* @throws Exceptions\Invalid |
|
150
|
|
|
* @throws Exceptions\PermissionDenied |
|
151
|
|
|
*/ |
|
152
|
|
|
public function remove($name) |
|
153
|
|
|
{ |
|
154
|
|
|
$loader = $this->loader($name); |
|
155
|
|
|
$path = $loader->path(); |
|
156
|
|
|
|
|
157
|
|
|
if (!File::isFile($path)) { |
|
158
|
|
|
throw new NotFound\Path($path); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
return File::remove($path); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* @return $this |
|
166
|
|
|
*/ |
|
167
|
|
|
public function cleanup() |
|
168
|
|
|
{ |
|
169
|
|
|
$this->loaders = []; |
|
170
|
|
|
$this->slices = []; |
|
171
|
|
|
|
|
172
|
|
|
return $this; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
} |
|
176
|
|
|
|