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

Commit 80240e7

Browse files
Remove constants
1 parent d38253f commit 80240e7

File tree

4 files changed

+46
-34
lines changed

4 files changed

+46
-34
lines changed

src/Abstract/ApplicationAbstract.php

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@
55
use Pavlusha311245\UnitPhpSdk\Config\Application\ProcessManagement\ApplicationProcess;
66
use Pavlusha311245\UnitPhpSdk\Config\Application\ProcessManagement\ProcessIsolation;
77
use Pavlusha311245\UnitPhpSdk\Config\Application\ProcessManagement\RequestLimit;
8-
use Pavlusha311245\UnitPhpSdk\Config\Listener;
9-
use Pavlusha311245\UnitPhpSdk\Enums\ApplicationTypeEnum;
10-
use Pavlusha311245\UnitPhpSdk\Enums\HttpMethodsEnum;
118
use Pavlusha311245\UnitPhpSdk\Exceptions\UnitException;
129
use Pavlusha311245\UnitPhpSdk\Interfaces\ApplicationControlInterface;
1310
use Pavlusha311245\UnitPhpSdk\Interfaces\ApplicationInterface;
@@ -18,11 +15,10 @@ abstract class ApplicationAbstract implements ApplicationInterface, ApplicationC
1815
{
1916
use HasListeners;
2017

21-
public const SOCKET = '/usr/local/var/run/unit/control.sock';
22-
public const ADDRESS = 'http://localhost';
23-
2418
private string $_type;
2519

20+
private UnitRequest $_unitRequest;
21+
2622
/**
2723
* Environment variables to be passed to the app
2824
*
@@ -88,19 +84,30 @@ abstract class ApplicationAbstract implements ApplicationInterface, ApplicationC
8884
*/
8985
private ?ProcessIsolation $_isolation = null;
9086

87+
/**
88+
* @throws UnitException
89+
*/
9190
public function __construct(array $data = null)
9291
{
9392
if (!empty($data)) {
9493
$this->parseFromArray($data);
9594
}
9695
}
9796

97+
/**
98+
* @param UnitRequest $unitRequest
99+
*/
100+
public function setUnitRequest(UnitRequest $unitRequest): void
101+
{
102+
$this->_unitRequest = $unitRequest;
103+
}
104+
98105
public function getType(): string
99106
{
100107
return $this->_type;
101108
}
102109

103-
public function setType(string $type)
110+
public function setType(string $type): void
104111
{
105112
$this->_type = $type;
106113
}
@@ -130,9 +137,12 @@ public function getEnvironment(): array
130137
return $this->_environment;
131138
}
132139

140+
/**
141+
* @throws UnitException
142+
*/
133143
public function setEnvironment(array $environment): void
134144
{
135-
foreach ($environment as $key => $value) {
145+
foreach ($environment as $value) {
136146
if (!is_string($value)) {
137147
throw new UnitException('Parse Exception');
138148
}
@@ -278,8 +288,7 @@ public function parseFromArray(array $data): void
278288
public function restartApplication(): bool
279289
{
280290
try {
281-
$request = new UnitRequest(self::SOCKET, self::ADDRESS);
282-
$result = $request->send("/control/applications/{$this->getName()}/restart");
291+
$this->_unitRequest->send("/control/applications/{$this->getName()}/restart");
283292
} catch (UnitException $exception) {
284293
return false;
285294
}

src/Config.php

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@
1616
*/
1717
class Config implements ConfigInterface
1818
{
19-
public const SOCKET = '/usr/local/var/run/unit/control.sock';
20-
public const ADDRESS = 'http://localhost';
21-
2219
/**
2320
* Listeners accept requests
2421
*
@@ -53,7 +50,7 @@ class Config implements ConfigInterface
5350
*
5451
* @throws UnitException
5552
*/
56-
public function __construct(array $data)
53+
public function __construct(array $data, private UnitRequest $_unitRequest)
5754
{
5855
if (array_key_exists('routes', $data)) {
5956
foreach ($data['routes'] as $routeName => $routeData) {
@@ -68,8 +65,8 @@ public function __construct(array $data)
6865
'php' => new Application\PhpApplication($appData),
6966
'external' => new Application\NodeJsApplication($appData),
7067
};
71-
7268
$this->_applications[$appName]->setName($appName);
69+
$this->_applications[$appName]->setUnitRequest($_unitRequest);
7370
}
7471
}
7572

@@ -126,11 +123,10 @@ public function getListenerByPort(int $port): Listener|null
126123
*/
127124
public function removeListener(Listener $listener): bool
128125
{
129-
$request = new UnitRequest(self::SOCKET, self::ADDRESS);
130-
$request->setMethod(HttpMethodsEnum::DELETE->value);
126+
$this->_unitRequest->setMethod(HttpMethodsEnum::DELETE->value);
131127

132128
$listenerId = $listener->getListener();
133-
$request->send("/config/listeners/{$listenerId}");
129+
$this->_unitRequest->send("/config/listeners/{$listenerId}");
134130

135131
return true;
136132
}
@@ -169,11 +165,10 @@ public function getApplication($applicationName): ApplicationAbstract
169165
*/
170166
public function removeApplication(ApplicationAbstract $application): bool
171167
{
172-
$request = new UnitRequest(self::SOCKET, self::ADDRESS);
173-
$request->setMethod(HttpMethodsEnum::DELETE->value);
168+
$this->_unitRequest->setMethod(HttpMethodsEnum::DELETE->value);
174169

175170
$applicationName = $application->getName();
176-
print_r($request->send("/config/applications/{$applicationName}"));
171+
$this->_unitRequest->send("/config/applications/{$applicationName}");
177172

178173
return true;
179174
}
@@ -213,9 +208,8 @@ public function removeRoutes(): bool
213208
}
214209

215210
try {
216-
$request = new UnitRequest(self::SOCKET, self::ADDRESS);
217-
$request->setMethod(HttpMethodsEnum::DELETE->value);
218-
$request->send('/config/routes');
211+
$this->_unitRequest->setMethod(HttpMethodsEnum::DELETE->value);
212+
$this->_unitRequest->send('/config/routes');
219213
} catch (UnitException $exception) {
220214
return false;
221215
}
@@ -250,10 +244,9 @@ public function setAccessLog($path, $format = null): bool
250244
}
251245

252246
try {
253-
$request = new UnitRequest(self::SOCKET, self::ADDRESS);
254-
$request->setMethod(HttpMethodsEnum::PUT->value);
255-
$request->setData(json_encode($data));
256-
$request->send('/config/access_log');
247+
$this->_unitRequest->setMethod(HttpMethodsEnum::PUT->value);
248+
$this->_unitRequest->setData(json_encode($data));
249+
$this->_unitRequest->send('/config/access_log');
257250
} catch (UnitException $exception) {
258251
return false;
259252
}
@@ -263,8 +256,7 @@ public function setAccessLog($path, $format = null): bool
263256

264257
public function getAccessLog(): ?AccessLog
265258
{
266-
$request = new UnitRequest(self::SOCKET, self::ADDRESS);
267-
$result = $request->send('/config/access_log');
259+
$result = $this->_unitRequest->send('/config/access_log');
268260

269261
// TODO: need null
270262
return new AccessLog($result);
@@ -273,9 +265,8 @@ public function getAccessLog(): ?AccessLog
273265
public function removeAccessLog(): bool
274266
{
275267
try {
276-
$request = new UnitRequest(self::SOCKET, self::ADDRESS);
277-
$request->setMethod(HttpMethodsEnum::DELETE->value);
278-
$request->send('/config/access_log');
268+
$this->_unitRequest->setMethod(HttpMethodsEnum::DELETE->value);
269+
$this->_unitRequest->send('/config/access_log');
279270
} catch (UnitException $exception) {
280271
return false;
281272
}

src/Unit.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ private function loadConfig(): void
5050
{
5151
$request = new UnitRequest($this->socket, $this->address);
5252
$result = $request->send('/config');
53-
$this->_config = new Config($result);
53+
$this->_config = new Config($result, new UnitRequest($this->socket, $this->address));
5454
}
5555

5656
/**

src/UnitRequest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,19 @@ public function send($uri)
7373
throw new UnitException('Error:' . curl_error($curlHandler));
7474
}
7575
curl_close($curlHandler);
76+
$this->clean();
7677

7778
return json_decode($result, true);
7879
}
80+
81+
/**
82+
* Clear data after request
83+
*
84+
* @return void
85+
*/
86+
private function clean(): void
87+
{
88+
$this->_method = 'GET';
89+
$this->_data = null;
90+
}
7991
}

0 commit comments

Comments
 (0)