|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of CaptainHook. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Sebastian Feldmann <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
namespace CaptainHook\App\Config; |
|
11
|
|
|
|
|
12
|
|
|
use CaptainHook\App\Hook\Util as HookUtil; |
|
13
|
|
|
use CaptainHook\App\Config; |
|
14
|
|
|
use CaptainHook\App\Storage\File\Json; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Class Util |
|
18
|
|
|
* |
|
19
|
|
|
* @package CaptainHook |
|
20
|
|
|
* @author Sebastian Feldmann <[email protected]> |
|
21
|
|
|
* @link https://github.com/captainhookphp/captainhook |
|
22
|
|
|
* @since Class available since Release 1.0.3 |
|
23
|
|
|
*/ |
|
24
|
|
|
abstract class Util |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* Validate a configuration |
|
28
|
|
|
* |
|
29
|
|
|
* @param array $json |
|
30
|
|
|
* @return void |
|
31
|
|
|
* @throws \RuntimeException |
|
32
|
|
|
*/ |
|
33
|
24 |
|
public static function validateJsonConfiguration(array $json) : void |
|
34
|
|
|
{ |
|
35
|
24 |
|
foreach (HookUtil::getValidHooks() as $hook => $class) { |
|
36
|
24 |
|
if (isset($json[$hook])) { |
|
37
|
23 |
|
self::validateHookConfig($json[$hook]); |
|
38
|
|
|
} |
|
39
|
|
|
} |
|
40
|
19 |
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Validate a hook configuration |
|
44
|
|
|
* |
|
45
|
|
|
* @param array $json |
|
46
|
|
|
* @return void |
|
47
|
|
|
* @throws \RuntimeException |
|
48
|
|
|
*/ |
|
49
|
23 |
|
public static function validateHookConfig(array $json) : void |
|
50
|
|
|
{ |
|
51
|
23 |
|
if (!self::keysExist(['enabled', 'actions'], $json)) { |
|
52
|
2 |
|
throw new \RuntimeException('Config error: invalid hook configuration'); |
|
53
|
|
|
} |
|
54
|
21 |
|
if (!is_array($json['actions'])) { |
|
55
|
1 |
|
throw new \RuntimeException('Config error: \'actions\' must be an array'); |
|
56
|
|
|
} |
|
57
|
20 |
|
self::validateActionsConfig($json['actions']); |
|
58
|
18 |
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Validate a list of action configurations |
|
62
|
|
|
* |
|
63
|
|
|
* @param array $json |
|
64
|
|
|
* @return void |
|
65
|
|
|
* @throws \RuntimeException |
|
66
|
|
|
*/ |
|
67
|
20 |
|
public static function validateActionsConfig(array $json) : void |
|
68
|
|
|
{ |
|
69
|
20 |
|
foreach ($json as $action) { |
|
70
|
14 |
|
if (!self::keysExist(['action'], $action)) { |
|
71
|
1 |
|
throw new \RuntimeException('Config error: \'action\' missing'); |
|
72
|
|
|
} |
|
73
|
13 |
|
if (empty($action['action'])) { |
|
74
|
1 |
|
throw new \RuntimeException('Config error: \'action\' can\'t be empty'); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
18 |
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Write the config to disk |
|
81
|
|
|
* |
|
82
|
|
|
* @param \CaptainHook\App\Config $config |
|
83
|
|
|
* @return void |
|
84
|
|
|
*/ |
|
85
|
6 |
|
public static function writeToDisk(Config $config) : void |
|
86
|
|
|
{ |
|
87
|
6 |
|
$filePath = $config->getPath(); |
|
88
|
6 |
|
$file = new Json($filePath); |
|
89
|
6 |
|
$file->write($config->getJsonData()); |
|
90
|
6 |
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Does an array have the expected keys |
|
94
|
|
|
* |
|
95
|
|
|
* @param array $keys |
|
96
|
|
|
* @param array $subject |
|
97
|
|
|
* @return bool |
|
98
|
|
|
*/ |
|
99
|
23 |
|
private static function keysExist(array $keys, array $subject) : bool |
|
100
|
|
|
{ |
|
101
|
23 |
|
foreach ($keys as $key) { |
|
102
|
23 |
|
if (!isset($subject[$key])) { |
|
103
|
3 |
|
return false; |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
21 |
|
return true; |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|