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

Commit 95250c5

Browse files
Update RouteMatch.php
1 parent b4102ba commit 95250c5

File tree

5 files changed

+262
-17
lines changed

5 files changed

+262
-17
lines changed

src/Config.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ class Config implements ConfigInterface
2020
private array $_routes = [];
2121

2222
private array $_applications = [];
23-
private array $_new_applications = [];
2423

2524
private array $_upstreams;
2625

@@ -35,8 +34,6 @@ public function __construct(array $data)
3534
$this->_routes[$routeName] = new Route($routeName, $routeData);
3635
}
3736
foreach ($data['applications'] as $appName => $appData) {
38-
// $this->_applications[$appName] = new Application($appName, $appData);
39-
4037
// TODO: implement go and nodejs detect
4138
$this->_applications[$appName] = match ($appData['type']) {
4239
'php' => new Application\PhpApplication($appData),

src/Config/Routes/RouteMatch.php

Lines changed: 240 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,103 @@
22

33
namespace Pavlusha311245\UnitPhpSdk\Config\Routes;
44

5+
use Pavlusha311245\UnitPhpSdk\Enums\HttpMethodsEnum;
56
use Pavlusha311245\UnitPhpSdk\Enums\HttpSchemeEnum;
67

78
class RouteMatch
89
{
10+
/**
11+
* Host header field, converted to lower case and normalized by removing the port number and the trailing period (if any).
12+
*
13+
* @var string
14+
*/
915
private string $_host;
1016

11-
private string|null $_method;
17+
/**
18+
* Method from the request line, uppercased.
19+
*
20+
* @var HttpMethodsEnum
21+
*/
22+
private HttpMethodsEnum $_method;
23+
24+
private string $_source;
25+
26+
/**
27+
* Target IP address and optional port of the request.
28+
*
29+
* @var string
30+
*/
31+
private string $_destination;
32+
33+
/**
34+
* URI scheme. Accepts only two patterns, either http or https.
35+
*
36+
* @var HttpSchemeEnum
37+
*/
38+
private HttpSchemeEnum $_scheme;
39+
40+
/**
41+
* Request target, percent decoded and normalized by removing the query string and
42+
* resolving relative references (“.” and “..”, “//”).
43+
*
44+
* @var array|string
45+
*/
46+
private array|string $_uri;
1247

13-
private string|null $_source;
48+
/**
49+
* Arguments supplied with the request’s query string; these names and value pairs are percent decoded,
50+
* with plus signs (+) replaced by spaces.
51+
*
52+
* @var array
53+
*/
54+
private array $_arguments;
1455

15-
private string|null $_destination;
56+
/**
57+
* Query string, percent decoded, with plus signs (+) replaced by spaces.
58+
*
59+
* @var array
60+
*/
61+
private array $_query;
1662

17-
private HttpSchemeEnum|null $_scheme;
63+
/**
64+
* Cookies supplied with the request.
65+
*
66+
* @var array
67+
*/
68+
private array $_cookies;
1869

19-
private array|string|null $_uri;
70+
/**
71+
* Header fields supplied with the request.
72+
*
73+
* @var array
74+
*/
75+
private array $_headers;
2076

21-
public function __construct($data)
77+
public function __construct($data = null)
2278
{
23-
$this->_uri = $data['uri'] ?? null;
24-
$this->_scheme = $data['scheme'] ?? null;
25-
$this->_method = $data['method'] ?? null;
79+
if (!empty($data)) {
80+
$this->parseFromArray($data);
81+
}
2682
}
2783

2884
/**
2985
* Get method
3086
*
31-
* @return string|null
87+
* @return HttpMethodsEnum
3288
*/
33-
public function getMethod(): ?string
89+
public function getMethod(): HttpMethodsEnum
3490
{
3591
return $this->_method;
3692
}
3793

94+
/**
95+
* @param HttpMethodsEnum $method
96+
*/
97+
public function setMethod(HttpMethodsEnum $method): void
98+
{
99+
$this->_method = $method;
100+
}
101+
38102
/**
39103
* Get uri
40104
*
@@ -69,4 +133,169 @@ public function setScheme(string $scheme): void
69133
default => null
70134
};
71135
}
136+
137+
/**
138+
* @return array
139+
*/
140+
public function getArguments(): array
141+
{
142+
return $this->_arguments;
143+
}
144+
145+
/**
146+
* @param array $arguments
147+
*/
148+
public function setArguments(array $arguments): void
149+
{
150+
$this->_arguments = $arguments;
151+
}
152+
153+
/**
154+
* @return array
155+
*/
156+
public function getCookies(): array
157+
{
158+
return $this->_cookies;
159+
}
160+
161+
/**
162+
* @param array $cookies
163+
*/
164+
public function setCookies(array $cookies): void
165+
{
166+
$this->_cookies = $cookies;
167+
}
168+
169+
/**
170+
* @return string
171+
*/
172+
public function getDestination(): string
173+
{
174+
return $this->_destination;
175+
}
176+
177+
/**
178+
* @param string $destination
179+
*/
180+
public function setDestination(string $destination): void
181+
{
182+
$this->_destination = $destination;
183+
}
184+
185+
/**
186+
* @return array
187+
*/
188+
public function getHeaders(): array
189+
{
190+
return $this->_headers;
191+
}
192+
193+
/**
194+
* @param array $headers
195+
*/
196+
public function setHeaders(array $headers): void
197+
{
198+
$this->_headers = $headers;
199+
}
200+
201+
/**
202+
* @return string
203+
*/
204+
public function getHost(): string
205+
{
206+
return $this->_host;
207+
}
208+
209+
/**
210+
* @param string $host
211+
*/
212+
public function setHost(string $host): void
213+
{
214+
$this->_host = $host;
215+
}
216+
217+
/**
218+
* @return array
219+
*/
220+
public function getQuery(): array
221+
{
222+
return $this->_query;
223+
}
224+
225+
/**
226+
* @param array $query
227+
*/
228+
public function setQuery(array $query): void
229+
{
230+
$this->_query = $query;
231+
}
232+
233+
234+
/**
235+
* @return string
236+
*/
237+
public function getSource(): string
238+
{
239+
return $this->_source;
240+
}
241+
242+
/**
243+
* @param string $source
244+
*/
245+
public function setSource(string $source): void
246+
{
247+
$this->_source = $source;
248+
}
249+
250+
251+
/**
252+
* @param array|string $uri
253+
*/
254+
public function setUri(array|string $uri): void
255+
{
256+
$this->_uri = $uri;
257+
}
258+
259+
public function parseFromArray(array $data): void
260+
{
261+
if (array_key_exists('arguments', $data)) {
262+
$this->setArguments($data['arguments']);
263+
}
264+
265+
if (array_key_exists('cookies', $data)) {
266+
$this->setCookies($data['cookies']);
267+
}
268+
269+
if (array_key_exists('destination', $data)) {
270+
$this->setDestination($data['destination']);
271+
}
272+
273+
if (array_key_exists('headers', $data)) {
274+
$this->setHeaders($data['headers']);
275+
}
276+
277+
if (array_key_exists('host', $data)) {
278+
$this->setHost($data['host']);
279+
}
280+
281+
if (array_key_exists('method', $data)) {
282+
$this->setMethod(HttpMethodsEnum::from(strtoupper($data['method'])));
283+
}
284+
285+
if (array_key_exists('query', $data)) {
286+
$this->setQuery($data['query']);
287+
}
288+
289+
if (array_key_exists('scheme', $data)) {
290+
$this->setScheme(HttpSchemeEnum::from($data['scheme'])->value);
291+
}
292+
293+
if (array_key_exists('source', $data)) {
294+
$this->setSource($data['source']);
295+
}
296+
297+
if (array_key_exists('uri', $data)) {
298+
$this->setUri($data['uri']);
299+
}
300+
}
72301
}

src/Enums/HttpMethodsEnum.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Pavlusha311245\UnitPhpSdk\Enums;
4+
5+
/**
6+
* https://datatracker.ietf.org/doc/html/rfc7231#section-4
7+
*/
8+
enum HttpMethodsEnum: string
9+
{
10+
case GET = 'GET';
11+
case HEAD = 'HEAD';
12+
case POST = 'POST';
13+
case PUT = 'PUT';
14+
case DELETE = 'DELETE';
15+
case CONNECT = 'CONNECT';
16+
case OPTIONS = 'OPTIONS';
17+
case TRACE = 'TRACE';
18+
}

src/Enums/HttpSchemeEnum.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
/**
66
* List of HTTP schemes
77
*/
8-
enum HttpSchemeEnum
8+
enum HttpSchemeEnum: string
99
{
10-
case HTTP;
11-
case HTTPS;
10+
case HTTP = 'http';
11+
case HTTPS = 'https';
1212
}

src/Interfaces/ConfigInterface.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public function getListeners();
1111
public function getListenerByPort(int $port): Listener|null;
1212

1313
public function createListener($data);
14+
1415
public function updateListener($data);
1516

1617
public function getRoutes();

0 commit comments

Comments
 (0)