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\Config; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class Action |
18
|
|
|
* |
19
|
|
|
* @package CaptainHook |
20
|
|
|
* @author Sebastian Feldmann <[email protected]> |
21
|
|
|
* @link https://github.com/captainhook-git/captainhook |
22
|
|
|
* @since Class available since Release 0.9.0 |
23
|
|
|
*/ |
24
|
|
|
class Action |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* Action php class, php static method, or cli script |
28
|
|
|
* |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
private string $action; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Map of options name => value |
35
|
|
|
* |
36
|
|
|
* @var \CaptainHook\App\Config\Options |
37
|
|
|
*/ |
38
|
|
|
private Options $options; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* List of action conditions |
42
|
|
|
* |
43
|
|
|
* @var \CaptainHook\App\Config\Condition[] |
44
|
|
|
*/ |
45
|
|
|
private array $conditions = []; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Action settings |
49
|
|
|
* |
50
|
|
|
* @var array<string, mixed> |
51
|
|
|
*/ |
52
|
|
|
private array $settings = []; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* List of available settings |
56
|
|
|
* |
57
|
|
|
* @var string[] |
58
|
|
|
*/ |
59
|
|
|
private static array $availableSettings = [ |
60
|
|
|
Config::SETTING_ALLOW_FAILURE, |
61
|
|
|
Config::SETTING_LABEL |
62
|
|
|
]; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Indicates if an action config was included from another file |
66
|
|
|
* |
67
|
|
|
* @var bool |
68
|
|
|
*/ |
69
|
|
|
private bool $isIncluded = false; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Action constructor |
73
|
|
|
* |
74
|
|
|
* @param string $action |
75
|
|
|
* @param array<string, mixed> $options |
76
|
|
|
* @param array<string, mixed> $conditions |
77
|
|
|
* @param array<string, mixed> $settings |
78
|
|
|
*/ |
79
|
144 |
|
public function __construct(string $action, array $options = [], array $conditions = [], array $settings = []) |
80
|
|
|
{ |
81
|
144 |
|
$this->action = $action; |
82
|
144 |
|
$this->setupOptions($options); |
83
|
144 |
|
$this->setupConditions($conditions); |
84
|
144 |
|
$this->setupSettings($settings); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Setup options |
89
|
|
|
* |
90
|
|
|
* @param array<string, mixed> $options |
91
|
|
|
*/ |
92
|
144 |
|
private function setupOptions(array $options): void |
93
|
|
|
{ |
94
|
144 |
|
$this->options = new Options($options); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Setup action conditions |
99
|
|
|
* |
100
|
|
|
* @param array<string, array<string, mixed>> $conditions |
101
|
|
|
*/ |
102
|
144 |
|
private function setupConditions(array $conditions): void |
103
|
|
|
{ |
104
|
144 |
|
foreach ($conditions as $condition) { |
105
|
8 |
|
$this->conditions[] = new Condition($condition['exec'], $condition['args'] ?? []); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Setting up the action settings |
111
|
|
|
* |
112
|
|
|
* @param array<string, mixed> $settings |
113
|
|
|
* @return void |
114
|
|
|
*/ |
115
|
144 |
|
private function setupSettings(array $settings): void |
116
|
|
|
{ |
117
|
144 |
|
foreach (self::$availableSettings as $setting) { |
118
|
144 |
|
if (isset($settings[$setting])) { |
119
|
9 |
|
$this->settings[$setting] = $settings[$setting]; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Marks a action config as included |
126
|
|
|
* |
127
|
|
|
* @return void |
128
|
|
|
*/ |
129
|
7 |
|
public function markIncluded(): void |
130
|
|
|
{ |
131
|
7 |
|
$this->isIncluded = true; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Check if an action config was included |
136
|
|
|
* |
137
|
|
|
* @return bool |
138
|
|
|
*/ |
139
|
8 |
|
public function isIncluded(): bool |
140
|
|
|
{ |
141
|
8 |
|
return $this->isIncluded; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Indicates if the action can fail without stopping the git operation |
146
|
|
|
* |
147
|
|
|
* @param bool $default |
148
|
|
|
* @return bool |
149
|
|
|
*/ |
150
|
10 |
|
public function isFailureAllowed(bool $default = false): bool |
151
|
|
|
{ |
152
|
10 |
|
return (bool) ($this->settings[Config::SETTING_ALLOW_FAILURE] ?? $default); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Return the label or the action if no label is set |
157
|
|
|
* |
158
|
|
|
* @return string |
159
|
|
|
*/ |
160
|
10 |
|
public function getLabel(): string |
161
|
|
|
{ |
162
|
10 |
|
return (string) ($this->settings[Config::SETTING_LABEL] ?? $this->getAction()); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Action getter |
167
|
|
|
* |
168
|
|
|
* @return string |
169
|
|
|
*/ |
170
|
15 |
|
public function getAction(): string |
171
|
|
|
{ |
172
|
15 |
|
return $this->action; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Return option map |
177
|
|
|
* |
178
|
|
|
* @return \CaptainHook\App\Config\Options |
179
|
|
|
*/ |
180
|
68 |
|
public function getOptions(): Options |
181
|
|
|
{ |
182
|
68 |
|
return $this->options; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Return condition configurations |
187
|
|
|
* |
188
|
|
|
* @return \CaptainHook\App\Config\Condition[] |
189
|
|
|
*/ |
190
|
11 |
|
public function getConditions(): array |
191
|
|
|
{ |
192
|
11 |
|
return $this->conditions; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Return config data |
197
|
|
|
* |
198
|
|
|
* @return array<string, mixed> |
199
|
|
|
*/ |
200
|
11 |
|
public function getJsonData(): array |
201
|
|
|
{ |
202
|
11 |
|
$data = [ |
203
|
11 |
|
'action' => $this->action |
204
|
11 |
|
]; |
205
|
|
|
|
206
|
11 |
|
$options = $this->options->getAll(); |
207
|
11 |
|
if (!empty($options)) { |
208
|
1 |
|
$data['options'] = $options; |
209
|
|
|
} |
210
|
|
|
|
211
|
11 |
|
$conditions = $this->getConditionJsonData(); |
212
|
11 |
|
if (!empty($conditions)) { |
213
|
1 |
|
$data['conditions'] = $conditions; |
214
|
|
|
} |
215
|
|
|
|
216
|
11 |
|
if (!empty($this->settings)) { |
217
|
1 |
|
$data['config'] = $this->settings; |
218
|
|
|
} |
219
|
|
|
|
220
|
11 |
|
return $data; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Return conditions json data |
225
|
|
|
* |
226
|
|
|
* @return array<int, mixed> |
227
|
|
|
*/ |
228
|
11 |
|
private function getConditionJsonData(): array |
229
|
|
|
{ |
230
|
11 |
|
$json = []; |
231
|
11 |
|
foreach ($this->conditions as $condition) { |
232
|
1 |
|
$json[] = $condition->getJsonData(); |
233
|
|
|
} |
234
|
11 |
|
return $json; |
235
|
|
|
} |
236
|
|
|
} |
237
|
|
|
|