Skip to content
This repository was archived by the owner on Oct 22, 2025. It is now read-only.

Commit 3219d28

Browse files
Update unit and config classes
1 parent 0ba970e commit 3219d28

File tree

4 files changed

+315
-71
lines changed

4 files changed

+315
-71
lines changed

src/Config.php

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

55
use Pavlusha311245\UnitPhpSdk\Abstract\ApplicationAbstract;
6+
use Pavlusha311245\UnitPhpSdk\Config\AccessLog;
67
use Pavlusha311245\UnitPhpSdk\Config\Application;
78
use Pavlusha311245\UnitPhpSdk\Config\Listener;
89
use Pavlusha311245\UnitPhpSdk\Config\Route;
9-
use Pavlusha311245\UnitPhpSdk\Enums\ApplicationTypeEnum;
10+
use Pavlusha311245\UnitPhpSdk\Enums\HttpMethodsEnum;
11+
use Pavlusha311245\UnitPhpSdk\Exceptions\UnitException;
1012
use Pavlusha311245\UnitPhpSdk\Interfaces\ConfigInterface;
11-
use PHPUnit\TextUI\Configuration\Php;
1213

1314
/**
1415
* This class contains Nginx Unit config data
1516
*/
1617
class Config implements ConfigInterface
1718
{
19+
const SOCKET = '/usr/local/var/run/unit/control.sock';
20+
const ADDRESS = 'http://localhost';
21+
1822
/**
1923
* Listeners accept requests
2024
*
@@ -51,30 +55,41 @@ class Config implements ConfigInterface
5155
*/
5256
public function __construct(array $data)
5357
{
54-
foreach ($data['routes'] as $routeName => $routeData) {
55-
$this->_routes[$routeName] = new Route($routeName, $routeData);
58+
if (array_key_exists('routes', $data)) {
59+
foreach ($data['routes'] as $routeName => $routeData) {
60+
$this->_routes[$routeName] = new Route($routeName, $routeData);
61+
}
5662
}
57-
foreach ($data['applications'] as $appName => $appData) {
58-
// TODO: implement go and nodejs detect
59-
$this->_applications[$appName] = match ($appData['type']) {
60-
'php' => new Application\PhpApplication($appData),
61-
'external' => new Application\NodeJsApplication($appData),
62-
};
63+
64+
if (array_key_exists('applications', $data)) {
65+
foreach ($data['applications'] as $appName => $appData) {
66+
// TODO: implement go and nodejs detect
67+
$this->_applications[$appName] = match ($appData['type']) {
68+
'php' => new Application\PhpApplication($appData),
69+
'external' => new Application\NodeJsApplication($appData),
70+
};
71+
72+
$this->_applications[$appName]->setName($appName);
73+
}
6374
}
6475

65-
foreach ($data['listeners'] as $listener => $listenerData) {
66-
$listener = (new Listener(
67-
_listener: $listener
68-
))->parseFromArray($listenerData);
69-
$typePath = $listener->getPass()[0];
70-
$typePathName = $listener->getPass()[1];
76+
if (array_key_exists('listeners', $data)) {
77+
foreach ($data['listeners'] as $listener => $listenerData) {
78+
$listener = (new Listener(
79+
_listener: $listener
80+
))->parseFromArray($listenerData);
81+
$typePath = $listener->getPass()[0];
82+
$typePathName = $listener->getPass()[1];
7183

72-
($this->{"_{$typePath}"}[$typePathName])->setListener($listener);
84+
($this->{"_{$typePath}"}[$typePathName])->setListener($listener);
7385

74-
$this->_listeners[] = $listener;
86+
$this->_listeners[] = $listener;
87+
}
7588
}
7689

77-
$this->_upstreams = $data['upstreams'] ?? [];
90+
if (array_key_exists('upstreams', $data)) {
91+
$this->_upstreams = $data['upstreams'] ?? [];
92+
}
7893
}
7994

8095
/**
@@ -95,9 +110,23 @@ public function getListenerByPort(int $port): Listener|null
95110
}
96111

97112
/**
98-
* Get listeners from config
113+
* Remove listener
99114
*
100-
* @return array
115+
* @throws UnitException
116+
*/
117+
public function removeListener(Listener $listener): bool
118+
{
119+
$request = new UnitRequest(self::SOCKET, self::ADDRESS);
120+
$request->setMethod(HttpMethodsEnum::DELETE->value);
121+
122+
$listenerId = $listener->getListener();
123+
$request->send("/config/listeners/{$listenerId}");
124+
125+
return true;
126+
}
127+
128+
/**
129+
* @inheritDoc
101130
*/
102131
public function getListeners(): array
103132
{
@@ -125,6 +154,20 @@ public function getApplication($applicationName): ApplicationAbstract
125154
return $this->_applications[$applicationName];
126155
}
127156

157+
/**
158+
* @throws UnitException
159+
*/
160+
public function removeApplication(ApplicationAbstract $application): bool
161+
{
162+
$request = new UnitRequest(self::SOCKET, self::ADDRESS);
163+
$request->setMethod(HttpMethodsEnum::DELETE->value);
164+
165+
$applicationName = $application->getName();
166+
print_r($request->send("/config/applications/{$applicationName}"));
167+
168+
return true;
169+
}
170+
128171
/**
129172
* Get routes from config
130173
*
@@ -138,14 +181,43 @@ public function getRoutes(): array
138181
/**
139182
* Get route from config by name
140183
*
141-
* @param $routeName
142-
* @return mixed
184+
* @param string $routeName
185+
* @return Route|null
143186
*/
144187
public function getRoute(string $routeName): Route|null
145188
{
146189
return $this->_routes[$routeName] ?? null;
147190
}
148191

192+
/**
193+
* @throws UnitException
194+
*/
195+
public function removeRoutes(): bool
196+
{
197+
foreach ($this->getRoutes() as $route) {
198+
if ($route->hasListeners()) {
199+
foreach ($route->getListeners() as $listener) {
200+
$this->removeListener($listener);
201+
}
202+
}
203+
}
204+
205+
try {
206+
$request = new UnitRequest(self::SOCKET, self::ADDRESS);
207+
$request->setMethod(HttpMethodsEnum::DELETE->value);
208+
$request->send('/config/routes');
209+
} catch (UnitException $exception) {
210+
return false;
211+
}
212+
213+
return true;
214+
}
215+
216+
public function removeRoute(Route|string $route): bool
217+
{
218+
// TODO: should be implemented
219+
}
220+
149221
/**
150222
* Get upstreams
151223
*
@@ -156,6 +228,51 @@ public function getUpstreams(): mixed
156228
return $this->_upstreams;
157229
}
158230

231+
/**
232+
* @inheritDoc
233+
*/
234+
public function setAccessLog($path, $format = null): bool
235+
{
236+
$data['path'] = $path;
237+
238+
if (!empty($format)) {
239+
$data['format'] = $format;
240+
}
241+
242+
try {
243+
$request = new UnitRequest(self::SOCKET, self::ADDRESS);
244+
$request->setMethod(HttpMethodsEnum::PUT->value);
245+
$request->setData(json_encode($data));
246+
$request->send('/config/access_log');
247+
} catch (UnitException $exception) {
248+
return false;
249+
}
250+
251+
return true;
252+
}
253+
254+
public function getAccessLog(): ?AccessLog
255+
{
256+
$request = new UnitRequest(self::SOCKET, self::ADDRESS);
257+
$result = $request->send('/config/access_log');
258+
259+
// TODO: need null
260+
return new AccessLog($result);
261+
}
262+
263+
public function removeAccessLog(): bool
264+
{
265+
try {
266+
$request = new UnitRequest(self::SOCKET, self::ADDRESS);
267+
$request->setMethod(HttpMethodsEnum::DELETE->value);
268+
$request->send('/config/access_log');
269+
} catch (UnitException $exception) {
270+
return false;
271+
}
272+
273+
return true;
274+
}
275+
159276
/**
160277
* Return config as array
161278
*

src/Interfaces/ConfigInterface.php

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,99 @@
33
namespace Pavlusha311245\UnitPhpSdk\Interfaces;
44

55
use Pavlusha311245\UnitPhpSdk\Abstract\ApplicationAbstract;
6+
use Pavlusha311245\UnitPhpSdk\Config\AccessLog;
67
use Pavlusha311245\UnitPhpSdk\Config\Listener;
78
use Pavlusha311245\UnitPhpSdk\Config\Route;
89

910
interface ConfigInterface
1011
{
12+
/**
13+
* Get all listeners
14+
*
15+
* @return array
16+
*/
1117
public function getListeners(): array;
1218

19+
/**
20+
* @param int $port
21+
* @return Listener|null
22+
*/
1323
public function getListenerByPort(int $port): Listener|null;
1424

25+
/**
26+
* @param Listener $listener
27+
* @return bool
28+
*/
29+
public function removeListener(Listener $listener): bool;
30+
31+
/**
32+
* @return array
33+
*/
1534
public function getRoutes(): array;
1635

36+
/**
37+
* @param string $routeName
38+
* @return Route|null
39+
*/
1740
public function getRoute(string $routeName): Route|null;
1841

42+
/**
43+
* Remove all routes and linked listeners
44+
*
45+
* @return bool
46+
*/
47+
public function removeRoutes(): bool;
48+
49+
/**
50+
* @param Route|string $route
51+
* @return bool
52+
*/
53+
public function removeRoute(Route|string $route): bool;
54+
55+
/**
56+
* @return array
57+
*/
1958
public function getApplications(): array;
2059

60+
/**
61+
* @param $applicationName
62+
* @return ApplicationAbstract
63+
*/
2164
public function getApplication($applicationName): ApplicationAbstract;
2265

66+
/**
67+
* @param ApplicationAbstract $application
68+
* @return bool
69+
*/
70+
public function removeApplication(ApplicationAbstract $application): bool;
71+
72+
/**
73+
* @return mixed
74+
*/
2375
public function getUpstreams();
2476

77+
/**
78+
* Setup access log file
79+
*
80+
* @param $path
81+
* @param $format
82+
* @return mixed
83+
*/
84+
public function setAccessLog($path, $format = null);
85+
86+
/**
87+
* Return access log if exists
88+
*
89+
* @return AccessLog|null
90+
*/
91+
public function getAccessLog(): ?AccessLog;
92+
93+
/**
94+
* Remove access log
95+
*
96+
* @return bool
97+
*/
98+
public function removeAccessLog(): bool;
99+
25100
public function toArray(): array;
26101
}

src/Interfaces/UnitInterface.php

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,59 @@
22

33
namespace Pavlusha311245\UnitPhpSdk\Interfaces;
44

5+
use Pavlusha311245\UnitPhpSdk\Certificate;
6+
use Pavlusha311245\UnitPhpSdk\Exceptions\UnitException;
7+
58
interface UnitInterface
69
{
10+
/**
11+
* Return Unit socket
12+
*
13+
* @return string
14+
*/
715
public function getSocket(): string;
816

17+
/**
18+
* Return Unit address
19+
*
20+
* @return string
21+
*/
922
public function getAddress(): string;
1023

24+
/**
25+
* Return full config uploaded on Nginx Unit
26+
*
27+
* @return ConfigInterface
28+
*/
1129
public function getConfig(): ConfigInterface;
1230

31+
/**
32+
* Return Usage Statistics from Unit
33+
*
34+
* @return StatisticsInterface
35+
* @throws UnitException
36+
*/
1337
public function getStatistics(): StatisticsInterface;
1438

39+
/**
40+
* Return array of certificates uploaded on Unit
41+
*
42+
* @return array
43+
*/
1544
public function getCertificates(): array;
1645

17-
public function setAccessLog($path, $format = null);
46+
/**
47+
* Return Certificate object
48+
*
49+
* @param $certificateName
50+
* @return Certificate|null
51+
*/
52+
public function getCertificate($certificateName): ?Certificate;
53+
54+
/**
55+
* Remove all data from config
56+
*
57+
* @return bool
58+
*/
59+
public function removeConfig(): bool;
1860
}

0 commit comments

Comments
 (0)