1 | <?php |
||
14 | abstract class AbstractConnection implements ConnectionInterface |
||
15 | { |
||
16 | private const RECEIVE_BYTES = 1024; |
||
17 | |||
18 | /** |
||
19 | * @var resource|null |
||
20 | */ |
||
21 | protected $resource; |
||
22 | |||
23 | /** |
||
24 | * @var array<string, array<int, \Closure>> |
||
25 | */ |
||
26 | private $listeners = [ |
||
27 | 'connect' => [] |
||
28 | ]; |
||
29 | |||
30 | final public function __destruct() |
||
31 | { |
||
32 | $this->disconnect(); |
||
33 | } |
||
34 | |||
35 | /** |
||
36 | * @param string $name |
||
37 | * @param \Closure $callback |
||
38 | */ |
||
39 | final public function addListener(string $name, \Closure $callback): void |
||
40 | { |
||
41 | $this->listeners[$name][] = $callback; |
||
42 | } |
||
43 | |||
44 | /** |
||
45 | * @param string $name |
||
46 | */ |
||
47 | final protected function fireEvent(string $name): void |
||
48 | { |
||
49 | if (!isset($this->listeners[$name])) { |
||
50 | return; |
||
51 | } |
||
52 | |||
53 | foreach ($this->listeners[$name] as $listener) { |
||
54 | $listener(); |
||
55 | } |
||
56 | } |
||
57 | |||
58 | abstract public function connect(): void; |
||
59 | |||
60 | final public function disconnect(): void |
||
61 | { |
||
62 | if ($this->resource !== null) { |
||
67 | |||
68 | /** |
||
69 | * @param float $timeout |
||
70 | */ |
||
71 | final public function timeout(float $timeout): void |
||
75 | |||
76 | /** |
||
77 | * @param string $request |
||
78 | * @return int |
||
79 | * @throws CannotWriteToStreamException |
||
80 | */ |
||
81 | final public function send(string $request): int |
||
91 | |||
92 | /** |
||
93 | * @return string |
||
94 | */ |
||
95 | final public function receive(): string |
||
107 | |||
108 | /** |
||
109 | * @param array<int, string> $keys |
||
110 | * @return array<string, mixed> |
||
|
|||
111 | */ |
||
112 | final public function getMetaData(array $keys = []): array |
||
128 | |||
129 | /** |
||
130 | * @return resource |
||
131 | */ |
||
132 | private function verifyConnection() |
||
140 | |||
141 | /** |
||
142 | * @return resource |
||
143 | * @throws ConnectionClosedException |
||
144 | * @throws ConnectionTimeoutException |
||
145 | */ |
||
146 | private function verifyAlive() |
||
160 | } |
||
161 |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.