1 | <?php |
||
7 | trait SocketTrait |
||
8 | { |
||
9 | /** |
||
10 | * Socket resource |
||
11 | * |
||
12 | * @var resource|null |
||
13 | */ |
||
14 | private $_socket; |
||
15 | |||
16 | /** |
||
17 | * Code of error |
||
18 | * |
||
19 | * @var int |
||
20 | */ |
||
21 | private $_socket_err_num; |
||
22 | |||
23 | /** |
||
24 | * Description of socket error |
||
25 | * |
||
26 | * @var string |
||
27 | */ |
||
28 | private $_socket_err_str; |
||
29 | |||
30 | /** |
||
31 | * Initiate socket session |
||
32 | * |
||
33 | * @return void |
||
34 | * @throws \RouterOS\Exceptions\ClientException |
||
35 | * @throws \RouterOS\Exceptions\ConfigException |
||
36 | */ |
||
37 | 16 | private function openSocket() |
|
38 | { |
||
39 | // Default: Context for ssl |
||
40 | 16 | $context = stream_context_create([ |
|
41 | 16 | 'ssl' => [ |
|
42 | 'ciphers' => 'ADH:ALL', |
||
43 | 'verify_peer' => false, |
||
44 | 'verify_peer_name' => false |
||
45 | ] |
||
46 | ]); |
||
47 | |||
48 | // Default: Proto tcp:// but for ssl we need ssl:// |
||
49 | 16 | $proto = $this->config('ssl') ? 'ssl://' : ''; |
|
|
|||
50 | |||
51 | // Initiate socket client |
||
52 | 16 | $socket = @stream_socket_client( |
|
53 | 16 | $proto . $this->config('host') . ':' . $this->config('port'), |
|
54 | 16 | $this->_socket_err_num, |
|
55 | 16 | $this->_socket_err_str, |
|
56 | 16 | $this->config('timeout'), |
|
57 | 16 | STREAM_CLIENT_CONNECT, |
|
58 | 16 | $context |
|
59 | ); |
||
60 | |||
61 | // Throw error is socket is not initiated |
||
62 | 16 | if (false === $socket) { |
|
63 | 1 | throw new ClientException('Unable to establish socket session, ' . $this->_socket_err_str); |
|
64 | } |
||
65 | |||
66 | //Timeout read |
||
67 | 15 | stream_set_timeout($socket, $this->config('timeout')); |
|
68 | |||
69 | // Save socket to static variable |
||
70 | 15 | $this->setSocket($socket); |
|
71 | 15 | } |
|
72 | |||
73 | /** |
||
74 | * Close socket session |
||
75 | * |
||
76 | * @return bool |
||
77 | */ |
||
78 | private function closeSocket(): bool |
||
82 | |||
83 | /** |
||
84 | * Save socket resource to static variable |
||
85 | * |
||
86 | * @param resource $socket |
||
87 | * @return void |
||
88 | */ |
||
89 | 15 | private function setSocket($socket) |
|
93 | |||
94 | /** |
||
95 | * Return socket resource if is exist |
||
96 | * |
||
97 | * @return resource |
||
98 | */ |
||
99 | 15 | public function getSocket() |
|
103 | } |
||
104 |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.