Skip to content

Commit f96ad11

Browse files
Refactor and extend SDK functionality in AccessLog and Route classes
Added `if` property to `AccessLog` for dynamic access log conditions with corresponding accessor methods. Enhanced `Route` with `getRouteBlock` and `removeRouteBlock` methods for better configurability. Standardized composer requirements to PHP 8.4. Improved exception handling in `Unit` class, added support for empty configuration uploads in `CanUpload` trait, and resolved array handling in `Config`. Simplified API endpoint references using `ApiPathEnum` and addressed minor formatting issues across the SDK.
1 parent 27dc361 commit f96ad11

File tree

7 files changed

+94
-18
lines changed

7 files changed

+94
-18
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"lint": "PHP_CS_FIXER_IGNORE_ENV=1 ./vendor/bin/php-cs-fixer fix --rules=@PSR12 ."
2929
},
3030
"require": {
31-
"php": "^8.3|^8.4",
31+
"php": "^8.4",
3232
"friendsofphp/php-cs-fixer": "^3.68",
3333
"guzzlehttp/guzzle": "^7.9",
3434
"nesbot/carbon": "^3.8",

src/Config.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ public function parseUpstreams(array $data): void
229229
*/
230230
public function getListener(string $listener): ?Listener
231231
{
232-
return $this->listeners[$listener] ?? null;
232+
return array_find($this->listeners, fn (Listener $item) => $item->getListener() == $listener);
233233
}
234234

235235
/**
@@ -549,8 +549,8 @@ public function setAccessLog($path, $format = null): bool
549549

550550
try {
551551
$this->unitRequest->setMethod(HttpMethodsEnum::PUT)
552-
->send(ApiPathEnum::ACCESS_LOG, requestOptions: [
553-
'json' => json_encode($data)
552+
->send(ApiPathEnum::ACCESS_LOG->value, requestOptions: [
553+
'json' => $data
554554
]);
555555
} catch (UnitException) {
556556
return false;
@@ -571,7 +571,9 @@ public function getAccessLog(): ?AccessLog
571571

572572
return new AccessLog($result);
573573
} catch (Throwable $exception) {
574-
throw new UnitException($exception->getMessage(), $exception->getCode(), $exception);
574+
// TODO: add for all 404 normal exception
575+
576+
return null;
575577
}
576578
}
577579

src/Config/AccessLog.php

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace UnitPhpSdk\Config;
44

5-
use UnitPhpSdk\Builders\EndpointBuilder;
65
use UnitPhpSdk\Contracts\Arrayable;
76
use UnitPhpSdk\Contracts\Uploadable;
87
use UnitPhpSdk\Enums\ApiPathEnum;
@@ -26,11 +25,25 @@ class AccessLog implements Uploadable, Arrayable
2625
*/
2726
private string|array|null $format;
2827

28+
/**
29+
* The access_log can be dynamically turned on and off by using the if option
30+
*
31+
* This feature lets users set conditions to determine whether access logs are recorded.
32+
* The if option supports a string and JavaScript code.
33+
* If its value is empty, 0, false, null, or undefined,
34+
* the logs will not be recorded. And the ‘!’ as a prefix inverses the condition.
35+
*
36+
* @var string|mixed|null
37+
*/
38+
private ?string $if;
39+
2940
public function __construct(
3041
array $data
31-
) {
42+
)
43+
{
3244
$this->path = $data['path'] ?? null;
3345
$this->format = $data['format'] ?? null;
46+
$this->if = $data['if'] ?? null;
3447

3548
$this->setApiEndpoint(ApiPathEnum::ACCESS_LOG->value);
3649
}
@@ -52,9 +65,9 @@ public function setPath(string $path): void
5265
}
5366

5467
/**
55-
* @return string|null
68+
* @return string|array|null
5669
*/
57-
public function getFormat(): ?string
70+
public function getFormat(): string|array|null
5871
{
5972
return $this->format;
6073
}
@@ -73,8 +86,20 @@ public function setFormat(string $format): void
7386
#[\Override] public function toArray(): array
7487
{
7588
return [
89+
'if' => $this->if,
7690
'path' => $this->path,
7791
'format' => $this->format,
7892
];
7993
}
94+
95+
public function getIf(): ?string
96+
{
97+
return $this->if;
98+
}
99+
100+
public function setIf(?string $if): void
101+
{
102+
$this->if = $if;
103+
}
104+
80105
}

src/Config/Route.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,16 @@ public function setRouteBlocks(array $routeBlocks): void
7777
}
7878
}
7979

80+
public function getRouteBlock($index): RouteBlock
81+
{
82+
return $this->routeBlocks->offsetGet($index);
83+
}
84+
85+
public function removeRouteBlock($index): void
86+
{
87+
$this->routeBlocks->offsetUnset($index);
88+
}
89+
8090
/**
8191
* @param RouteBlock $routeBlock
8292
* @return void

src/Http/UnitRequest.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@ class UnitRequest
3434
public function __construct(
3535
string $address,
3636
private readonly ?string $socket = null,
37-
private ?ClientInterface $client = null
38-
) {
37+
private ?ClientInterface $client = null
38+
)
39+
{
3940
$this->client = $client ?? new Client([
4041
'base_uri' => $address,
4142
'curl' => $this->socket ? [CURLOPT_UNIX_SOCKET_PATH => $this->socket] : []
@@ -89,6 +90,9 @@ public function send($uri, bool $associative = true, array $requestOptions = [])
8990
try {
9091
$response = $this->client->request($this->method, $this->address . $uri, $requestOptions);
9192
} catch (GuzzleException $e) {
93+
// $responseBody = $e->hasResponse() ? $e->getResponse()->getBody()->getContents() : null;
94+
// throw new UnitException($responseBody ? json_decode($responseBody)['error'] : '');
95+
9296
throw new UnitException($e->getMessage());
9397
}
9498

src/Traits/CanUpload.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
namespace UnitPhpSdk\Traits;
44

5+
use UnitPhpSdk\Abstract\AbstractApplication;
6+
use UnitPhpSdk\Config\Listener;
7+
use UnitPhpSdk\Config\Route;
8+
use UnitPhpSdk\Config\Upstream;
9+
use UnitPhpSdk\Enums\ApiPathEnum;
510
use UnitPhpSdk\Enums\HttpMethodsEnum;
611
use UnitPhpSdk\Exceptions\UnitException;
712
use UnitPhpSdk\Http\UnitRequest;
@@ -48,6 +53,12 @@ protected function removeEmptyArrays($array): array
4853
public function upload(UnitRequest $request)
4954
{
5055
$data = $this->removeEmptyArrays($this->toArray());
56+
$configInstancesThatCanBeNotFound = [
57+
Route::class => ApiPathEnum::ROUTES,
58+
Listener::class => ApiPathEnum::LISTENERS,
59+
AbstractApplication::class => ApiPathEnum::APPLICATIONS,
60+
Upstream::class => ApiPathEnum::UPSTREAMS
61+
];
5162

5263
try {
5364
$request->setMethod(HttpMethodsEnum::PUT)->send(
@@ -56,6 +67,22 @@ public function upload(UnitRequest $request)
5667
['json' => $data]
5768
);
5869
} catch (UnitException $e) {
70+
foreach ($configInstancesThatCanBeNotFound as $configInstance => $path) {
71+
if ($this instanceof $configInstance) {
72+
$request->setMethod(HttpMethodsEnum::PUT)->send(
73+
$path->value,
74+
true,
75+
['json' => (object)[]]
76+
);
77+
78+
$request->setMethod(HttpMethodsEnum::PUT)->send(
79+
$this->getApiEndpoint(),
80+
true,
81+
['json' => $data]
82+
);
83+
return;
84+
}
85+
}
5986
throw new UnitException($e->getMessage());
6087
}
6188
}

src/Unit.php

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
use UnitPhpSdk\Contracts\{CertificateInterface, UnitInterface, Uploadable};
66
use Override;
77
use UnitPhpSdk\Abstract\AbstractApplication;
8+
use UnitPhpSdk\Config\Listener;
9+
use UnitPhpSdk\Config\Route;
10+
use UnitPhpSdk\Config\Upstream;
811
use UnitPhpSdk\Enums\ApiPathEnum;
912
use UnitPhpSdk\Enums\HttpMethodsEnum;
1013
use UnitPhpSdk\Exceptions\FileNotFoundException;
@@ -60,7 +63,8 @@ class Unit implements UnitInterface
6063
public function __construct(
6164
private readonly string $address,
6265
private readonly ?string $socket = null,
63-
) {
66+
)
67+
{
6468
$this->request = new UnitRequest(
6569
address: $this->address,
6670
socket: $this->socket
@@ -145,11 +149,15 @@ public function removeCertificate(string $certificateName): bool
145149
$this->request
146150
->setMethod(HttpMethodsEnum::DELETE)
147151
->send(ApiPathEnum::CERTIFICATE->getPath($certificateName));
148-
} catch (UnitException) {
149-
return false;
150-
}
151152

152-
return true;
153+
return true;
154+
} catch (UnitException $exception) {
155+
if ($exception->getCode() == 404) {
156+
throw new UnitException('Certificate not found');
157+
}
158+
159+
throw new UnitException($exception->getMessage());
160+
}
153161
}
154162

155163
/**
@@ -325,9 +333,9 @@ public function restartApplication(AbstractApplication $application): bool
325333
#[Override] public function toArray(): array
326334
{
327335
return [
328-
'certificates' => array_map(fn ($certificate) => $certificate->toArray(), $this->getCertificates()),
336+
'certificates' => array_map(fn($certificate) => $certificate->toArray(), $this->getCertificates()),
329337
'config' => $this->getConfig()->toArray(),
330-
'js_modules' => array_map(fn ($module) => $module->toArray(), $this->getJsModules()),
338+
'js_modules' => array_map(fn($module) => $module->toArray(), $this->getJsModules()),
331339
'status' => $this->getStatistics()->toArray()
332340
];
333341
}

0 commit comments

Comments
 (0)