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

Commit 1ef01dd

Browse files
Add ListenerPass class. Add getListener method
1 parent 300d119 commit 1ef01dd

File tree

3 files changed

+66
-16
lines changed

3 files changed

+66
-16
lines changed

src/Config.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,12 @@ class Config implements ConfigInterface
4646
*
4747
* @var array|mixed
4848
*/
49-
private array $_upstreams;
49+
private array $_upstreams = [];
5050

5151
/**
5252
* Constructor
5353
*
54-
* @throws \Exception
54+
* @throws UnitException
5555
*/
5656
public function __construct(array $data)
5757
{
@@ -76,10 +76,11 @@ public function __construct(array $data)
7676
if (array_key_exists('listeners', $data)) {
7777
foreach ($data['listeners'] as $listener => $listenerData) {
7878
$listener = (new Listener(
79-
_listener: $listener
79+
_listener: $listener,
80+
pass: $listenerData['pass']
8081
))->parseFromArray($listenerData);
81-
$typePath = $listener->getPass()[0];
82-
$typePathName = $listener->getPass()[1];
82+
$typePath = $listener->getPass()->getPassType();
83+
$typePathName = $listener->getPass()->toArray()[1];
8384

8485
($this->{"_{$typePath}"}[$typePathName])->setListener($listener);
8586

@@ -92,6 +93,15 @@ public function __construct(array $data)
9293
}
9394
}
9495

96+
/**
97+
* @param string $listener
98+
* @return Listener|null
99+
*/
100+
public function getListener(string $listener): ?Listener
101+
{
102+
return $this->_listeners[$listener] ?? null;
103+
}
104+
95105
/**
96106
* Get listener by port
97107
*

src/Config/Listener.php

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,19 @@
1010
class Listener
1111
{
1212
private string $_link;
13-
private array $_pass = [];
13+
private ListenerPass $_pass;
1414
private int $_port;
1515

1616
public function __construct(
17-
private string $_listener,
18-
string $pass = '',
19-
private array $_tls = [],
20-
private array $_forwarded = [],
17+
private readonly string $_listener,
18+
string $pass,
19+
private array $_tls = [],
20+
private array $_forwarded = [],
2121
) {
2222
$this->parsePort();
2323
$this->generateLink();
2424

25-
if (!empty($pass)) {
26-
$this->_pass = explode('/', $pass);
27-
}
25+
$this->_pass = new ListenerPass($pass);
2826
}
2927

3028
/**
@@ -83,9 +81,9 @@ public function getForwarded(): array
8381
/**
8482
* Get pass
8583
*
86-
* @return array
84+
* @return ListenerPass
8785
*/
88-
public function getPass(): array
86+
public function getPass(): ListenerPass
8987
{
9088
return $this->_pass;
9189
}
@@ -121,7 +119,6 @@ public function parseFromArray(array $data): Listener
121119
throw new UnitException("Missing required 'pass' array key");
122120
}
123121

124-
$this->_pass = explode('/', $data['pass']);
125122
$this->_forwarded = $data['forwarded'] ?? [];
126123
$this->_tls = $data['tls'] ?? [];
127124

src/Config/ListenerPass.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace Pavlusha311245\UnitPhpSdk\Config;
4+
5+
readonly class ListenerPass
6+
{
7+
private string $_passType;
8+
9+
public function __construct(private string $_data)
10+
{
11+
$this->_passType = explode('/', $_data)[0];
12+
}
13+
14+
/**
15+
* Return pass type (application, routes, route, upstreams)
16+
*
17+
* @return string
18+
*/
19+
public function getPassType(): string
20+
{
21+
return $this->_passType;
22+
}
23+
24+
/**
25+
* Return pass as string
26+
*
27+
* @return string
28+
*/
29+
public function toString(): string
30+
{
31+
return $this->_data;
32+
}
33+
34+
/**
35+
* Return pass as array
36+
*
37+
* @return array
38+
*/
39+
public function toArray(): array
40+
{
41+
return explode('/', $this->_data);
42+
}
43+
}

0 commit comments

Comments
 (0)