Skip to content

Commit 62031bf

Browse files
Add methods uploadUpstream uploadUpstreamsFromFile
1 parent 268b93a commit 62031bf

File tree

2 files changed

+66
-1
lines changed

2 files changed

+66
-1
lines changed

src/Config.php

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*
1717
* @implements ConfigInterface
1818
*/
19-
class Config implements ConfigInterface
19+
class Config implements ConfigInterface, Arrayable
2020
{
2121
/**
2222
* Listeners that accept requests
@@ -410,6 +410,54 @@ public function getUpstreams(): array
410410
return $this->_upstreams;
411411
}
412412

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+
413461
/**
414462
* @inheritDoc
415463
*/

src/Contracts/ConfigInterface.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,23 @@ public function removeApplication(ApplicationAbstract|string $application): bool
114114
*/
115115
public function getUpstreams(): array;
116116

117+
/**
118+
* Upload Upstream object to nginx Unit
119+
*
120+
* @param Upstream $upstream
121+
* @param string $name
122+
* @return mixed
123+
*/
124+
public function uploadUpstream(Upstream $upstream, string $name): bool;
125+
126+
/**
127+
* Upload all upstreams from file to Nginx Unit
128+
*
129+
* @param string $path
130+
* @return mixed
131+
*/
132+
public function uploadUpstreamsFromFile(string $path): bool;
133+
117134
/**
118135
* Setup access log file
119136
*

0 commit comments

Comments
 (0)