|
16 | 16 | *
|
17 | 17 | * @implements ConfigInterface
|
18 | 18 | */
|
19 |
| -class Config implements ConfigInterface |
| 19 | +class Config implements ConfigInterface, Arrayable |
20 | 20 | {
|
21 | 21 | /**
|
22 | 22 | * Listeners that accept requests
|
@@ -410,6 +410,54 @@ public function getUpstreams(): array
|
410 | 410 | return $this->_upstreams;
|
411 | 411 | }
|
412 | 412 |
|
| 413 | + /** |
| 414 | + * @inheritDoc |
| 415 | + * @throws UnitException |
| 416 | + */ |
| 417 | + public function uploadUpstream(Upstream $upstream, string $name = ''): bool |
| 418 | + { |
| 419 | + if (empty($upstream->getName()) && empty($name)) { |
| 420 | + throw new UnitException('Name isn\'t be empty'); |
| 421 | + } |
| 422 | + |
| 423 | + $upstreamName = empty($upstream->getName()) ? $name : $upstream->getName(); |
| 424 | + |
| 425 | + try { |
| 426 | + $this->_unitRequest->setMethod(HttpMethodsEnum::PUT->value); |
| 427 | + $this->_unitRequest->setData(json_encode($upstream->toJson())); |
| 428 | + $this->_unitRequest->send("/config/upstreams/{$upstreamName}"); |
| 429 | + } catch (UnitException $exception) { |
| 430 | + print_r($exception->getMessage()); |
| 431 | + return false; |
| 432 | + } |
| 433 | + |
| 434 | + return true; |
| 435 | + } |
| 436 | + |
| 437 | + public function uploadUpstreamsFromFile(string $path): bool |
| 438 | + { |
| 439 | + $fileContent = file_get_contents($path); |
| 440 | + |
| 441 | + if (!$fileContent) { |
| 442 | + throw new FileNotFoundException(); |
| 443 | + } |
| 444 | + |
| 445 | + if (empty(json_decode($fileContent, true))) { |
| 446 | + throw new UnitException('It\'s not a JSON file'); |
| 447 | + } |
| 448 | + |
| 449 | + try { |
| 450 | + $this->_unitRequest->setMethod(HttpMethodsEnum::PUT->value); |
| 451 | + $this->_unitRequest->setData(json_encode($fileContent)); |
| 452 | + $this->_unitRequest->send('/config/upstreams'); |
| 453 | + } catch (UnitException $exception) { |
| 454 | + print_r($exception->getMessage()); |
| 455 | + return false; |
| 456 | + } |
| 457 | + |
| 458 | + return true; |
| 459 | + } |
| 460 | + |
413 | 461 | /**
|
414 | 462 | * @inheritDoc
|
415 | 463 | */
|
|
0 commit comments