|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* |
|
5
|
|
|
* @author Donii Sergii <[email protected]> |
|
6
|
|
|
* Date: 10/24/17 |
|
7
|
|
|
* Time: 11:08 AM |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace sonrac\WAMP; |
|
11
|
|
|
|
|
12
|
|
|
use sonrac\WAMP\Exceptions\InvalidWampTransportProvider; |
|
13
|
|
|
use Thruway\Peer\Client; |
|
|
|
|
|
|
14
|
|
|
use Thruway\Transport\TransportProviderInterface; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Class WAMP. |
|
18
|
|
|
* |
|
19
|
|
|
* @summary WAMP Server adapter for lumen|laravel |
|
20
|
|
|
*/ |
|
21
|
|
|
class WAMP |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* WAMP host. |
|
25
|
|
|
* |
|
26
|
|
|
* @var string |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $host; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* WAMP realm. |
|
32
|
|
|
* |
|
33
|
|
|
* @var string |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $realm; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* WAMP port. |
|
39
|
|
|
* |
|
40
|
|
|
* @var string |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $port; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* WAMP path. |
|
46
|
|
|
* |
|
47
|
|
|
* @var int|string |
|
48
|
|
|
*/ |
|
49
|
|
|
protected $path; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Debug mode is enabled. |
|
53
|
|
|
* |
|
54
|
|
|
* @var bool |
|
55
|
|
|
*/ |
|
56
|
|
|
protected $debug = false; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* In background run mode is enable. |
|
60
|
|
|
* |
|
61
|
|
|
* @var bool |
|
62
|
|
|
*/ |
|
63
|
|
|
protected $inBackground = false; |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Use WAMP security connection. |
|
67
|
|
|
* |
|
68
|
|
|
* @var bool |
|
69
|
|
|
*/ |
|
70
|
|
|
protected $tls = false; |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Transport provider class. |
|
74
|
|
|
* |
|
75
|
|
|
* @var \Thruway\Transport\TransportInterface |
|
76
|
|
|
*/ |
|
77
|
|
|
protected $transportProvider = 'Thruway\Transport\RatchetTransportProvider'; |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Clients. |
|
81
|
|
|
* |
|
82
|
|
|
* @var array |
|
83
|
|
|
* |
|
84
|
|
|
* @author Donii Sergii <[email protected]> |
|
85
|
|
|
*/ |
|
86
|
|
|
protected $clients = []; |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* WAMP router. |
|
90
|
|
|
* |
|
91
|
|
|
* @var mixed|\sonrac\WAMP\Contracts\WAMPRouterInterface|\sonrac\WAMP\Routers\Router |
|
92
|
|
|
* |
|
93
|
|
|
* @author Donii Sergii <[email protected]> |
|
94
|
|
|
*/ |
|
95
|
|
|
public $router = null; |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* WAMP constructor. |
|
99
|
|
|
* |
|
100
|
|
|
* @param string $host WAMP host |
|
101
|
|
|
* @param string|int $port WAMP port |
|
102
|
|
|
* @param string $path WAMP path |
|
103
|
|
|
* @param string $realm WAMP realm |
|
104
|
|
|
* @param bool $tls Wamp security enable |
|
105
|
|
|
* @param bool $debug WAMP debug is enabled |
|
106
|
|
|
* @param bool $inBackground WAMP in background mode. |
|
107
|
|
|
* @param \Thruway\Transport\TransportInterface $transportProvider Transport provider class |
|
108
|
|
|
* |
|
109
|
|
|
* @throws InvalidWampTransportProvider |
|
110
|
|
|
*/ |
|
111
|
|
|
public function __construct( |
|
112
|
|
|
$host = null, |
|
113
|
|
|
$port = null, |
|
114
|
|
|
$path = null, |
|
115
|
|
|
$realm = null, |
|
116
|
|
|
$tls = false, |
|
117
|
|
|
$debug = false, |
|
118
|
|
|
$inBackground = false, |
|
119
|
|
|
$transportProvider = null |
|
120
|
|
|
) { |
|
121
|
|
|
$this->host = $host ?? $this->getConfig('host'); |
|
122
|
|
|
$this->port = $port ?? $this->getConfig('port'); |
|
123
|
|
|
$this->realm = $realm ?? $this->getConfig('realm'); |
|
124
|
|
|
$this->tls = $tls ?? $this->getConfig('tls'); |
|
125
|
|
|
$this->debug = $debug ?? $this->getConfig('debug'); |
|
126
|
|
|
$this->inBackground = $inBackground ?? $this->getConfig('inBackground'); |
|
127
|
|
|
$this->path = $path ?? $this->getConfig('path'); |
|
128
|
|
|
$this->transportProvider = $transportProvider ?? $this->getConfig('transportProvider'); |
|
129
|
|
|
|
|
130
|
|
|
if (!class_exists($this->transportProvider) || |
|
|
|
|
|
|
131
|
|
|
!((new \ReflectionClass($this->transportProvider))->implementsInterface( |
|
132
|
|
|
'\Thruway\Transport\TransportInterface' |
|
133
|
|
|
)) |
|
134
|
|
|
) { |
|
135
|
|
|
throw new InvalidWampTransportProvider(); |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Get router. |
|
141
|
|
|
* |
|
142
|
|
|
* @return mixed|null|\sonrac\WAMP\Contracts\WAMPRouterInterface|\sonrac\WAMP\Routers\Router |
|
143
|
|
|
*/ |
|
144
|
|
|
public function getRouter() |
|
145
|
|
|
{ |
|
146
|
|
|
return $this->router ?? $this->setupRouter(); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Get client. |
|
151
|
|
|
* |
|
152
|
|
|
* @param string|null $realm Realm |
|
153
|
|
|
* |
|
154
|
|
|
* @return \sonrac\WAMP\Client|\Thruway\Peer\ClientInterface |
|
155
|
|
|
* |
|
156
|
|
|
* @author Donii Sergii <[email protected]> |
|
157
|
|
|
*/ |
|
158
|
|
View Code Duplication |
public function getClient($realm = null) |
|
|
|
|
|
|
159
|
|
|
{ |
|
160
|
|
|
$realm = $realm ?? $this->realm; |
|
161
|
|
|
if (!isset($this->clients[$realm])) { |
|
162
|
|
|
$this->clients[$realm] = new Client($realm); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
return $this->clients[$realm]; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* Create and get client with new realm. |
|
170
|
|
|
* |
|
171
|
|
|
* @param null|string $realm |
|
172
|
|
|
* |
|
173
|
|
|
* @return mixed|\sonrac\WAMP\Client|\Thruway\Peer\ClientInterface |
|
174
|
|
|
* |
|
175
|
|
|
* @author Donii Sergii <[email protected]> |
|
176
|
|
|
*/ |
|
177
|
|
View Code Duplication |
public function createClient($realm = null) |
|
|
|
|
|
|
178
|
|
|
{ |
|
179
|
|
|
$realm = $realm ?? $this->realm; |
|
180
|
|
|
|
|
181
|
|
|
if ($realm === $this->realm && isset($this->clients[$realm])) { |
|
182
|
|
|
return $this->clients[$realm]; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
return $this->clients[$realm] = new Client($realm); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* Get value from config. |
|
190
|
|
|
* |
|
191
|
|
|
* @param string $name Option name |
|
192
|
|
|
* @param null|string $propName Property name |
|
193
|
|
|
* |
|
194
|
|
|
* @return mixed |
|
195
|
|
|
*/ |
|
196
|
|
|
protected function getConfig($name, $propName = null) |
|
197
|
|
|
{ |
|
198
|
|
|
$propName = $propName ?? $name; |
|
199
|
|
|
$options = config('wamp'); |
|
|
|
|
|
|
200
|
|
|
|
|
201
|
|
|
if (null === $options) { |
|
202
|
|
|
return $this->{$propName}; |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
if (isset($options[$name])) { |
|
206
|
|
|
return $options[$name]; |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
return $this->{$propName}; |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* Setup router. |
|
214
|
|
|
* |
|
215
|
|
|
* @return mixed|\sonrac\WAMP\Contracts\WAMPRouterInterface|\sonrac\WAMP\Routers\Router |
|
216
|
|
|
*/ |
|
217
|
|
|
protected function setupRouter() |
|
218
|
|
|
{ |
|
219
|
|
|
$this->router = app()->wampRouter; |
|
|
|
|
|
|
220
|
|
|
$this->router->setClient($this->getClient()); |
|
221
|
|
|
$this->router->register(); |
|
222
|
|
|
return $this->router; |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
/** |
|
226
|
|
|
* Get transport provider. |
|
227
|
|
|
* |
|
228
|
|
|
* @return \Thruway\Transport\TransportProviderInterface |
|
229
|
|
|
*/ |
|
230
|
|
|
protected function getTransportProvider(): TransportProviderInterface |
|
231
|
|
|
{ |
|
232
|
|
|
return new $this->transportProvider(); |
|
233
|
|
|
} |
|
234
|
|
|
} |
|
235
|
|
|
|
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: