1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: Donii Sergii <[email protected]> |
5
|
|
|
* Date: 11/3/17 |
6
|
|
|
* Time: 6:08 PM |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace sonrac\WAMP\Commands; |
10
|
|
|
|
11
|
|
|
use Thruway\Logging\ConsoleLogger; |
12
|
|
|
use Thruway\Logging\Logger; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @trait WAMPCommandTrait |
16
|
|
|
* |
17
|
|
|
* @author Donii Sergii <[email protected]> |
18
|
|
|
*/ |
19
|
|
|
trait WAMPCommandTrait |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* WAMP router host. |
23
|
|
|
* |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected $host = '127.0.0.1'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Wamp realm to used. |
30
|
|
|
* |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
protected $realm = 'realm'; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* WAMP router port. |
37
|
|
|
* |
38
|
|
|
* @var int |
39
|
|
|
*/ |
40
|
|
|
protected $port = '9090'; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Run in debug mode. Echo output to console. |
44
|
|
|
* |
45
|
|
|
* @var bool |
46
|
|
|
*/ |
47
|
|
|
protected $noDebug = false; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Run in loop or once. |
51
|
|
|
* |
52
|
|
|
* @var bool |
53
|
|
|
*/ |
54
|
|
|
protected $runOnce = false; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Specify the router protocol as wss. |
58
|
|
|
* |
59
|
|
|
* @var bool |
60
|
|
|
*/ |
61
|
|
|
protected $tls = false; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* WAMP routes path. |
65
|
|
|
* |
66
|
|
|
* @var string |
67
|
|
|
* |
68
|
|
|
* @author Donii Sergii <[email protected]> |
69
|
|
|
*/ |
70
|
|
|
protected $routePath = null; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Transport provider class. |
74
|
|
|
* |
75
|
|
|
* @var string|\Thruway\Transport\RatchetTransportProvider|null |
76
|
|
|
* |
77
|
|
|
* @author Donii Sergii <[email protected]> |
78
|
|
|
*/ |
79
|
|
|
protected $transportProvider = 'Thruway\Transport\RatchetTransportProvider'; |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Run in background. |
83
|
|
|
* |
84
|
|
|
* @var bool |
85
|
|
|
* |
86
|
|
|
* @author Donii Sergii <[email protected]> |
87
|
|
|
*/ |
88
|
|
|
protected $runInBackground = false; |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* No loop runner |
92
|
|
|
* |
93
|
|
|
* @var bool |
94
|
|
|
* |
95
|
|
|
* @author Donii Sergii <[email protected]> |
96
|
|
|
*/ |
97
|
|
|
protected $noLoop = false; |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Get option value from input. |
101
|
|
|
* |
102
|
|
|
* @param string $optionName |
103
|
|
|
* |
104
|
|
|
* @return array|null|string |
105
|
|
|
*/ |
106
|
|
|
protected function getOptionFromInput(string $optionName, $default = null) |
107
|
|
|
{ |
108
|
|
|
if (null === $this->input->getOption($optionName)) { |
109
|
|
|
return $default; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return $this->option($optionName); |
|
|
|
|
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Get minion config. |
117
|
|
|
* |
118
|
|
|
* @param string|null $optName Option name |
119
|
|
|
* @param mixed $default Default value |
120
|
|
|
* |
121
|
|
|
* @return array|string|int|null |
122
|
|
|
*/ |
123
|
|
|
protected function getConfig($optName = null, $default = null) |
124
|
|
|
{ |
125
|
|
|
$options = config('wamp') ?? []; |
|
|
|
|
126
|
|
|
|
127
|
|
|
if (null === $optName) { |
128
|
|
|
return $options ?? []; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
return isset($options[$optName]) ? $options[$optName] : $default; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Get logger class for wamp |
136
|
|
|
* |
137
|
|
|
* @return \Psr\Log\LoggerInterface |
138
|
|
|
* |
139
|
|
|
* @author Donii Sergii <[email protected]> |
140
|
|
|
*/ |
141
|
|
|
abstract protected function getLogger(); |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Change WAMP logger |
145
|
|
|
* |
146
|
|
|
* @param string $fileName |
147
|
|
|
* |
148
|
|
|
* @return void |
149
|
|
|
* |
150
|
|
|
* @author Donii Sergii <[email protected]> |
151
|
|
|
*/ |
152
|
|
|
protected function changeWampLogger($fileName = 'wamp-server.log') |
|
|
|
|
153
|
|
|
{ |
154
|
|
|
if (!$this->noDebug) { |
155
|
|
|
return; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
Logger::set($this->getLogger()); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Parse base options |
163
|
|
|
* |
164
|
|
|
* @author Donii Sergii <[email protected]> |
165
|
|
|
*/ |
166
|
|
|
protected function parseBaseOptions() |
167
|
|
|
{ |
168
|
|
|
$this->host = $this->getOptionFromInput('host') ?? $this->getConfig('host', $this->host); |
|
|
|
|
169
|
|
|
$this->port = $this->getOptionFromInput('port') ?? $this->getConfig('port', $this->port); |
170
|
|
|
$this->realm = $this->getOptionFromInput('realm') ?? $this->getConfig('realm', $this->realm); |
|
|
|
|
171
|
|
|
$this->tls = $this->getOptionFromInput('tls') ?? $this->getConfig('tls', $this->tls); |
172
|
|
|
$this->transportProvider = $this->getOptionFromInput('transport-provider') ?? $this->getConfig('transportProvider', |
|
|
|
|
173
|
|
|
$this->transportProvider); |
174
|
|
|
$this->runInBackground = $this->getOptionFromInput('in-background') ?? false; |
175
|
|
|
|
176
|
|
|
$this->noDebug = $this->getOptionFromInput('no-debug') ?? $this->noDebug; |
177
|
|
|
$this->runOnce = $this->getOptionFromInput('no-loop') ?? $this->runOnce; |
178
|
|
|
|
179
|
|
|
$this->routePath = $this->getOptionFromInput('route-path') ?? $this->getConfig('routePath', $this->routePath); |
|
|
|
|
180
|
|
|
$this->routePath = is_string($this->routePath) ? realpath($this->routePath) : null; |
181
|
|
|
|
182
|
|
|
if (!$this->noDebug) { |
183
|
|
|
Logger::set(new ConsoleLogger()); |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Parse input options |
189
|
|
|
* |
190
|
|
|
* @return mixed |
191
|
|
|
* |
192
|
|
|
* @author Donii Sergii <[email protected]> |
193
|
|
|
*/ |
194
|
|
|
abstract protected function parseOptions(); |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Get transport provider |
198
|
|
|
* |
199
|
|
|
* @return mixed |
200
|
|
|
* |
201
|
|
|
* @author Donii Sergii <[email protected]> |
202
|
|
|
*/ |
203
|
|
|
abstract protected function getTransportProvider(); |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Add pid process |
207
|
|
|
* |
208
|
|
|
* @param int $pid Process pid |
209
|
|
|
* @param string $fileName Filename |
210
|
|
|
* |
211
|
|
|
* @author Donii Sergii <[email protected]> |
212
|
|
|
*/ |
213
|
|
|
protected function addPidToLog($pid, $fileName = 'server.pids') |
214
|
|
|
{ |
215
|
|
|
$fileName = storage_path($fileName); |
|
|
|
|
216
|
|
|
if (!file_exists(storage_path($fileName))) { |
217
|
|
|
file_put_contents($fileName, $pid); |
218
|
|
|
|
219
|
|
|
return; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
$content = file_get_contents($fileName); |
223
|
|
|
|
224
|
|
|
file_put_contents($fileName, $content.PHP_EOL.$pid); |
225
|
|
|
} |
226
|
|
|
} |
227
|
|
|
|