1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: conci |
5
|
|
|
* Date: 10/24/17 |
6
|
|
|
* Time: 11:08 AM |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace sonrac\WAMP; |
10
|
|
|
|
11
|
|
|
use sonrac\WAMP\Exceptions\InvalidWampTransportProvider; |
12
|
|
|
use Thruway\Transport\TransportProviderInterface; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class WAMP |
16
|
|
|
* |
17
|
|
|
* @package sonrac\WAMP |
18
|
|
|
*/ |
19
|
|
|
class WAMP |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* WAMP host |
23
|
|
|
* |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected $host; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* WAMP realm |
30
|
|
|
* |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
protected $realm; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* WAMP port |
37
|
|
|
* |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
protected $port; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* WAMP path |
44
|
|
|
* |
45
|
|
|
* @var int|string |
46
|
|
|
*/ |
47
|
|
|
protected $path; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Debug mode is enabled |
51
|
|
|
* |
52
|
|
|
* @var bool |
53
|
|
|
*/ |
54
|
|
|
protected $debug = false; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* In background run mode is enable |
58
|
|
|
* |
59
|
|
|
* @var bool |
60
|
|
|
*/ |
61
|
|
|
protected $inBackground = false; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Use WAMP security connection |
65
|
|
|
* |
66
|
|
|
* @var bool |
67
|
|
|
*/ |
68
|
|
|
protected $tls = false; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @var \Thruway\Transport\TransportInterface |
72
|
|
|
*/ |
73
|
|
|
protected $transportProvider = 'Thruway\Transport\RatchetTransportProvider'; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* WAMP constructor. |
77
|
|
|
* |
78
|
|
|
* @param string $host WAMP host |
79
|
|
|
* @param string|int $port WAMP port |
80
|
|
|
* @param string $path WAMP path |
81
|
|
|
* @param string $realm WAMP realm |
82
|
|
|
* @param bool $tls Wamp security enable |
83
|
|
|
* @param bool $debug WAMP debug is enabled |
84
|
|
|
* @param bool $inBackground WAMP in background mode. |
85
|
|
|
* @param \Thruway\Transport\TransportInterface $transportProvider Transport provider class |
86
|
|
|
* |
87
|
|
|
* @thrown InvalidWampTransportProvider |
88
|
|
|
*/ |
89
|
|
|
public function __construct( |
90
|
|
|
$host = null, |
91
|
|
|
$port = null, |
92
|
|
|
$path = null, |
93
|
|
|
$realm = null, |
94
|
|
|
$tls = false, |
95
|
|
|
$debug = false, |
96
|
|
|
$inBackground = false, |
97
|
|
|
$transportProvider = null |
98
|
|
|
) { |
99
|
|
|
$this->host = $host ?? $this->getConfig('host'); |
100
|
|
|
$this->port = $port ?? $this->getConfig('port'); |
101
|
|
|
$this->realm = $realm ?? $this->getConfig('realm'); |
102
|
|
|
$this->tls = $tls ?? $this->getConfig('tls'); |
103
|
|
|
$this->debug = $debug ?? $this->getConfig('debug'); |
104
|
|
|
$this->inBackground = $inBackground ?? $this->getConfig('inBackground'); |
105
|
|
|
$this->path = $path ?? $this->getConfig('path'); |
106
|
|
|
$this->transportProvider = $transportProvider ?? $this->getConfig('transportProvider'); |
107
|
|
|
|
108
|
|
|
if (!class_exists($this->transportProvider) || |
|
|
|
|
109
|
|
|
!((new \ReflectionClass($this->transportProvider))->implementsInterface( |
110
|
|
|
'\Thruway\Transport\TransportInterface' |
111
|
|
|
)) |
112
|
|
|
) { |
113
|
|
|
throw new InvalidWampTransportProvider(); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Get value from config |
119
|
|
|
* |
120
|
|
|
* @param string $name Option name |
121
|
|
|
* @param null|string $propName Property name |
122
|
|
|
* |
123
|
|
|
* @return mixed |
124
|
|
|
*/ |
125
|
|
|
protected function getConfig($name, $propName = null) |
126
|
|
|
{ |
127
|
|
|
$propName = $propName ?? $name; |
128
|
|
|
$options = config('wamp'); |
129
|
|
|
|
130
|
|
|
if (null === $options) { |
131
|
|
|
return $this->{$propName}; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
if (isset($options[$name])) { |
135
|
|
|
return $options[$name]; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
return $this->{$propName}; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Get transport provider |
143
|
|
|
* |
144
|
|
|
* @return \Thruway\Transport\TransportProviderInterface |
145
|
|
|
*/ |
146
|
|
|
protected function getTransportProvider(): TransportProviderInterface |
147
|
|
|
{ |
148
|
|
|
return new $this->transportProvider(); |
|
|
|
|
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|