-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathFoxxHandler.php
417 lines (383 loc) · 15.5 KB
/
FoxxHandler.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
<?php
/**
* ArangoDB PHP client: foxx upload
*
* @package ArangoDBClient
* @author Tom Regner <[email protected]>
* @copyright Copyright 2016, triagens GmbH, Cologne, Germany
* @copyright Copyright 2022, ArangoDB GmbH, Cologne, Germany
*/
namespace ArangoDBClient;
use ArangoDBClient\HttpHelper;
use ArangoDBClient\Urls;
use ArangoDBClient\UrlHelper;
use ArangoDBClient\ServerException;
use ArangoDBClient\ClientException;
use ArangoDBClient\Multipart;
/**
* A class for uploading Foxx application zips to a database
*
* @package ArangoDBClient
* @since 3.1
*/
class FoxxHandler extends Handler
{
/* Field names */
/**
* Name of the dependencie field
*/
const FOXX_APP_DEPENDENCIES = 'dependencies';
/**
* Name of the configuration field
*/
const FOXX_APP_CONFIGURATION = 'configuration';
/* Url parameter names */
/**
* Name of the setup parameter
*/
const FOXX_APP_SETUP = 'setup';
/**
* Name of the teardown parameter
*/
const FOXX_APP_TEARDOWN = 'teardown';
/**
* Name of the legacy parameter
*/
const FOXX_APP_LEGACY = 'legacy';
/**
* Name of development parameter
*/
const FOXX_APP_DEVELOPMENT = 'development';
/**
* Name of the force parameter
*/
const FOXX_APP_FORCE = 'force';
/**
* Name of the exclude system parameter
*/
const FOXX_APP_EXCLUDE_SYSTEM = 'excludeSystem';
/* Default values and custom consts */
/**
* Default values for the options above
*/
const FOXX_APP_DEFAULT_PARAMS = [
self::FOXX_APP_DEVELOPMENT => false,
self::FOXX_APP_LEGACY => false,
self::FOXX_APP_SETUP => true,
self::FOXX_APP_TEARDOWN => true,
self::FOXX_APP_FORCE => false,
self::FOXX_APP_EXCLUDE_SYSTEM => true,
];
/**
* Multipart boundary for foxx app api calls
*/
const FOXX_APP_MIME_BOUNDARY = 'XXXfoxxhandlerXXX';
/**
* Upload and install a foxx app.
*
* @throws ClientException
*
* @param string $localZip - the path to the local foxx-app zip-archive to upload/install
* @param string $mountPoint - the mount-point for the app, must begin with a '/'
* @param array $options - You can pass configuration (array), dependencies (array) and control options
* (bool) legacy, development, setup, teardown
* Defaults are
* - configuration empty
* - dependencies empty
* - control options: see FoxxHandler::FOXX_APP_DEFAULT_PARAMS
*
* @return array - the server response
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
* @deprecated Use installService()
*/
public function installFoxxZip($localZip, $mountPoint, array $options = [])
{
$this->installService($localZip, $mountPoint, $options);
}
/**
* Upload and install a foxx app.
*
* @throws ClientException
*
* @param string $localZip - the path to the local foxx-app zip-archive to upload/install
* @param string $mountPoint - the mount-point for the app, must begin with a '/'
* @param array $options - You can pass configuration (array), dependencies (array) and control options
* (bool) legacy, development, setup, teardown
* Defaults are
* - configuration empty
* - dependencies empty
* - control options: see FoxxHandler::FOXX_APP_DEFAULT_PARAMS
*
* @return array - the server response
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function installService($localZip, $mountPoint, array $options = [])
{
return $this->zipAction($localZip, $mountPoint, $options, false, false);
}
/**
* Upload a zip amd upgrade an existing service.
*
* @throws ClientException
*
* @param string $localZip - the path to the local foxx-app zip-archive to upload/install
* @param string $mountPoint - the mount-point for the app, must begin with a '/'
* @param array $options - You can pass configuration (array), dependencies (array) and control options
* (bool) legacy, development, setup, teardown, force
* Defaults are
* - configuration empty
* - dependencies empty
* - control options: see FoxxHandler::FOXX_APP_DEFAULT_PARAMS
*
* @return array - the server response
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function upgradeService($localZip, $mountPoint, array $options = [])
{
return $this->zipAction($localZip, $mountPoint, $options, true, false);
}
/**
* Upload a zip amd replace an existing service.
*
* @throws ClientException
*
* @param string $localZip - the path to the local foxx-app zip-archive to upload/install
* @param string $mountPoint - the mount-point for the app, must begin with a '/'
* @param array $options - You can pass configuration (array), dependencies (array) and control options
* (bool) legacy, development, setup, teardown, force
* Defaults are
* - configuration empty
* - dependencies empty
* - control options: see FoxxHandler::FOXX_APP_DEFAULT_PARAMS
*
* @return array - the server response
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function replaceService($localZip, $mountPoint, array $options = [])
{
return $this->zipAction($localZip, $mountPoint, $options, false, true);
}
/**
* Install/Upgrade/Replace a foxx service via zip-bundle
*
* @throws ClientException
*
* @param string $localZip - the path to the local foxx-app zip-archive to upload/install
* @param string $mountPoint - the mount-point for the app, must begin with a '/'
* @param array $options - You can pass configuration (array), dependencies (array) and control options
* @param bool $upgrade - If true, try to upgrade an existing service, if this is true, $replace will be ignored
* @param bool $replace - if true replace an existing service; if $upgrade is true, this will be ignored
*/
protected function zipAction($localZip, $mountPoint, array $options, bool $upgrade = false, bool $replace = false)
{
if (!file_exists($localZip)) {
throw new ClientException("Foxx-Zip {$localZip} does not exist (or file is unreadable).");
}
$dependencies = [];
if (!empty($options[self::FOXX_APP_DEPENDENCIES])) {
$dependencies = $options[self::FOXX_APP_DEPENDENCIES];
unset($options[self::FOXX_APP_DEPENDENCIES]);
}
$configuration = [];
if (!empty($options[self::FOXX_APP_CONFIGURATION])) {
$configuration = $options[self::FOXX_APP_CONFIGURATION];
unset($options[self::FOXX_APP_CONFIGURATION]);
}
try {
$post = file_get_contents($localZip);
$bodyParts = [
new Multipart([
Multipart::MULTIPART_NAME => 'configuration',
Multipart::MULTIPART_VALUE => json_encode($configuration, JSON_FORCE_OBJECT),
]),
new Multipart([
Multipart::MULTIPART_NAME => 'dependencies',
Multipart::MULTIPART_VALUE => json_encode($dependencies, JSON_FORCE_OBJECT),
]),
new Multipart([
Multipart::MULTIPART_NAME => 'source',
Multipart::MULTIPART_VALUE => $post,
Multipart::MULTIPART_TRANSFER_ENCODING => Multipart::MULTIPART_ENCODING_BINARY,
Multipart::MULTIPART_FILENAME => basename($localZip),
]),
];
$bodyStr = HttpHelper::buildMultiPartFormDataBody(self::FOXX_APP_MIME_BOUNDARY, ...$bodyParts);
$optionNames = [self::FOXX_APP_LEGACY, self::FOXX_APP_DEVELOPMENT, self::FOXX_APP_SETUP];
if (true === $upgrade|| true === $replace) {
$optionNames[] = self::FOXX_APP_FORCE;
}
$params = static::buildParameterArray($optionNames, $mountPoint, $options);
if (true === $upgrade) {
$response = $this->getConnection()->patch(
UrlHelper::appendParamsUrl(Urls::URL_FOXX_UPGRADE, $params),
$bodyStr,
["Content-Type" => "multipart/form-data; boundary=" . self::FOXX_APP_MIME_BOUNDARY]
);
} elseif (true === $replace) {
$response = $this->getConnection()->put(
UrlHelper::appendParamsUrl(Urls::URL_FOXX_REPLACE, $params),
$bodyStr,
["Content-Type" => "multipart/form-data; boundary=" . self::FOXX_APP_MIME_BOUNDARY]
);
} else {
$response = $this->getConnection()->post(
UrlHelper::appendParamsUrl(Urls::URL_FOXX_INSTALL, $params),
$bodyStr,
["Content-Type" => "multipart/form-data; boundary=" . self::FOXX_APP_MIME_BOUNDARY]
);
}
$code = $response->getHttpCode();
$response = $response->getJson();
if (true === $upgrade || true === $replace) {
if (200 !== $code) {
throw new ClientException("Foxx-Zip replace/upgrade failed: {$response['errorMessage']} (errno {$response['errorNum']})");
}
} else {
if (201 !== $code) {
throw new ClientException("Foxx-Zip install failed: {$response['errorMessage']} (errno {$response['errorNum']})");
}
}
return $response;
} catch (ServerException $e) {
throw new ClientException($e);
}
}
/**
* Remove a foxx-app.
*
* @throws ClientException
*
* @param string $mountPoint - the mount-point for the app, must begin with a '/'
* @param array $options - you can pass the control option teardown (bool)
*
* @return array - the server response
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
* @deprecated use uninstallService()
*/
public function removeFoxxApp($mountPoint, array $options = [])
{
return $this->uninstallService($mountPoint, $options);
}
/**
* Remove a foxx-app.
*
* @throws ClientException
*
* @param string $mountPoint - the mount-point for the app, must begin with a '/'
* @param array $options - you can pass the control option teardown (bool)
*
* @return array - the server response
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function uninstallService($mountPoint, array $options = [])
{
try {
$params = static::buildParameterArray([self::FOXX_APP_TEARDOWN], $mountPoint, $options);
$url = UrlHelper::appendParamsUrl(Urls::URL_FOXX_UNINSTALL, $params);
$response = $this->getConnection()->delete($url);
if ($response->getHttpCode() === 204) {
return $response->getJson();
}
throw new ClientException(sprintf('Foxx uninstall failed (Code: %d)', $response->getHttpCode()));
} catch (ServerException $e) {
if ($e->getMessage() === 'Service not found') {
throw new ClientException(sprintf('Mount point %s not present.', $mountPoint));
}
throw new ClientException($e->getMessage());
}
}
/**
* Retrieve a list of meta data for all installed services
*
* @throws ClientException
*
* @param array $options - you cann pass the option excludeSystem (bool)
*
* @return array - the server response
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function services(array $options = [])
{
try {
$params = static::buildParameterArray([self::FOXX_APP_EXCLUDE_SYSTEM], '', $options);
$url = UrlHelper::appendParamsUrl(Urls::URL_FOXX, $params);
$response = $this->getConnection()->get($url);
$code = $response->getHttpCode();
if (200 === $code) {
return $response->getJson();
}
throw new ClientException(sprintf('Error when fetching services meta data (Code: %d)', $response->getHttpCode()), $response->getJson());
} catch (ServerException $e) {
if ($e->getMessage() === 'Service not found') {
throw new ClientException(sprintf('Mount point %s not present.', $mountPoint));
}
throw new ClientException($e->getMessage());
}
}
/**
* Retrieve service meta data
*
* @throws ClientException
*
* @param string $mountPoint - the mount-point for the app, must begin with a '/'
* @param array $options - for future use
*
* @return array - the server response
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function serviceInfo($mountPoint, array $options = [])
{
try {
$params = static::buildParameterArray([], $mountPoint, $options);
$url = UrlHelper::appendParamsUrl(Urls::URL_FOXX_SERVICE, $params);
$response = $this->getConnection()->get($url);
$code = $response->getHttpCode();
if (200 === $code) {
return $response->getJson();
} elseif (400 === $code) {
throw new ClientException("Service unknown: {$mountPoint}");
}
throw new ClientException(sprintf('Error when fetching service meta data (Code: %d)', $response->getHttpCode()), $response->getJson());
} catch (ServerException $e) {
if ($e->getMessage() === 'Service not found') {
throw new ClientException(sprintf('Mount point %s not present.', $mountPoint));
}
throw new ClientException($e->getMessage());
}
}
/**
* Build an array of url parameters.
*
* Parameter names found in $options are removed from the array.
*
* @param array $names the names of the parameters to determin the value for
* @param string $mountPoint Mount point of the foxx app to affect
* @param array $options pass values to use here, if not present defaults will be used.
*
* @return array
*/
protected static function buildParameterArray(array $names, string $mountPoint, array &$options)
{
$params = !empty($mountPoint) ? ['mount' => $mountPoint] : [];
foreach ($names as $param) {
if (isset($options[$param])) {
$params[$param] = $options[$param];
unset($options[$param]);
} elseif (array_key_exists($param, static::FOXX_APP_DEFAULT_PARAMS)) {
$params[$param] = self::FOXX_APP_DEFAULT_PARAMS[$param];
} else {
throw new ClientException("Unknown option '{$param}'.");
}
}
return $params;
}
}
class_alias(FoxxHandler::class, '\triagens\ArangoDb\FoxxHandler');