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