1
|
|
|
<?php |
2
|
|
|
namespace phpbu\App\Configuration\Loader; |
3
|
|
|
|
4
|
|
|
use phpbu\App\Configuration; |
5
|
|
|
use phpbu\App\Configuration\Loader; |
6
|
|
|
use phpbu\App\Exception; |
7
|
|
|
use phpbu\App\Util\Arr; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Loader class for a phpbu JSON configuration file. |
11
|
|
|
* |
12
|
|
|
* Example JSON configuration file: |
13
|
|
|
* <code> |
14
|
|
|
* { |
15
|
|
|
* "verbose": true, |
16
|
|
|
* "colors": true, |
17
|
|
|
* "debug": false, |
18
|
|
|
* "logging": [ |
19
|
|
|
* { |
20
|
|
|
* "type": "json", |
21
|
|
|
* "target": "backup/json.log" |
22
|
|
|
* } |
23
|
|
|
* ], |
24
|
|
|
* "backups": [ |
25
|
|
|
* { |
26
|
|
|
* "name": "some dir", |
27
|
|
|
* "source": { |
28
|
|
|
* "type": "tar", |
29
|
|
|
* "options": { |
30
|
|
|
* "path": "some/path" |
31
|
|
|
* } |
32
|
|
|
* }, |
33
|
|
|
* "target": { |
34
|
|
|
* "dirname": "backup", |
35
|
|
|
* "filename": "tarball-%Y%m%d-%H%i.tar", |
36
|
|
|
* "compress": "bzip2" |
37
|
|
|
* }, |
38
|
|
|
* "checks": [ |
39
|
|
|
* { |
40
|
|
|
* "type": "sizemin", |
41
|
|
|
* "value": "1B" |
42
|
|
|
* } |
43
|
|
|
* ], |
44
|
|
|
* "cleanup": { |
45
|
|
|
* "type": "Capacity", |
46
|
|
|
* "options": { |
47
|
|
|
* "size": "5M" |
48
|
|
|
* } |
49
|
|
|
* } |
50
|
|
|
* } |
51
|
|
|
* ] |
52
|
|
|
* } |
53
|
|
|
* </code> |
54
|
|
|
* |
55
|
|
|
* @package phpbu |
56
|
|
|
* @subpackage App |
57
|
|
|
* @author Sebastian Feldmann <[email protected]> |
58
|
|
|
* @copyright Sebastian Feldmann <[email protected]> |
59
|
|
|
* @license https://opensource.org/licenses/MIT The MIT License (MIT) |
60
|
|
|
* @link http://phpbu.de/ |
61
|
|
|
* @since Class available since Release 2.1.2 |
62
|
|
|
*/ |
63
|
|
|
class Json extends File implements Loader |
64
|
|
|
{ |
65
|
|
|
/** |
66
|
|
|
* Config file. |
67
|
|
|
* |
68
|
|
|
* @var array |
69
|
|
|
*/ |
70
|
|
|
private $json; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Json constructor. |
74
|
|
|
* |
75
|
|
|
* @param string $file |
76
|
|
|
* @param \phpbu\App\Configuration\Bootstrapper $bootstrapper |
77
|
|
|
* @throws \phpbu\App\Exception |
78
|
|
|
*/ |
79
|
20 |
|
public function __construct(string $file, Configuration\Bootstrapper $bootstrapper) |
80
|
|
|
{ |
81
|
20 |
|
parent::__construct($file, $bootstrapper); |
82
|
20 |
|
$this->json = $this->loadJsonFile($file); |
83
|
18 |
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Return list of adapter configs. |
87
|
|
|
* |
88
|
|
|
* @return array |
89
|
|
|
* @throws \phpbu\App\Exception |
90
|
|
|
*/ |
91
|
16 |
|
protected function getAdapterConfigs() |
92
|
|
|
{ |
93
|
16 |
|
$adapters = []; |
94
|
16 |
|
if (isset($this->json['adapters'])) { |
95
|
3 |
|
foreach ($this->json['adapters'] as $a) { |
96
|
3 |
|
if (!isset($a['type'])) { |
97
|
1 |
|
throw new Exception('invalid adapter configuration: type missing'); |
98
|
|
|
} |
99
|
2 |
|
if (!isset($a['name'])) { |
100
|
1 |
|
throw new Exception('invalid adapter configuration: name missing'); |
101
|
|
|
} |
102
|
1 |
|
$adapters[] = new Configuration\Adapter($a['type'], $a['name'], $this->getOptions($a)); |
103
|
|
|
} |
104
|
|
|
} |
105
|
14 |
|
return $adapters; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Set the phpbu application settings. |
110
|
|
|
* |
111
|
|
|
* @param \phpbu\App\Configuration $configuration |
112
|
|
|
*/ |
113
|
16 |
|
public function setAppSettings(Configuration $configuration) |
114
|
|
|
{ |
115
|
16 |
|
if (isset($this->json['bootstrap'])) { |
116
|
9 |
|
$configuration->setBootstrap($this->toAbsolutePath($this->json['bootstrap'])); |
117
|
|
|
} |
118
|
16 |
|
if (isset($this->json['verbose'])) { |
119
|
16 |
|
$configuration->setVerbose($this->json['verbose']); |
120
|
|
|
} |
121
|
16 |
|
if (isset($this->json['colors'])) { |
122
|
9 |
|
$configuration->setColors($this->json['colors']); |
123
|
|
|
} |
124
|
16 |
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Set the log configuration. |
128
|
|
|
* |
129
|
|
|
* @param \phpbu\App\Configuration $configuration |
130
|
|
|
* @throws \phpbu\App\Exception |
131
|
|
|
*/ |
132
|
14 |
|
public function setLoggers(Configuration $configuration) |
133
|
|
|
{ |
134
|
14 |
|
if (isset($this->json['logging'])) { |
135
|
14 |
|
foreach ($this->json['logging'] as $l) { |
136
|
14 |
|
if (!isset($l['type'])) { |
137
|
1 |
|
throw new Exception('invalid logger configuration: type missing'); |
138
|
|
|
} |
139
|
13 |
|
$type = $l['type']; |
140
|
13 |
|
$options = $this->getOptions($l); |
141
|
13 |
|
if (isset($options['target'])) { |
142
|
1 |
|
$options['target'] = $this->toAbsolutePath($options['target']); |
143
|
|
|
} |
144
|
|
|
// search for target attribute to convert to option |
145
|
13 |
|
if (isset($l['target'])) { |
146
|
12 |
|
$options['target'] = $this->toAbsolutePath($this->getAdapterizedValue($l['target'])); |
147
|
|
|
} |
148
|
13 |
|
$configuration->addLogger(new Configuration\Logger($type, $options)); |
149
|
|
|
} |
150
|
|
|
} |
151
|
13 |
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Set the backup configurations. |
155
|
|
|
* |
156
|
|
|
* @param \phpbu\App\Configuration $configuration |
157
|
|
|
* @throws \phpbu\App\Exception |
158
|
|
|
*/ |
159
|
13 |
|
public function setBackups(Configuration $configuration) |
160
|
|
|
{ |
161
|
13 |
|
if (!isset($this->json['backups'])) { |
162
|
1 |
|
throw new Exception('no backup configured'); |
163
|
|
|
} |
164
|
12 |
|
foreach ($this->json['backups'] as $backup) { |
165
|
12 |
|
$configuration->addBackup($this->getBackupConfig($backup)); |
166
|
|
|
} |
167
|
6 |
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Get the config for a single backup node. |
171
|
|
|
* |
172
|
|
|
* @param array $json |
173
|
|
|
* @throws \phpbu\App\Exception |
174
|
|
|
* @return \phpbu\App\Configuration\Backup |
175
|
|
|
*/ |
176
|
12 |
|
private function getBackupConfig(array $json) |
177
|
|
|
{ |
178
|
12 |
|
$name = $this->getAdapterizedValue(Arr::getValue($json, 'name', '')); |
179
|
12 |
|
$stopOnFailure = Arr::getValue($json, 'stopOnFailure', false); |
180
|
12 |
|
$backup = new Configuration\Backup($name, $stopOnFailure); |
181
|
|
|
|
182
|
12 |
|
$backup->setSource($this->getSource($json)); |
183
|
10 |
|
$backup->setTarget($this->getTarget($json)); |
184
|
|
|
|
185
|
9 |
|
$this->setChecks($backup, $json); |
186
|
9 |
|
$this->setCrypt($backup, $json); |
187
|
8 |
|
$this->setSyncs($backup, $json); |
188
|
7 |
|
$this->setCleanup($backup, $json); |
189
|
|
|
|
190
|
6 |
|
return $backup; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Get source configuration. |
195
|
|
|
* |
196
|
|
|
* @param array $json |
197
|
|
|
* @return \phpbu\App\Configuration\Backup\Source |
198
|
|
|
* @throws \phpbu\App\Exception |
199
|
|
|
*/ |
200
|
12 |
|
protected function getSource(array $json) |
201
|
|
|
{ |
202
|
12 |
|
if (!isset($json['source'])) { |
203
|
1 |
|
throw new Exception('backup requires exactly one source config'); |
204
|
|
|
} |
205
|
11 |
|
if (!isset($json['source']['type'])) { |
206
|
1 |
|
throw new Exception('source requires type'); |
207
|
|
|
} |
208
|
|
|
|
209
|
10 |
|
return new Configuration\Backup\Source($json['source']['type'], $this->getOptions($json['source'])); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Get Target configuration. |
214
|
|
|
* |
215
|
|
|
* @param array $json |
216
|
|
|
* @return \phpbu\App\Configuration\Backup\Target |
217
|
|
|
* @throws \phpbu\App\Exception |
218
|
|
|
*/ |
219
|
10 |
|
protected function getTarget(array $json) |
220
|
|
|
{ |
221
|
10 |
|
if (!isset($json['target'])) { |
222
|
1 |
|
throw new Exception('backup requires a target config'); |
223
|
|
|
} |
224
|
9 |
|
$compress = Arr::getValue($json['target'], 'compress'); |
225
|
9 |
|
$filename = $this->getAdapterizedValue(Arr::getValue($json['target'], 'filename', '')); |
226
|
9 |
|
$dirname = $this->getAdapterizedValue(Arr::getValue($json['target'], 'dirname', '')); |
227
|
|
|
|
228
|
9 |
|
if ($dirname) { |
229
|
9 |
|
$dirname = $this->toAbsolutePath($dirname); |
230
|
|
|
} |
231
|
|
|
|
232
|
9 |
|
return new Configuration\Backup\Target($dirname, $filename, $compress); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* Set backup checks. |
237
|
|
|
* |
238
|
|
|
* @param \phpbu\App\Configuration\Backup $backup |
239
|
|
|
* @param array $json |
240
|
|
|
*/ |
241
|
9 |
|
protected function setChecks(Configuration\Backup $backup, array $json) |
242
|
|
|
{ |
243
|
9 |
|
if (isset($json['checks'])) { |
244
|
9 |
|
foreach ($json['checks'] as $c) { |
245
|
9 |
|
$type = Arr::getValue($c, 'type'); |
246
|
9 |
|
$value = Arr::getValue($c, 'value'); |
247
|
|
|
// skip invalid sanity checks |
248
|
9 |
|
if (!$type || !$value) { |
249
|
1 |
|
continue; |
250
|
|
|
} |
251
|
8 |
|
$backup->addCheck(new Configuration\Backup\Check($type, $value)); |
252
|
|
|
} |
253
|
|
|
} |
254
|
9 |
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* Set the crypt configuration. |
258
|
|
|
* |
259
|
|
|
* @param \phpbu\App\Configuration\Backup $backup |
260
|
|
|
* @param array $json |
261
|
|
|
* @throws \phpbu\App\Exception |
262
|
|
|
*/ |
263
|
9 |
|
protected function setCrypt(Configuration\Backup $backup, array $json) |
264
|
|
|
{ |
265
|
9 |
|
if (isset($json['crypt'])) { |
266
|
9 |
|
if (!isset($json['crypt']['type'])) { |
267
|
1 |
|
throw new Exception('invalid crypt configuration: type missing'); |
268
|
|
|
} |
269
|
8 |
|
$type = $json['crypt']['type']; |
270
|
8 |
|
$skip = Arr::getValue($json['crypt'], 'skipOnFailure', true); |
271
|
8 |
|
$options = $this->getOptions($json['crypt']); |
272
|
8 |
|
$backup->setCrypt(new Configuration\Backup\Crypt($type, $skip, $options)); |
273
|
|
|
} |
274
|
8 |
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* Set backup sync configurations. |
278
|
|
|
* |
279
|
|
|
* @param \phpbu\App\Configuration\Backup $backup |
280
|
|
|
* @param array $json |
281
|
|
|
* @throws \phpbu\App\Exception |
282
|
|
|
*/ |
283
|
8 |
|
protected function setSyncs(Configuration\Backup $backup, array $json) |
284
|
|
|
{ |
285
|
8 |
|
if (isset($json['syncs'])) { |
286
|
8 |
|
foreach ($json['syncs'] as $s) { |
287
|
8 |
|
if (!isset($s['type'])) { |
288
|
1 |
|
throw new Exception('invalid sync configuration: attribute type missing'); |
289
|
|
|
} |
290
|
7 |
|
$type = $s['type']; |
291
|
7 |
|
$skip = Arr::getValue($s, 'skipOnFailure', true); |
292
|
7 |
|
$options = $this->getOptions($s); |
293
|
7 |
|
$backup->addSync(new Configuration\Backup\Sync($type, $skip, $options)); |
294
|
|
|
} |
295
|
|
|
} |
296
|
7 |
|
} |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* Set the cleanup configuration. |
300
|
|
|
* |
301
|
|
|
* @param \phpbu\App\Configuration\Backup $backup |
302
|
|
|
* @param array $json |
303
|
|
|
* @throws \phpbu\App\Exception |
304
|
|
|
*/ |
305
|
7 |
|
protected function setCleanup(Configuration\Backup $backup, array $json) |
306
|
|
|
{ |
307
|
7 |
|
if (isset($json['cleanup'])) { |
308
|
7 |
|
if (!isset($json['cleanup']['type'])) { |
309
|
1 |
|
throw new Exception('invalid cleanup configuration: type missing'); |
310
|
|
|
} |
311
|
6 |
|
$type = $json['cleanup']['type']; |
312
|
6 |
|
$skip = Arr::getValue($json['cleanup'], 'skipOnFailure', true); |
313
|
6 |
|
$options = $this->getOptions($json['cleanup']); |
314
|
6 |
|
$backup->setCleanup(new Configuration\Backup\Cleanup($type, $skip, $options)); |
315
|
|
|
} |
316
|
6 |
|
} |
317
|
|
|
|
318
|
|
|
/** |
319
|
|
|
* Extracts all option tags |
320
|
|
|
* |
321
|
|
|
* @param array $json |
322
|
|
|
* @return array |
323
|
|
|
* @throws \phpbu\App\Exception |
324
|
|
|
*/ |
325
|
13 |
|
protected function getOptions(array $json) |
326
|
|
|
{ |
327
|
13 |
|
$options = isset($json['options']) ? $json['options'] : []; |
328
|
|
|
|
329
|
13 |
|
foreach ($options as $name => $value) { |
330
|
11 |
|
$options[$name] = $this->getAdapterizedValue($value); |
331
|
|
|
} |
332
|
|
|
|
333
|
13 |
|
return $options; |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
/** |
337
|
|
|
* Load the JSON-File. |
338
|
|
|
* |
339
|
|
|
* @param string $filename |
340
|
|
|
* @throws \phpbu\App\Exception |
341
|
|
|
* @return array |
342
|
|
|
*/ |
343
|
20 |
|
private function loadJsonFile($filename) |
344
|
|
|
{ |
345
|
20 |
|
$contents = $this->loadFile($filename); |
346
|
19 |
|
$reporting = error_reporting(0); |
347
|
19 |
|
$json = json_decode($contents, true); |
348
|
19 |
|
error_reporting($reporting); |
349
|
|
|
|
350
|
19 |
|
if (!is_array($json)) { |
351
|
1 |
|
throw new Exception(sprintf('Error loading file "%s"', $filename)); |
352
|
|
|
} |
353
|
18 |
|
return $json; |
354
|
|
|
} |
355
|
|
|
} |
356
|
|
|
|