|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace TheAentMachine\Service; |
|
4
|
|
|
|
|
5
|
|
|
use Opis\JsonSchema\ValidationError; |
|
6
|
|
|
use Opis\JsonSchema\Validator; |
|
7
|
|
|
use TheAentMachine\Service\Exception\ServiceException; |
|
8
|
|
|
|
|
9
|
|
|
class Service implements \JsonSerializable |
|
10
|
|
|
{ |
|
11
|
|
|
/** @var string */ |
|
12
|
|
|
protected $serviceName = ''; |
|
13
|
|
|
/** @var string */ |
|
14
|
|
|
protected $image = ''; |
|
15
|
|
|
/** @var int[] */ |
|
16
|
|
|
protected $internalPorts = []; |
|
17
|
|
|
/** @var string[] */ |
|
18
|
|
|
protected $dependsOn = []; |
|
19
|
|
|
/** @var mixed[] */ |
|
20
|
|
|
protected $ports = []; |
|
21
|
|
|
/** @var mixed[] */ |
|
22
|
|
|
protected $labels = []; |
|
23
|
|
|
/** @var mixed[] */ |
|
24
|
|
|
protected $environment = []; |
|
25
|
|
|
/** @var mixed[] */ |
|
26
|
|
|
protected $volumes = []; |
|
27
|
|
|
/** @var \stdClass */ |
|
28
|
|
|
protected $validatorSchema; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Service constructor. |
|
32
|
|
|
*/ |
|
33
|
|
|
public function __construct() |
|
34
|
|
|
{ |
|
35
|
|
|
$this->validatorSchema = json_decode(file_get_contents(__DIR__ . '/ServiceJsonSchema.json'), false); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param mixed[] $payload |
|
40
|
|
|
* @return Service |
|
41
|
|
|
* @throws ServiceException |
|
42
|
|
|
*/ |
|
43
|
|
|
public static function parsePayload(array $payload): Service |
|
44
|
|
|
{ |
|
45
|
|
|
$service = new Service(); |
|
46
|
|
|
$service->checkValidity($payload); |
|
47
|
|
|
$service->serviceName = $payload['serviceName'] ?? ''; |
|
48
|
|
|
$s = $payload['service'] ?? array(); |
|
49
|
|
|
if (!empty($s)) { |
|
50
|
|
|
$service->image = $s['image'] ?? ''; |
|
51
|
|
|
$service->internalPorts = $s['internalPorts'] ?? array(); |
|
52
|
|
|
$service->dependsOn = $s['dependsOn'] ?? array(); |
|
53
|
|
|
$service->ports = $s['ports'] ?? array(); |
|
54
|
|
|
$service->labels = $s['labels'] ?? array(); |
|
55
|
|
|
$service->environment = $s['environment'] ?? array(); |
|
56
|
|
|
$service->volumes = $s['volumes'] ?? array(); |
|
57
|
|
|
} |
|
58
|
|
|
return $service; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Specify data which should be serialized to JSON |
|
63
|
|
|
* @link http://php.net/manual/en/jsonserializable.jsonserialize.php |
|
64
|
|
|
* @return array data which can be serialized by <b>json_encode</b>, |
|
65
|
|
|
* which is a value of any type other than a resource. |
|
66
|
|
|
* @since 5.4.0 |
|
67
|
|
|
* @throws ServiceException |
|
68
|
|
|
*/ |
|
69
|
|
|
public function jsonSerialize(): array |
|
70
|
|
|
{ |
|
71
|
|
|
$array = self::arrayFilterRec(array( |
|
72
|
|
|
'serviceName' => $this->serviceName, |
|
73
|
|
|
'service' => array( |
|
74
|
|
|
'image' => $this->image, |
|
75
|
|
|
'internalPorts' => $this->internalPorts, |
|
76
|
|
|
'dependsOn' => $this->dependsOn, |
|
77
|
|
|
'ports' => $this->ports, |
|
78
|
|
|
'labels' => $this->labels, |
|
79
|
|
|
'environment' => $this->environment, |
|
80
|
|
|
'volumes' => $this->volumes, |
|
81
|
|
|
) |
|
82
|
|
|
)); |
|
83
|
|
|
$this->checkValidity($array); |
|
84
|
|
|
return $array; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @param \stdClass|array|string $data |
|
89
|
|
|
* @return bool |
|
90
|
|
|
* @throws ServiceException |
|
91
|
|
|
*/ |
|
92
|
|
|
private function checkValidity($data): bool |
|
93
|
|
|
{ |
|
94
|
|
|
if (\is_array($data)) { |
|
95
|
|
|
$data = json_decode(json_encode($data), false); |
|
96
|
|
|
} |
|
97
|
|
|
$validator = new Validator(); |
|
98
|
|
|
$result = $validator->dataValidation($data, $this->validatorSchema); |
|
99
|
|
|
if (!$result->isValid()) { |
|
100
|
|
|
/** @var ValidationError $vError */ |
|
101
|
|
|
$vError = $result->getFirstError(); |
|
102
|
|
|
throw ServiceException::invalidServiceData($vError); |
|
103
|
|
|
} |
|
104
|
|
|
return $result->isValid(); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Delete all key/value pairs with empty value by recursively using array_filter |
|
109
|
|
|
* @param array $input |
|
110
|
|
|
* @return mixed[] array |
|
111
|
|
|
*/ |
|
112
|
|
|
private static function arrayFilterRec(array $input): array |
|
113
|
|
|
{ |
|
114
|
|
|
foreach ($input as &$value) { |
|
115
|
|
|
if (\is_array($value)) { |
|
116
|
|
|
$value = self::arrayFilterRec($value); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
return array_filter($input); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @return string |
|
124
|
|
|
*/ |
|
125
|
|
|
public function getServiceName(): string |
|
126
|
|
|
{ |
|
127
|
|
|
return $this->serviceName; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* @return string |
|
132
|
|
|
*/ |
|
133
|
|
|
public function getImage(): string |
|
134
|
|
|
{ |
|
135
|
|
|
return $this->image; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* @return int[] |
|
140
|
|
|
*/ |
|
141
|
|
|
public function getInternalPorts(): array |
|
142
|
|
|
{ |
|
143
|
|
|
return $this->internalPorts; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* @return string[] |
|
148
|
|
|
*/ |
|
149
|
|
|
public function getDependsOn(): array |
|
150
|
|
|
{ |
|
151
|
|
|
return $this->dependsOn; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* @return mixed[] |
|
156
|
|
|
*/ |
|
157
|
|
|
public function getPorts(): array |
|
158
|
|
|
{ |
|
159
|
|
|
return $this->ports; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* @return mixed[] |
|
164
|
|
|
*/ |
|
165
|
|
|
public function getLabels(): array |
|
166
|
|
|
{ |
|
167
|
|
|
return $this->labels; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* @return mixed[] |
|
172
|
|
|
*/ |
|
173
|
|
|
public function getEnvironment(): array |
|
174
|
|
|
{ |
|
175
|
|
|
return $this->environment; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* @return mixed[] |
|
180
|
|
|
*/ |
|
181
|
|
|
public function getVolumes(): array |
|
182
|
|
|
{ |
|
183
|
|
|
return $this->volumes; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* @param int $internalPort |
|
188
|
|
|
*/ |
|
189
|
|
|
public function addInternalPort(int $internalPort): void |
|
190
|
|
|
{ |
|
191
|
|
|
$this->internalPorts[] = $internalPort; |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* @param string $dependsOn |
|
196
|
|
|
*/ |
|
197
|
|
|
public function addDependsOn(string $dependsOn): void |
|
198
|
|
|
{ |
|
199
|
|
|
$this->dependsOn[] = $dependsOn; |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* @param mixed $port |
|
204
|
|
|
*/ |
|
205
|
|
|
public function addPort(array $port): void |
|
206
|
|
|
{ |
|
207
|
|
|
$this->ports[] = array( |
|
208
|
|
|
'source' => $port['source'] ?? '', //optional |
|
209
|
|
|
'target' => $port['target'], |
|
210
|
|
|
); |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
/** |
|
214
|
|
|
* @param mixed $label |
|
215
|
|
|
*/ |
|
216
|
|
|
public function addLabel(array $label): void |
|
217
|
|
|
{ |
|
218
|
|
|
$this->labels[] = array( |
|
219
|
|
|
'key' => $label['key'], |
|
220
|
|
|
'values' => $label['values'] ?? '', //optional |
|
221
|
|
|
); |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
/** |
|
225
|
|
|
* @param mixed $environment |
|
226
|
|
|
*/ |
|
227
|
|
|
public function addEnvironment(array $environment): void |
|
228
|
|
|
{ |
|
229
|
|
|
$this->environment[] = array( |
|
230
|
|
|
'key' => $environment['key'], |
|
231
|
|
|
'values' => $environment['values'] ?? '', //optional |
|
232
|
|
|
); |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
/** |
|
236
|
|
|
* @param mixed $volume |
|
237
|
|
|
*/ |
|
238
|
|
|
public function addVolume(array $volume): void |
|
239
|
|
|
{ |
|
240
|
|
|
$this->volumes[] = array( |
|
241
|
|
|
'type' => $volume['type'], |
|
242
|
|
|
'source' => $volume['source'], |
|
243
|
|
|
'target' => $volume['target'], |
|
244
|
|
|
'readOnly' => $volume['readOnly'] ?? null, |
|
245
|
|
|
); |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
/** |
|
249
|
|
|
* @param string $serviceName |
|
250
|
|
|
* @return Service |
|
251
|
|
|
*/ |
|
252
|
|
|
public function setServiceName(string $serviceName): Service |
|
253
|
|
|
{ |
|
254
|
|
|
$this->serviceName = $serviceName; |
|
255
|
|
|
return $this; |
|
256
|
|
|
} |
|
257
|
|
|
|
|
258
|
|
|
/** |
|
259
|
|
|
* @param string $image |
|
260
|
|
|
* @return Service |
|
261
|
|
|
*/ |
|
262
|
|
|
public function setImage(string $image): Service |
|
263
|
|
|
{ |
|
264
|
|
|
$this->image = $image; |
|
265
|
|
|
return $this; |
|
266
|
|
|
} |
|
267
|
|
|
|
|
268
|
|
|
/** |
|
269
|
|
|
* @param int[] $internalPorts |
|
270
|
|
|
* @return Service |
|
271
|
|
|
*/ |
|
272
|
|
|
public function setInternalPorts(array $internalPorts): Service |
|
273
|
|
|
{ |
|
274
|
|
|
$this->internalPorts = $internalPorts; |
|
275
|
|
|
return $this; |
|
276
|
|
|
} |
|
277
|
|
|
|
|
278
|
|
|
/** |
|
279
|
|
|
* @param string[] $dependsOn |
|
280
|
|
|
* @return Service |
|
281
|
|
|
*/ |
|
282
|
|
|
public function setDependsOn(array $dependsOn): Service |
|
283
|
|
|
{ |
|
284
|
|
|
$this->dependsOn = $dependsOn; |
|
285
|
|
|
return $this; |
|
286
|
|
|
} |
|
287
|
|
|
|
|
288
|
|
|
/** |
|
289
|
|
|
* @param mixed[] $ports |
|
290
|
|
|
* @return Service |
|
291
|
|
|
*/ |
|
292
|
|
|
public function setPorts(array $ports): Service |
|
293
|
|
|
{ |
|
294
|
|
|
$this->ports = $ports; |
|
295
|
|
|
return $this; |
|
296
|
|
|
} |
|
297
|
|
|
|
|
298
|
|
|
/** |
|
299
|
|
|
* @param mixed[] $labels |
|
300
|
|
|
* @return Service |
|
301
|
|
|
*/ |
|
302
|
|
|
public function setLabels(array $labels): Service |
|
303
|
|
|
{ |
|
304
|
|
|
$this->labels = $labels; |
|
305
|
|
|
return $this; |
|
306
|
|
|
} |
|
307
|
|
|
|
|
308
|
|
|
/** |
|
309
|
|
|
* @param mixed[] $environment |
|
310
|
|
|
* @return Service |
|
311
|
|
|
*/ |
|
312
|
|
|
public function setEnvironment(array $environment): Service |
|
313
|
|
|
{ |
|
314
|
|
|
$this->environment = $environment; |
|
315
|
|
|
return $this; |
|
316
|
|
|
} |
|
317
|
|
|
|
|
318
|
|
|
/** |
|
319
|
|
|
* @param mixed[] $volumes |
|
320
|
|
|
* @return Service |
|
321
|
|
|
*/ |
|
322
|
|
|
public function setVolumes(array $volumes): Service |
|
323
|
|
|
{ |
|
324
|
|
|
$this->volumes = $volumes; |
|
325
|
|
|
return $this; |
|
326
|
|
|
} |
|
327
|
|
|
|
|
328
|
|
|
} |
|
329
|
|
|
|