1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace RouterOS; |
4
|
|
|
|
5
|
|
|
use RouterOS\Exceptions\ClientException; |
6
|
|
|
|
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
|
8 |
|
private function openSocket(): void |
38
|
|
|
{ |
39
|
|
|
// Default: Context for ssl |
40
|
8 |
|
$context = stream_context_create([ |
41
|
|
|
'ssl' => [ |
42
|
8 |
|
'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
|
8 |
|
$proto = $this->config('ssl') ? 'ssl://' : ''; |
|
|
|
|
50
|
|
|
|
51
|
|
|
// Initiate socket client |
52
|
8 |
|
$socket = @stream_socket_client( |
53
|
8 |
|
$proto . $this->config('host') . ':' . $this->config('port'), |
|
|
|
|
54
|
8 |
|
$this->socket_err_num, |
55
|
8 |
|
$this->socket_err_str, |
56
|
8 |
|
$this->config('timeout'), |
|
|
|
|
57
|
8 |
|
STREAM_CLIENT_CONNECT, |
58
|
|
|
$context |
59
|
|
|
); |
60
|
|
|
|
61
|
|
|
// Throw error is socket is not initiated |
62
|
8 |
|
if (false === $socket) { |
63
|
1 |
|
throw new ClientException('Unable to establish socket session, ' . $this->socket_err_str); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
//Timeout read |
67
|
7 |
|
stream_set_timeout($socket, $this->config('timeout')); |
|
|
|
|
68
|
|
|
|
69
|
|
|
// Save socket to static variable |
70
|
7 |
|
$this->setSocket($socket); |
71
|
7 |
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Close socket session |
75
|
|
|
* |
76
|
|
|
* @return bool |
77
|
|
|
*/ |
78
|
|
|
private function closeSocket(): bool |
79
|
|
|
{ |
80
|
|
|
return fclose($this->socket); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Save socket resource to static variable |
85
|
|
|
* |
86
|
|
|
* @param resource $socket |
87
|
|
|
* |
88
|
|
|
* @return void |
89
|
|
|
*/ |
90
|
7 |
|
private function setSocket($socket): void |
91
|
|
|
{ |
92
|
7 |
|
$this->socket = $socket; |
93
|
7 |
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Return socket resource if is exist |
97
|
|
|
* |
98
|
|
|
* @return resource |
99
|
|
|
*/ |
100
|
7 |
|
public function getSocket() |
101
|
|
|
{ |
102
|
7 |
|
return $this->socket; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
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.