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