Skip to content

Commit 661e67b

Browse files
Update Config
1 parent 0a287dd commit 661e67b

File tree

5 files changed

+155
-28
lines changed

5 files changed

+155
-28
lines changed

src/Config.php

Lines changed: 105 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@
33
namespace Pavlusha311245\UnitPhpSdk;
44

55
use Pavlusha311245\UnitPhpSdk\Abstract\ApplicationAbstract;
6-
use Pavlusha311245\UnitPhpSdk\Config\AccessLog;
7-
use Pavlusha311245\UnitPhpSdk\Config\Application;
8-
use Pavlusha311245\UnitPhpSdk\Config\Listener;
9-
use Pavlusha311245\UnitPhpSdk\Config\Route;
6+
use Pavlusha311245\UnitPhpSdk\Config\{
7+
Application,
8+
Listener,
9+
Route,
10+
Upstream,
11+
AccessLog
12+
};
1013
use Pavlusha311245\UnitPhpSdk\Enums\HttpMethodsEnum;
1114
use Pavlusha311245\UnitPhpSdk\Exceptions\UnitException;
1215
use Pavlusha311245\UnitPhpSdk\Interfaces\ConfigInterface;
@@ -50,43 +53,105 @@ class Config implements ConfigInterface
5053
*
5154
* @throws UnitException
5255
*/
53-
public function __construct(array $data, private UnitRequest $_unitRequest)
56+
public function __construct(object $data, private readonly UnitRequest $_unitRequest)
5457
{
55-
if (array_key_exists('routes', $data)) {
56-
foreach ($data['routes'] as $routeName => $routeData) {
57-
$this->_routes[$routeName] = new Route($routeName, $routeData);
58+
$rawData = $data;
59+
$jsonData = json_decode(json_encode($data), true);
60+
61+
$this->loadRoutes($rawData);
62+
$this->loadApplications($jsonData);
63+
$this->loadUpstreams($jsonData);
64+
$this->loadListeners($jsonData);
65+
}
66+
67+
/**
68+
* @param array $data
69+
* @return void
70+
* @throws UnitException
71+
*/
72+
public function loadListeners(array $data): void
73+
{
74+
if (array_key_exists('listeners', $data)) {
75+
foreach ($data['listeners'] as $listener => $listenerData) {
76+
$listener = (new Listener(
77+
_listener: $listener,
78+
pass: $listenerData['pass']
79+
))->parseFromArray($listenerData);
80+
81+
$typePath = $listener->getPass()->getPassType();
82+
$typePathName = $listener->getPass()->toArray()[1] ?? null;
83+
84+
($this->{"_{$typePath}"}[$typePathName ?? 'default'])?->setListener($listener);
85+
86+
$this->_listeners[] = $listener;
5887
}
5988
}
89+
}
90+
6091

92+
/**
93+
* @param array $data
94+
* @return void
95+
* @throws UnitException
96+
*/
97+
public function loadApplications(array $data): void
98+
{
6199
if (array_key_exists('applications', $data)) {
62100
foreach ($data['applications'] as $appName => $appData) {
63-
// TODO: implement go and nodejs detect
101+
// TODO: implement Perl, Python and Ruby applications
64102
$this->_applications[$appName] = match ($appData['type']) {
65103
'php' => new Application\PhpApplication($appData),
66-
'external' => new Application\NodeJsApplication($appData),
104+
'java' => new Application\JavaApplication($appData),
105+
'external' => $this->isNodeJsApplication($appData) ? new Application\NodeJsApplication($appData) : new Application\GoApplication($appData),
67106
};
68107
$this->_applications[$appName]->setName($appName);
69-
$this->_applications[$appName]->setUnitRequest($_unitRequest);
108+
$this->_applications[$appName]->setUnitRequest($this->_unitRequest);
70109
}
71110
}
111+
}
72112

73-
if (array_key_exists('listeners', $data)) {
74-
foreach ($data['listeners'] as $listener => $listenerData) {
75-
$listener = (new Listener(
76-
_listener: $listener,
77-
pass: $listenerData['pass']
78-
))->parseFromArray($listenerData);
79-
$typePath = $listener->getPass()->getPassType();
80-
$typePathName = $listener->getPass()->toArray()[1];
113+
/**
114+
* @param $appData
115+
* @return bool
116+
*/
117+
public function isNodeJsApplication($appData): bool
118+
{
119+
foreach ($appData['arguments'] as $argument) {
120+
if (str_contains($argument, '.js')) {
121+
return true;
122+
}
123+
}
81124

82-
($this->{"_{$typePath}"}[$typePathName])->setListener($listener);
125+
if (str_contains($appData['executable'], '.js')) {
126+
return true;
127+
}
83128

84-
$this->_listeners[] = $listener;
129+
return false;
130+
}
131+
132+
public function loadRoutes(object $rawData): void
133+
{
134+
if (!empty($rawData->routes)) {
135+
if (!is_array($rawData->routes)) {
136+
foreach ((array)$rawData->routes as $routeName => $routeData) {
137+
$this->_routes[$routeName] = new Route($routeName, $routeData);
138+
}
139+
} else {
140+
$this->_routes['default'] = new Route('default', (array)$rawData->routes[0], true);
85141
}
86142
}
143+
}
87144

145+
/**
146+
* @param array $data
147+
* @return void
148+
*/
149+
public function loadUpstreams(array $data): void
150+
{
88151
if (array_key_exists('upstreams', $data)) {
89-
$this->_upstreams = $data['upstreams'] ?? [];
152+
foreach ($data['upstreams'] as $upstreamName => $upstreamData) {
153+
$this->_upstreams[$upstreamName] = new Upstream($upstreamName, $upstreamData);
154+
}
90155
}
91156
}
92157

@@ -116,6 +181,22 @@ public function getListenerByPort(int $port): Listener|null
116181
return null;
117182
}
118183

184+
/**
185+
* @inheritDoc
186+
*/
187+
public function addListener(Listener $listener): bool
188+
{
189+
try {
190+
$this->_unitRequest->setMethod(HttpMethodsEnum::PUT->value);
191+
$this->_unitRequest->setData($listener->toJson());
192+
$this->_unitRequest->send("/config/listeners/{$listener->getListener()}");
193+
} catch (UnitException $exception) {
194+
return false;
195+
}
196+
197+
return true;
198+
}
199+
119200
/**
120201
* Remove listener
121202
*
@@ -220,6 +301,8 @@ public function removeRoutes(): bool
220301
public function removeRoute(Route|string $route): bool
221302
{
222303
// TODO: should be implemented
304+
305+
return false;
223306
}
224307

225308
/**
@@ -258,7 +341,6 @@ public function getAccessLog(): ?AccessLog
258341
{
259342
$result = $this->_unitRequest->send('/config/access_log');
260343

261-
// TODO: need null
262344
return new AccessLog($result);
263345
}
264346

src/Config/Listener.php

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ private function generateLink()
5656
$separatedListener = explode(':', $this->_listener);
5757

5858
$this->_link = $separatedListener[0] == '*' ?
59-
"0.0.0.0:{$separatedListener[1]}" : $separatedListener;
59+
"0.0.0.0:{$separatedListener[1]}" : $this->_listener;
6060
}
6161

6262
/**
@@ -135,4 +135,36 @@ public function parseFromArray(array $data): Listener
135135

136136
return $this;
137137
}
138+
139+
/**
140+
* Return listener as array
141+
*
142+
* @return array
143+
*/
144+
public function toArray(): array
145+
{
146+
$listenerArray = [
147+
'pass' => $this->_pass->toString(),
148+
];
149+
150+
if (!empty($this->_tls)) {
151+
$listenerArray['tls'] = $this->_tls;
152+
}
153+
154+
if (!empty($this->_forwarded)) {
155+
$listenerArray['forwarded'] = $this->_forwarded;
156+
}
157+
158+
return $listenerArray;
159+
}
160+
161+
/**
162+
* Return Listener as JSON
163+
*
164+
* @return string
165+
*/
166+
public function toJson(): string
167+
{
168+
return json_encode($this->toArray());
169+
}
138170
}

src/Config/Route.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,15 @@ class Route implements RouteInterface
2525

2626
public function __construct(
2727
private readonly string $_name,
28-
$data
28+
$data,
29+
bool $single = false
2930
) {
30-
foreach ($data as $routeBlock) {
31-
$this->_routeBlocks[] = new RouteBlock($routeBlock);
31+
if ($single) {
32+
$this->_routeBlocks[] = new RouteBlock($data);
33+
} else {
34+
foreach ($data as $routeBlock) {
35+
$this->_routeBlocks[] = new RouteBlock($routeBlock);
36+
}
3237
}
3338
}
3439

src/Interfaces/ConfigInterface.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ public function getListeners(): array;
2222
*/
2323
public function getListenerByPort(int $port): Listener|null;
2424

25+
/**
26+
* Add listener
27+
*
28+
* @param Listener $listener
29+
* @return bool
30+
*/
31+
public function addListener(Listener $listener): bool;
32+
2533
/**
2634
* @param Listener $listener
2735
* @return bool

src/Unit.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public function getConfig(): Interfaces\ConfigInterface
4949
private function loadConfig(): void
5050
{
5151
$request = new UnitRequest($this->socket, $this->address);
52-
$result = $request->send('/config');
52+
$result = $request->send('/config', false);
5353
$this->_config = new Config($result, new UnitRequest($this->socket, $this->address));
5454
}
5555

0 commit comments

Comments
 (0)