1
|
|
|
<?php |
2
|
|
|
namespace phpbu\App\Result; |
3
|
|
|
|
4
|
|
|
use phpbu\App\Cli\Statistics; |
5
|
|
|
use phpbu\App\Event; |
6
|
|
|
use phpbu\App\Listener; |
7
|
|
|
use phpbu\App\Result; |
8
|
|
|
use phpbu\App\Util; |
9
|
|
|
use SebastianBergmann\Environment\Console; |
10
|
|
|
use SebastianBergmann\Environment\Runtime; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Default app output. |
14
|
|
|
* |
15
|
|
|
* Heavily 'inspired' by Sebastian Bergmann's phpunit PHPUnit_TextUI_ResultPrinter. |
16
|
|
|
* |
17
|
|
|
* @package phpbu |
18
|
|
|
* @subpackage Result |
19
|
|
|
* @author Sebastian Feldmann <[email protected]> |
20
|
|
|
* @copyright Sebastian Feldmann <[email protected]> |
21
|
|
|
* @license https://opensource.org/licenses/MIT The MIT License (MIT) |
22
|
|
|
* @link http://phpbu.de/ |
23
|
|
|
* @since Class available since Release 1.0.0 |
24
|
|
|
*/ |
25
|
|
|
class PrinterCli implements Listener |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* Verbose output |
29
|
|
|
* |
30
|
|
|
* @var bool |
31
|
|
|
*/ |
32
|
|
|
protected $verbose = false; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Output with colors |
36
|
|
|
* |
37
|
|
|
* @var bool |
38
|
|
|
*/ |
39
|
|
|
protected $colors = false; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Is debug active |
43
|
|
|
* |
44
|
|
|
* @var bool |
45
|
|
|
*/ |
46
|
|
|
protected $debug = false; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Amount of executed backups |
50
|
|
|
* |
51
|
|
|
* @var integer |
52
|
|
|
*/ |
53
|
|
|
private $numBackups = 0; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Amount of executed checks |
57
|
|
|
* |
58
|
|
|
* @var integer |
59
|
|
|
*/ |
60
|
|
|
private $numChecks = 0; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Amount of executed crypts |
64
|
|
|
* |
65
|
|
|
* @var integer |
66
|
|
|
*/ |
67
|
|
|
private $numCrypts = 0; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Amount of executed Syncs |
71
|
|
|
* |
72
|
|
|
* @var integer |
73
|
|
|
*/ |
74
|
|
|
private $numSyncs = 0; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Amount of executed Cleanups |
78
|
|
|
* |
79
|
|
|
* @var integer |
80
|
|
|
*/ |
81
|
|
|
private $numCleanups = 0; |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Console |
85
|
|
|
* |
86
|
|
|
* @var \SebastianBergmann\Environment\Console |
87
|
|
|
*/ |
88
|
|
|
private $console; |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* PHP Runtime |
92
|
|
|
* |
93
|
|
|
* @var \SebastianBergmann\Environment\Runtime |
94
|
|
|
*/ |
95
|
|
|
private $runtime; |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Returns an array of event names this subscriber wants to listen to. |
99
|
|
|
* |
100
|
|
|
* The array keys are event names and the value can be: |
101
|
|
|
* |
102
|
|
|
* * The method name to call (priority defaults to 0) |
103
|
|
|
* * An array composed of the method name to call and the priority |
104
|
|
|
* * An array of arrays composed of the method names to call and respective |
105
|
|
|
* priorities, or 0 if unset |
106
|
|
|
* |
107
|
|
|
* @return array The event names to listen to |
108
|
|
|
*/ |
109
|
3 |
|
public static function getSubscribedEvents(): array |
110
|
|
|
{ |
111
|
|
|
return [ |
112
|
3 |
|
'phpbu.debug' => 'onDebug', |
113
|
|
|
'phpbu.warning' => 'onWarning', |
114
|
|
|
'phpbu.app_start' => 'onPhpbuStart', |
115
|
|
|
'phpbu.backup_start' => 'onBackupStart', |
116
|
|
|
'phpbu.backup_failed' => 'onBackupFailed', |
117
|
|
|
'phpbu.backup_end' => 'onBackupEnd', |
118
|
|
|
'phpbu.check_start' => 'onCheckStart', |
119
|
|
|
'phpbu.check_failed' => 'onCheckFailed', |
120
|
|
|
'phpbu.check_end' => 'onCheckEnd', |
121
|
|
|
'phpbu.crypt_start' => 'onCryptStart', |
122
|
|
|
'phpbu.crypt_skipped' => 'onCryptSkipped', |
123
|
|
|
'phpbu.crypt_failed' => 'onCryptFailed', |
124
|
|
|
'phpbu.crypt_end' => 'onCryptEnd', |
125
|
|
|
'phpbu.sync_start' => 'onSyncStart', |
126
|
|
|
'phpbu.sync_skipped' => 'onSyncSkipped', |
127
|
|
|
'phpbu.sync_failed' => 'onSyncFailed', |
128
|
|
|
'phpbu.sync_end' => 'onSyncEnd', |
129
|
|
|
'phpbu.cleanup_start' => 'onCleanupStart', |
130
|
|
|
'phpbu.cleanup_skipped' => 'onCleanupSkipped', |
131
|
|
|
'phpbu.cleanup_failed' => 'onCleanupFailed', |
132
|
|
|
'phpbu.cleanup_end' => 'onCleanupEnd', |
133
|
|
|
'phpbu.app_end' => 'onPhpbuEnd', |
134
|
|
|
]; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Constructor |
139
|
|
|
* |
140
|
|
|
* @param bool $verbose |
141
|
|
|
* @param bool $colors |
142
|
|
|
* @param bool $debug |
143
|
|
|
* @throws \InvalidArgumentException |
144
|
29 |
|
*/ |
145
|
|
|
public function __construct(bool $verbose = false, bool $colors = false, bool $debug = false) |
146
|
29 |
|
{ |
147
|
29 |
|
$this->console = new Console; |
148
|
29 |
|
$this->runtime = new Runtime; |
149
|
29 |
|
$this->debug = $debug; |
150
|
29 |
|
$this->verbose = $verbose; |
151
|
29 |
|
$this->colors = $colors && $this->console->hasColorSupport(); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* phpbu start event. |
156
|
|
|
* |
157
|
|
|
* @param \phpbu\App\Event\App\Start $event |
158
|
2 |
|
*/ |
159
|
|
|
public function onPhpbuStart(Event\App\Start $event) |
160
|
2 |
|
{ |
161
|
2 |
|
$configuration = $event->getConfiguration(); |
162
|
1 |
|
if ($this->verbose) { |
163
|
1 |
|
$this->write( |
164
|
1 |
|
'Runtime: ' . $this->runtime->getNameWithVersion() . PHP_EOL . |
165
|
1 |
|
'Configuration: ' . $configuration->getFilename() . PHP_EOL . |
166
|
|
|
PHP_EOL |
167
|
|
|
); |
168
|
2 |
|
} |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Backup start event. |
173
|
|
|
* |
174
|
|
|
* @param \phpbu\App\Event\Backup\Start $event |
175
|
3 |
|
*/ |
176
|
|
|
public function onBackupStart(Event\Backup\Start $event) |
177
|
3 |
|
{ |
178
|
3 |
|
$this->numBackups++; |
179
|
2 |
|
if ($this->debug) { |
180
|
2 |
|
$backup = $event->getConfiguration(); |
181
|
|
|
$this->writeWithAsterisk('backup: [' . $backup->getSource()->type . '] '); |
182
|
3 |
|
} |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Backup failed event. |
187
|
|
|
* |
188
|
|
|
* @param \phpbu\App\Event\Backup\Failed $event |
189
|
1 |
|
*/ |
190
|
|
|
public function onBackupFailed(Event\Backup\Failed $event) |
|
|
|
|
191
|
1 |
|
{ |
192
|
1 |
|
if ($this->debug) { |
193
|
|
|
$this->writeWithColor('fg-white, bg-red, bold', 'failed' . PHP_EOL); |
194
|
1 |
|
} |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Backup end event. |
199
|
|
|
* |
200
|
|
|
* @param \phpbu\App\Event\Backup\End $event |
201
|
2 |
|
*/ |
202
|
|
|
public function onBackupEnd(Event\Backup\End $event) |
|
|
|
|
203
|
2 |
|
{ |
204
|
1 |
|
if ($this->debug) { |
205
|
|
|
$this->writeWithColor('fg-black, bg-green', 'ok' . PHP_EOL); |
206
|
2 |
|
} |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Check start event. |
211
|
|
|
* |
212
|
|
|
* @param \phpbu\App\Event\Check\Start $event |
213
|
3 |
|
*/ |
214
|
|
|
public function onCheckStart(Event\Check\Start $event) |
215
|
3 |
|
{ |
216
|
3 |
|
$this->numChecks++; |
217
|
2 |
|
if ($this->debug) { |
218
|
2 |
|
$check = $event->getConfiguration(); |
219
|
2 |
|
$this->writeWithAsterisk('check: [' . $check->type . '] '); |
220
|
|
|
$this->write('checking: [' . $check->value . '] '); |
221
|
3 |
|
} |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Check failed event. |
226
|
|
|
* |
227
|
|
|
* @param \phpbu\App\Event\Check\Failed $event |
228
|
1 |
|
*/ |
229
|
|
|
public function onCheckFailed(Event\Check\Failed $event) |
|
|
|
|
230
|
1 |
|
{ |
231
|
1 |
|
if ($this->debug) { |
232
|
|
|
$this->writeWithColor('fg-white, bg-red, bold', 'failed' . PHP_EOL); |
233
|
1 |
|
} |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* Check end event. |
238
|
|
|
* |
239
|
|
|
* @param \phpbu\App\Event\Check\End $event |
240
|
2 |
|
*/ |
241
|
|
|
public function onCheckEnd(Event\Check\End $event) |
|
|
|
|
242
|
2 |
|
{ |
243
|
1 |
|
if ($this->debug) { |
244
|
|
|
$this->writeWithColor('fg-black, bg-green', 'ok' . PHP_EOL); |
245
|
2 |
|
} |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* Crypt start event. |
250
|
|
|
* |
251
|
|
|
* @param \phpbu\App\Event\Crypt\Start $event |
252
|
4 |
|
*/ |
253
|
|
|
public function onCryptStart(Event\Crypt\Start $event) |
254
|
4 |
|
{ |
255
|
4 |
|
$this->numCrypts++; |
256
|
3 |
|
if ($this->debug) { |
257
|
3 |
|
$crypt = $event->getConfiguration(); |
258
|
|
|
$this->writeWithAsterisk('crypt: [' . $crypt->type . '] '); |
259
|
4 |
|
} |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* Crypt skipped event. |
264
|
|
|
* |
265
|
|
|
* @param \phpbu\App\Event\Crypt\Skipped $event |
266
|
1 |
|
*/ |
267
|
|
|
public function onCryptSkipped(Event\Crypt\Skipped $event) |
|
|
|
|
268
|
1 |
|
{ |
269
|
1 |
|
if ($this->debug) { |
270
|
|
|
$this->writeWithColor('fg-black, bg-yellow', 'skipped' . PHP_EOL); |
271
|
1 |
|
} |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* Crypt failed event. |
276
|
|
|
* |
277
|
|
|
* @param \phpbu\App\Event\Crypt\Failed $event |
278
|
1 |
|
*/ |
279
|
|
|
public function onCryptFailed(Event\Crypt\Failed $event) |
|
|
|
|
280
|
1 |
|
{ |
281
|
1 |
|
if ($this->debug) { |
282
|
|
|
$this->writeWithColor('fg-white, bg-red, bold', 'failed' . PHP_EOL); |
283
|
1 |
|
} |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* Crypt end event. |
288
|
|
|
* |
289
|
|
|
* @param \phpbu\App\Event\Crypt\End $event |
290
|
2 |
|
*/ |
291
|
|
|
public function onCryptEnd(Event\Crypt\End $event) |
|
|
|
|
292
|
2 |
|
{ |
293
|
1 |
|
if ($this->debug) { |
294
|
|
|
$this->writeWithColor('fg-black, bg-green', 'ok' . PHP_EOL); |
295
|
2 |
|
} |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* Sync start event. |
300
|
|
|
* |
301
|
|
|
* @param \phpbu\App\Event\Sync\Start $event |
302
|
4 |
|
*/ |
303
|
|
|
public function onSyncStart(Event\Sync\Start $event) |
304
|
4 |
|
{ |
305
|
4 |
|
$this->numSyncs++; |
306
|
3 |
|
if ($this->debug) { |
307
|
3 |
|
$sync = $event->getConfiguration(); |
308
|
|
|
$this->writeWithAsterisk('sync: [' . $sync->type . '] '); |
309
|
4 |
|
} |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
/** |
313
|
|
|
* Sync skipped event. |
314
|
|
|
* |
315
|
|
|
* @param \phpbu\App\Event\Sync\Skipped $event |
316
|
1 |
|
*/ |
317
|
|
|
public function onSyncSkipped(Event\Sync\Skipped $event) |
|
|
|
|
318
|
1 |
|
{ |
319
|
1 |
|
if ($this->debug) { |
320
|
|
|
$this->writeWithColor('fg-black, bg-yellow', 'skipped' . PHP_EOL); |
321
|
1 |
|
} |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
/** |
325
|
|
|
* Sync failed event. |
326
|
|
|
* |
327
|
|
|
* @param \phpbu\App\Event\Sync\Failed $event |
328
|
1 |
|
*/ |
329
|
|
|
public function onSyncFailed(Event\Sync\Failed $event) |
|
|
|
|
330
|
1 |
|
{ |
331
|
1 |
|
if ($this->debug) { |
332
|
|
|
$this->writeWithColor('fg-white, bg-red, bold', 'failed' . PHP_EOL); |
333
|
1 |
|
} |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
/** |
337
|
|
|
* Sync end event. |
338
|
|
|
* |
339
|
|
|
* @param \phpbu\App\Event\Sync\End $event |
340
|
2 |
|
*/ |
341
|
|
|
public function onSyncEnd(Event\Sync\End $event) |
|
|
|
|
342
|
2 |
|
{ |
343
|
1 |
|
if ($this->debug) { |
344
|
|
|
$this->writeWithColor('fg-black, bg-green', 'ok' . PHP_EOL); |
345
|
2 |
|
} |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
/** |
349
|
|
|
* Cleanup start event. |
350
|
|
|
* |
351
|
|
|
* @param \phpbu\App\Event\Cleanup\Start $event |
352
|
4 |
|
*/ |
353
|
|
|
public function onCleanupStart(Event\Cleanup\Start $event) |
354
|
4 |
|
{ |
355
|
4 |
|
$this->numCleanups++; |
356
|
3 |
|
if ($this->debug) { |
357
|
3 |
|
$cleanup = $event->getConfiguration(); |
358
|
|
|
$this->writeWithAsterisk('cleanup: [' . $cleanup->type . '] '); |
359
|
4 |
|
} |
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
/** |
363
|
|
|
* Cleanup skipped event. |
364
|
|
|
* |
365
|
|
|
* @param \phpbu\App\Event\Cleanup\Skipped $event |
366
|
1 |
|
*/ |
367
|
|
|
public function onCleanupSkipped(Event\Cleanup\Skipped $event) |
|
|
|
|
368
|
1 |
|
{ |
369
|
1 |
|
if ($this->debug) { |
370
|
|
|
$this->writeWithColor('fg-black, bg-yellow', 'skipped' . PHP_EOL); |
371
|
1 |
|
} |
372
|
|
|
} |
373
|
|
|
|
374
|
|
|
/** |
375
|
|
|
* Cleanup failed event. |
376
|
|
|
* |
377
|
|
|
* @param \phpbu\App\Event\Cleanup\Failed $event |
378
|
1 |
|
*/ |
379
|
|
|
public function onCleanupFailed(Event\Cleanup\Failed $event) |
|
|
|
|
380
|
1 |
|
{ |
381
|
1 |
|
if ($this->debug) { |
382
|
|
|
$this->writeWithColor('fg-white, bg-red, bold', 'failed' . PHP_EOL); |
383
|
1 |
|
} |
384
|
|
|
} |
385
|
|
|
|
386
|
|
|
/** |
387
|
|
|
* Cleanup end event. |
388
|
|
|
* |
389
|
|
|
* @param \phpbu\App\Event\Cleanup\End $event |
390
|
2 |
|
*/ |
391
|
|
|
public function onCleanupEnd(Event\Cleanup\End $event) |
|
|
|
|
392
|
2 |
|
{ |
393
|
1 |
|
if ($this->debug) { |
394
|
|
|
$this->writeWithColor('fg-black, bg-green', 'ok' . PHP_EOL); |
395
|
2 |
|
} |
396
|
|
|
} |
397
|
|
|
|
398
|
|
|
/** |
399
|
|
|
* Debugging. |
400
|
|
|
* |
401
|
|
|
* @param \phpbu\App\Event\Debug $event |
402
|
1 |
|
*/ |
403
|
|
|
public function onDebug(Event\Debug $event) |
404
|
1 |
|
{ |
405
|
1 |
|
if ($this->debug) { |
406
|
|
|
$this->write($event->getMessage() . PHP_EOL); |
407
|
1 |
|
} |
408
|
|
|
} |
409
|
|
|
|
410
|
|
|
/** |
411
|
|
|
* Warnings. |
412
|
|
|
* |
413
|
|
|
* @param \phpbu\App\Event\Warning $event |
414
|
1 |
|
*/ |
415
|
|
|
public function onWarning(Event\Warning $event) |
416
|
1 |
|
{ |
417
|
1 |
|
$this->writeWithColor('fg-black, bg-yellow', $event->getMessage() . PHP_EOL); |
418
|
1 |
|
} |
419
|
|
|
|
420
|
|
|
/** |
421
|
|
|
* phpbu end event. |
422
|
|
|
* |
423
|
|
|
* @param \phpbu\App\Event\App\End $event |
424
|
|
|
*/ |
425
|
5 |
|
public function onPhpbuEnd(Event\App\End $event) |
426
|
|
|
{ |
427
|
5 |
|
$result = $event->getResult(); |
428
|
5 |
|
$this->printResult($result); |
429
|
|
|
} |
430
|
5 |
|
|
431
|
3 |
|
/** |
432
|
3 |
|
* Prints a result summary. |
433
|
|
|
* |
434
|
|
|
* @param \phpbu\App\Result $result |
435
|
5 |
|
*/ |
436
|
5 |
|
public function printResult(Result $result) |
437
|
|
|
{ |
438
|
|
|
$this->printHeader(); |
439
|
|
|
$this->printErrors($result); |
440
|
|
|
|
441
|
5 |
|
if ($this->verbose) { |
442
|
|
|
foreach ($result->getBackups() as $backup) { |
443
|
5 |
|
$this->printBackupVerbose($backup); |
444
|
5 |
|
} |
445
|
|
|
} |
446
|
|
|
$this->printFooter($result); |
447
|
|
|
} |
448
|
|
|
|
449
|
|
|
/** |
450
|
|
|
* Prints the result header with memory usage info. |
451
|
5 |
|
*/ |
452
|
|
|
protected function printHeader() |
453
|
|
|
{ |
454
|
5 |
|
$this->write(Statistics::resourceUsage() . PHP_EOL . PHP_EOL); |
455
|
1 |
|
} |
456
|
1 |
|
|
457
|
1 |
|
/** |
458
|
1 |
|
* Print error information. |
459
|
1 |
|
* |
460
|
1 |
|
* @param \phpbu\App\Result $result |
461
|
1 |
|
*/ |
462
|
|
|
protected function printErrors(Result $result) |
463
|
|
|
{ |
464
|
|
|
/* @var $e \Exception */ |
465
|
5 |
|
foreach ($result->getErrors() as $e) { |
466
|
|
|
$this->write( |
467
|
|
|
sprintf( |
468
|
|
|
"Exception '%s' with message '%s'\nin %s:%d\n\n", |
469
|
|
|
get_class($e), |
470
|
|
|
$e->getMessage(), |
471
|
|
|
$e->getFile(), |
472
|
3 |
|
$e->getLine() |
473
|
|
|
) |
474
|
3 |
|
); |
475
|
3 |
|
} |
476
|
1 |
|
} |
477
|
1 |
|
|
478
|
1 |
|
/** |
479
|
|
|
* Prints verbose backup information. |
480
|
2 |
|
* |
481
|
1 |
|
* @param \phpbu\App\Result\Backup $backup |
482
|
1 |
|
*/ |
483
|
1 |
|
protected function printBackupVerbose(Result\Backup $backup) |
484
|
|
|
{ |
485
|
|
|
$this->write(sprintf('backup %s: ', $backup->getName())); |
486
|
1 |
|
if ($backup->allOk()) { |
487
|
1 |
|
$this->writeWithColor( |
488
|
1 |
|
'fg-green', |
489
|
|
|
'OK' |
490
|
|
|
); |
491
|
3 |
|
} elseif ($backup->okButSkipsOrFails()) { |
492
|
3 |
|
$this->writeWithColor( |
493
|
3 |
|
'fg-yellow', |
494
|
3 |
|
'OK, but skipped or failed Crypts, Syncs or Cleanups!' |
495
|
3 |
|
); |
496
|
3 |
|
} else { |
497
|
3 |
|
$this->writeWithColor( |
498
|
3 |
|
'fg-red', |
499
|
3 |
|
'FAILED' |
500
|
3 |
|
); |
501
|
3 |
|
} |
502
|
|
|
$chExecuted = str_pad($backup->checkCount(), 8, ' ', STR_PAD_LEFT); |
503
|
3 |
|
$chFailed = str_pad($backup->checkCountFailed(), 6, ' ', STR_PAD_LEFT); |
504
|
3 |
|
$crExecuted = str_pad($backup->cryptCount(), 8, ' ', STR_PAD_LEFT); |
505
|
3 |
|
$crSkipped = str_pad($backup->cryptCountSkipped(), 7, ' ', STR_PAD_LEFT); |
506
|
3 |
|
$crFailed = str_pad($backup->cryptCountFailed(), 6, ' ', STR_PAD_LEFT); |
507
|
3 |
|
$syExecuted = str_pad($backup->syncCount(), 8, ' ', STR_PAD_LEFT); |
508
|
3 |
|
$sySkipped = str_pad($backup->syncCountSkipped(), 7, ' ', STR_PAD_LEFT); |
509
|
3 |
|
$syFailed = str_pad($backup->syncCountFailed(), 6, ' ', STR_PAD_LEFT); |
510
|
|
|
$clExecuted = str_pad($backup->cleanupCount(), 8, ' ', STR_PAD_LEFT); |
511
|
3 |
|
$clSkipped = str_pad($backup->cleanupCountSkipped(), 7, ' ', STR_PAD_LEFT); |
512
|
3 |
|
$clFailed = str_pad($backup->cleanupCountFailed(), 6, ' ', STR_PAD_LEFT); |
513
|
|
|
|
514
|
|
|
$out = PHP_EOL . ' | executed | skipped | failed |' . PHP_EOL |
515
|
|
|
. '----------+----------+---------+--------+' . PHP_EOL |
516
|
|
|
. ' checks | ' . $chExecuted . ' | | ' . $chFailed . ' |' . PHP_EOL |
517
|
|
|
. ' crypts | ' . $crExecuted . ' | ' . $crSkipped . ' | ' . $crFailed . ' |' . PHP_EOL |
518
|
|
|
. ' syncs | ' . $syExecuted . ' | ' . $sySkipped . ' | ' . $syFailed . ' |' . PHP_EOL |
519
|
5 |
|
. ' cleanups | ' . $clExecuted . ' | ' . $clSkipped . ' | ' . $clFailed . ' |' . PHP_EOL |
520
|
|
|
. '----------+----------+---------+--------+' . PHP_EOL . PHP_EOL; |
521
|
5 |
|
|
522
|
2 |
|
$this->write($out); |
523
|
2 |
|
} |
524
|
2 |
|
|
525
|
|
|
/** |
526
|
3 |
|
* Prints 'OK' or 'FAILURE' footer. |
527
|
1 |
|
* |
528
|
1 |
|
* @param Result $result |
529
|
1 |
|
*/ |
530
|
1 |
|
protected function printFooter(Result $result) |
531
|
1 |
|
{ |
532
|
1 |
|
if (count($result->getBackups()) === 0) { |
533
|
1 |
|
$this->writeWithColor( |
534
|
1 |
|
'fg-yellow', |
535
|
1 |
|
'No backups executed!' |
536
|
1 |
|
); |
537
|
1 |
|
} elseif ($result->allOk()) { |
538
|
1 |
|
$this->writeWithColor( |
539
|
1 |
|
'fg-green', |
540
|
1 |
|
sprintf( |
541
|
|
|
'OK (%d %s, %d %s, %d %s, %d %s, %d %s)' . PHP_EOL, |
542
|
|
|
count($result->getBackups()), |
543
|
2 |
|
Util\Str::appendPluralS('backup', count($result->getBackups())), |
544
|
1 |
|
$this->numChecks, |
545
|
1 |
|
Util\Str::appendPluralS('check', $this->numChecks), |
546
|
1 |
|
$this->numCrypts, |
547
|
1 |
|
Util\Str::appendPluralS('crypt', $this->numCrypts), |
548
|
1 |
|
$this->numSyncs, |
549
|
1 |
|
Util\Str::appendPluralS('sync', $this->numSyncs), |
550
|
1 |
|
$this->numCleanups, |
551
|
1 |
|
Util\Str::appendPluralS('cleanup', $this->numCleanups) |
552
|
1 |
|
) |
553
|
1 |
|
); |
554
|
1 |
|
} elseif ($result->backupOkButSkipsOrFails()) { |
555
|
1 |
|
$this->writeWithColor( |
556
|
|
|
'fg-yellow', |
557
|
|
|
sprintf( |
558
|
|
|
"WARNING, skipped|failed Crypts, Syncs or Cleanups!" . PHP_EOL . |
559
|
1 |
|
'Backups: %d, Crypts: %d|%d, Syncs: %d|%d, Cleanups: %d|%d ' . PHP_EOL, |
560
|
1 |
|
count($result->getBackups()), |
561
|
1 |
|
$result->cryptsSkippedCount(), |
562
|
1 |
|
$result->cryptsFailedCount(), |
563
|
1 |
|
$result->syncsSkippedCount(), |
564
|
1 |
|
$result->syncsFailedCount(), |
565
|
1 |
|
$result->cleanupsSkippedCount(), |
566
|
1 |
|
$result->cleanupsFailedCount() |
567
|
1 |
|
) |
568
|
1 |
|
); |
569
|
1 |
|
} else { |
570
|
1 |
|
$this->writeWithColor( |
571
|
1 |
|
'fg-red', |
572
|
1 |
|
sprintf( |
573
|
|
|
"FAILURE!" . PHP_EOL . |
574
|
|
|
'Backups: %d, ' |
575
|
|
|
. 'failed Checks: %d, ' |
576
|
5 |
|
. 'failed Crypts: %d, ' |
577
|
|
|
. 'failed Syncs: %d, ' |
578
|
|
|
. 'failed Cleanups: %d.' . PHP_EOL, |
579
|
|
|
count($result->getBackups()), |
580
|
|
|
$result->checksFailedCount(), |
581
|
|
|
$result->cryptsFailedCount(), |
582
|
|
|
$result->syncsFailedCount(), |
583
|
|
|
$result->cleanupsFailedCount() |
584
|
|
|
) |
585
|
18 |
|
); |
586
|
|
|
} |
587
|
18 |
|
} |
588
|
1 |
|
|
589
|
|
|
/** |
590
|
18 |
|
* Writes a buffer out with a color sequence if colors are enabled. |
591
|
18 |
|
* |
592
|
|
|
* @author Sebastian Bergmann <[email protected]> |
593
|
|
|
* @param string $color |
594
|
|
|
* @param string $buffer |
595
|
|
|
*/ |
596
|
|
|
protected function writeWithColor($color, $buffer) |
597
|
|
|
{ |
598
|
13 |
|
if ($this->colors) { |
599
|
|
|
$buffer = Util\Cli::formatWithColor($color, $buffer); |
600
|
13 |
|
} |
601
|
13 |
|
$this->write($buffer . PHP_EOL); |
602
|
|
|
} |
603
|
|
|
|
604
|
|
|
/** |
605
|
|
|
* Writes a buffer with Ansible like asterisk decoration. |
606
|
|
|
* |
607
|
|
|
* @param string $buffer |
608
|
20 |
|
*/ |
609
|
|
|
protected function writeWithAsterisk($buffer) |
610
|
20 |
|
{ |
611
|
|
|
$this->write(Util\Cli::formatWithAsterisk($buffer)); |
612
|
|
|
} |
613
|
20 |
|
|
614
|
20 |
|
/** |
615
|
|
|
* Writes a buffer. |
616
|
|
|
* |
617
|
|
|
* @param string $buffer |
618
|
|
|
*/ |
619
|
|
|
public function write($buffer) |
620
|
|
|
{ |
621
|
|
|
if (PHP_SAPI != 'cli') { |
622
|
|
|
$buffer = htmlspecialchars($buffer); |
623
|
|
|
} |
624
|
|
|
echo $buffer; |
625
|
|
|
} |
626
|
|
|
} |
627
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.