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

Commit 8f0c836

Browse files
Update route blocks.
Add exception.
1 parent 5d44057 commit 8f0c836

File tree

13 files changed

+3040
-1761
lines changed

13 files changed

+3040
-1761
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
.idea
22
vendor
3+
.php-cs-fixer.cache

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"minimum-stability": "stable",
2929
"require": {
3030
"php": ">=8.2",
31+
"friendsofphp/php-cs-fixer": "^3.21",
3132
"ext-curl": "*"
3233
},
3334
"require-dev": {

composer.lock

Lines changed: 2806 additions & 1729 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Config.php

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,8 @@ public function __construct(array $data)
3333

3434
foreach ($data['listeners'] as $listener => $listenerData) {
3535
$listener = (new Listener(
36-
listener: $listener
36+
_listener: $listener
3737
))->parseFromArray($listenerData);
38-
3938
$typePath = $listener->getPass()[0];
4039
$typePathName = $listener->getPass()[1];
4140

@@ -100,13 +99,37 @@ public function getRoute($routeName)
10099
}
101100

102101
/**
102+
* Get upstreams
103+
*
103104
* @return mixed|null
104105
*/
105106
public function getUpstreams(): mixed
106107
{
107108
return $this->_upstreams;
108109
}
109110

111+
/**
112+
* Setup access log file path
113+
*
114+
* @return void
115+
*/
116+
public function setApplicationLogPath()
117+
{
118+
// TODO: Implement setApplicationLogPath() method.
119+
// Implement functions from this source https://unit.nginx.org/configuration/#access-log
120+
}
121+
122+
/**
123+
* Setup access log file format
124+
*
125+
* @return void
126+
*/
127+
public function setApplicationLogFormat()
128+
{
129+
// TODO: Implement setApplicationLogFormat() method.
130+
// Implement functions from this source https://unit.nginx.org/configuration/#access-log
131+
}
132+
110133
/**
111134
* Return config as array
112135
*

src/Config/Listener.php

Lines changed: 73 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,43 +2,105 @@
22

33
namespace Pavlusha311245\UnitPhpSdk\Config;
44

5+
use Pavlusha311245\UnitPhpSdk\Exceptions\UnitException;
6+
57
class Listener
68
{
7-
private $_link;
9+
private string $_link;
810
private array $_pass = [];
9-
private string $_listener;
10-
private $_port;
11+
private int $_port;
1112

1213
public function __construct(
13-
string $listener,
14-
string $pass = '',
15-
private array $_tls = [],
16-
private array $_forwarded = [],
14+
private string $_listener,
15+
string $pass = '',
16+
private array $_tls = [],
17+
private array $_forwarded = [],
1718
)
1819
{
19-
$this->_listener = $listener;
20-
$this->_port = explode(':', $listener)[1];
20+
$this->parsePort();
21+
$this->generateLink();
22+
2123
if (!empty($pass)) {
2224
$this->_pass = explode('/', $pass);
2325
}
2426
}
2527

28+
/**
29+
* Get link
30+
*
31+
* @return string
32+
*/
33+
public function getLink(): string
34+
{
35+
return $this->_link;
36+
}
37+
38+
/**
39+
* Generate link from listener
40+
*
41+
* @return void
42+
*/
43+
private function generateLink()
44+
{
45+
$separatedListener = explode(':', $this->_listener);
46+
47+
$this->_link = $separatedListener[0] == '*' ?
48+
"0.0.0.0:{$separatedListener[1]}" : $separatedListener;
49+
}
50+
51+
/**
52+
* Parse port
53+
*
54+
* @return void
55+
*/
56+
private function parsePort(): void
57+
{
58+
$this->_port = explode(':', $this->_listener)[1];
59+
}
60+
61+
/**
62+
* Get port
63+
*
64+
* @return int
65+
*/
66+
public function getPort(): int
67+
{
68+
return $this->_port;
69+
}
70+
71+
/**
72+
* Get forwarded
73+
*
74+
* @return array
75+
*/
2676
public function getForwarded(): array
2777
{
2878
return $this->_forwarded;
2979
}
3080

81+
/**
82+
* Get pass
83+
*
84+
* @return array
85+
*/
3186
public function getPass(): array
3287
{
3388
return $this->_pass;
3489
}
3590

91+
/**
92+
* Get tls section
93+
*
94+
* @return array
95+
*/
3696
public function getTls(): array
3797
{
3898
return $this->_tls;
3999
}
40100

41101
/**
102+
* Get listener
103+
*
42104
* @return mixed
43105
*/
44106
public function getListener()
@@ -49,12 +111,12 @@ public function getListener()
49111
/**
50112
* Parse data from array
51113
*
52-
* @throws \Exception
114+
* @throws UnitException
53115
*/
54116
public function parseFromArray(array $data): Listener
55117
{
56118
if (!array_key_exists('pass', $data)) {
57-
throw new \Exception("Missing required 'pass' array key");
119+
throw new UnitException("Missing required 'pass' array key");
58120
}
59121

60122
$this->_pass = explode('/', $data['pass']);

src/Config/Routes/RouteAction.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,14 @@ class RouteAction
88
private string $_proxy;
99
private int $_return;
1010
private string $_location;
11-
private $_share;
11+
private array|string $_share;
1212
private string $_rewrite;
1313

14+
public function __construct($data)
15+
{
16+
$this->_share = $data['share'] ?? null;
17+
}
18+
1419
/**
1520
* Receive return key
1621
*
@@ -28,8 +33,7 @@ public function getReturn()
2833
*/
2934
public function setReturn(int $return): void
3035
{
31-
if ($return > 999 && $return < 0)
32-
{
36+
if ($return > 999 && $return < 0) {
3337
throw new \OutOfRangeException();
3438
}
3539

src/Config/Routes/RouteBlock.php

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,37 @@
44

55
class RouteBlock
66
{
7-
private $_action;
7+
private RouteAction $_action;
88

99
private $_match;
1010

1111
public function __construct(array $data)
1212
{
13-
$this->_action = $data['action'];
14-
$this->_match = $data['match'] ?? null;
13+
// $this->_action = $data['action'];
14+
// $this->_match = $data['match'] ?? null;
15+
16+
$this->_action = new RouteAction($data['action']);
17+
if (isset($data['match']))
18+
{
19+
$this->_match = new RouteMatch($data['match']);
20+
}
1521
}
1622

1723
/**
18-
* @return mixed|null
24+
* Get match
25+
*
26+
* @return RouteMatch
1927
*/
20-
public function getMatch(): mixed
28+
public function getMatch()
2129
{
2230
return $this->_match;
2331
}
2432

33+
/**
34+
* Get action
35+
*
36+
* @return mixed
37+
*/
2538
public function getAction(): mixed
2639
{
2740
return $this->_action;

src/Config/Routes/RouteMatch.php

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,70 @@
22

33
namespace Pavlusha311245\UnitPhpSdk\Config\Routes;
44

5+
use Pavlusha311245\UnitPhpSdk\Enums\HttpSchemeEnum;
6+
57
class RouteMatch
68
{
79
private string $_host;
810

9-
private string $_method;
11+
private string|null $_method;
12+
13+
private string|null $_source;
1014

11-
private string $_source;
15+
private string|null $_destination;
1216

13-
private string $_destination;
17+
private HttpSchemeEnum|null $_scheme;
1418

15-
private string $_scheme;
19+
private array|string|null $_uri;
1620

17-
private array|string $_uri;
21+
public function __construct($data)
22+
{
23+
$this->_uri = $data['uri'] ?? null;
24+
$this->_scheme = $data['scheme'] ?? null;
25+
$this->_method = $data['method'] ?? null;
26+
}
27+
28+
/**
29+
* Get method
30+
*
31+
* @return string|null
32+
*/
33+
public function getMethod(): ?string
34+
{
35+
return $this->_method;
36+
}
37+
38+
/**
39+
* Get uri
40+
*
41+
* @return array|string|null
42+
*/
43+
public function getUri(): array|string|null
44+
{
45+
return $this->_uri;
46+
}
47+
48+
/**
49+
* Get http scheme
50+
*
51+
* @return HttpSchemeEnum|null
52+
*/
53+
public function getScheme(): ?HttpSchemeEnum
54+
{
55+
return $this->_scheme;
56+
}
1857

58+
/**
59+
* Set HTTP scheme
60+
*
61+
* @param string $scheme
62+
* @return void
63+
*/
1964
public function setScheme(string $scheme): void
2065
{
2166
$this->_scheme = match ($scheme) {
22-
'http' || 'https' => $scheme,
67+
'http' => HttpSchemeEnum::HTTP,
68+
'https' => HttpSchemeEnum::HTTPS,
2369
default => null
2470
};
2571
}

src/Config/Statistic.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,31 @@ public function __construct(array $data)
1919
$this->_applications = $data['applications'];
2020
}
2121

22+
/**
23+
* Get connections
24+
*
25+
* @return array
26+
*/
2227
public function getConnections(): array
2328
{
2429
return $this->_connections;
2530
}
2631

32+
/**
33+
* Get requests
34+
*
35+
* @return array
36+
*/
2737
public function getRequests(): array
2838
{
2939
return $this->_requests;
3040
}
3141

42+
/**
43+
* Get an applications
44+
*
45+
* @return array
46+
*/
3247
public function getApplications(): array
3348
{
3449
return $this->_applications;

src/Enums/HttpSchemeEnum.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace Pavlusha311245\UnitPhpSdk\Enums;
4+
5+
enum HttpSchemeEnum
6+
{
7+
case HTTP;
8+
case HTTPS;
9+
}

0 commit comments

Comments
 (0)