|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SwooleTW\Http; |
|
4
|
|
|
|
|
5
|
|
|
use SwooleTW\Http\Helpers\FW; |
|
6
|
|
|
use Illuminate\Queue\QueueManager; |
|
7
|
|
|
use SwooleTW\Http\Server\PidManager; |
|
8
|
|
|
use Swoole\Http\Server as HttpServer; |
|
9
|
|
|
use Illuminate\Support\ServiceProvider; |
|
10
|
|
|
use Illuminate\Database\DatabaseManager; |
|
11
|
|
|
use SwooleTW\Http\Server\Facades\Server; |
|
12
|
|
|
use SwooleTW\Http\Coroutine\MySqlConnection; |
|
13
|
|
|
use SwooleTW\Http\Commands\HttpServerCommand; |
|
14
|
|
|
use Swoole\Websocket\Server as WebsocketServer; |
|
15
|
|
|
use SwooleTW\Http\Task\Connectors\SwooleTaskConnector; |
|
16
|
|
|
use SwooleTW\Http\Coroutine\Connectors\ConnectorFactory; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @codeCoverageIgnore |
|
20
|
|
|
*/ |
|
21
|
|
|
abstract class HttpServiceProvider extends ServiceProvider |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* Indicates if loading of the provider is deferred. |
|
25
|
|
|
* |
|
26
|
|
|
* @var bool |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $defer = false; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var boolean |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $isWebsocket = false; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var \Swoole\Http\Server | \Swoole\Websocket\Server |
|
37
|
|
|
*/ |
|
38
|
|
|
protected static $server; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Boot the service provider. |
|
42
|
|
|
* |
|
43
|
|
|
* @return void |
|
44
|
|
|
*/ |
|
45
|
|
|
public function boot() |
|
46
|
|
|
{ |
|
47
|
|
|
$this->publishFiles(); |
|
48
|
|
|
$this->loadConfigs(); |
|
49
|
|
|
$this->mergeConfigs(); |
|
50
|
|
|
$this->setIsWebsocket(); |
|
51
|
|
|
|
|
52
|
|
|
$config = $this->app->make('config'); |
|
53
|
|
|
|
|
54
|
|
|
if ($config->get('swoole_http.websocket.enabled')) { |
|
55
|
|
|
$this->bootWebsocketRoutes(); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
if ($config->get('swoole_http.server.access_log')) { |
|
59
|
|
|
$this->pushAccessLogMiddleware(); |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Register the service provider. |
|
65
|
|
|
* |
|
66
|
|
|
* @return void |
|
67
|
|
|
*/ |
|
68
|
|
|
public function register() |
|
69
|
|
|
{ |
|
70
|
|
|
$this->registerServer(); |
|
71
|
|
|
$this->registerManager(); |
|
72
|
|
|
$this->registerCommands(); |
|
73
|
|
|
$this->registerPidManager(); |
|
74
|
|
|
$this->registerDatabaseDriver(); |
|
75
|
|
|
$this->registerSwooleQueueDriver(); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Register manager. |
|
80
|
|
|
* |
|
81
|
|
|
* @return void |
|
82
|
|
|
*/ |
|
83
|
|
|
abstract protected function registerManager(); |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Boot websocket routes. |
|
87
|
|
|
* |
|
88
|
|
|
* @return void |
|
89
|
|
|
*/ |
|
90
|
|
|
abstract protected function bootWebsocketRoutes(); |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Register access log middleware to container. |
|
94
|
|
|
* |
|
95
|
|
|
* @return void |
|
96
|
|
|
*/ |
|
97
|
|
|
abstract protected function pushAccessLogMiddleware(); |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Publish files of this package. |
|
101
|
|
|
*/ |
|
102
|
|
|
protected function publishFiles() |
|
103
|
|
|
{ |
|
104
|
|
|
$this->publishes([ |
|
105
|
|
|
__DIR__ . '/../config/swoole_http.php' => base_path('config/swoole_http.php'), |
|
106
|
|
|
__DIR__ . '/../config/swoole_websocket.php' => base_path('config/swoole_websocket.php'), |
|
107
|
|
|
__DIR__ . '/../routes/websocket.php' => base_path('routes/websocket.php'), |
|
108
|
|
|
], 'laravel-swoole'); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Load configurations. |
|
113
|
|
|
*/ |
|
114
|
|
|
protected function loadConfigs() |
|
115
|
|
|
{ |
|
116
|
|
|
// do nothing |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Merge configurations. |
|
121
|
|
|
*/ |
|
122
|
|
|
protected function mergeConfigs() |
|
123
|
|
|
{ |
|
124
|
|
|
$this->mergeConfigFrom(__DIR__ . '/../config/swoole_http.php', 'swoole_http'); |
|
125
|
|
|
$this->mergeConfigFrom(__DIR__ . '/../config/swoole_websocket.php', 'swoole_websocket'); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Register pid manager. |
|
130
|
|
|
* |
|
131
|
|
|
* @return void |
|
132
|
|
|
*/ |
|
133
|
|
|
protected function registerPidManager(): void |
|
134
|
|
|
{ |
|
135
|
|
|
$this->app->singleton(PidManager::class, function() { |
|
136
|
|
|
return new PidManager( |
|
137
|
|
|
$this->app->make('config')->get('swoole_http.server.options.pid_file') |
|
138
|
|
|
); |
|
139
|
|
|
}); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Set isWebsocket. |
|
144
|
|
|
*/ |
|
145
|
|
|
protected function setIsWebsocket() |
|
146
|
|
|
{ |
|
147
|
|
|
$this->isWebsocket = $this->app->make('config') |
|
148
|
|
|
->get('swoole_http.websocket.enabled'); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Register commands. |
|
153
|
|
|
*/ |
|
154
|
|
|
protected function registerCommands() |
|
155
|
|
|
{ |
|
156
|
|
|
$this->commands([ |
|
157
|
|
|
HttpServerCommand::class, |
|
158
|
|
|
]); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Create swoole server. |
|
163
|
|
|
*/ |
|
164
|
|
|
protected function createSwooleServer() |
|
165
|
|
|
{ |
|
166
|
|
|
$server = $this->isWebsocket ? WebsocketServer::class : HttpServer::class; |
|
167
|
|
|
$config = $this->app->make('config'); |
|
168
|
|
|
$host = $config->get('swoole_http.server.host'); |
|
169
|
|
|
$port = $config->get('swoole_http.server.port'); |
|
170
|
|
|
$socketType = $config->get('swoole_http.server.socket_type', SWOOLE_SOCK_TCP); |
|
171
|
|
|
$processType = $config->get('swoole_http.server.process_type', SWOOLE_PROCESS); |
|
172
|
|
|
|
|
173
|
|
|
static::$server = new $server($host, $port, $processType, $socketType); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* Set swoole server configurations. |
|
178
|
|
|
*/ |
|
179
|
|
|
protected function configureSwooleServer() |
|
180
|
|
|
{ |
|
181
|
|
|
$config = $this->app->make('config'); |
|
182
|
|
|
$options = $config->get('swoole_http.server.options'); |
|
183
|
|
|
|
|
184
|
|
|
// lookup for set swoole driver |
|
185
|
|
|
$isDefinedSwooleDriver = in_array( |
|
186
|
|
|
'swoole', |
|
187
|
|
|
array_column( |
|
188
|
|
|
$config->get('queue.connections'), |
|
189
|
|
|
'driver' |
|
190
|
|
|
), |
|
191
|
|
|
true |
|
192
|
|
|
) || $config->get('queue.default') === 'swoole'; |
|
193
|
|
|
|
|
194
|
|
|
// only enable task worker in websocket mode and for queue driver |
|
195
|
|
|
if (! $isDefinedSwooleDriver && ! $this->isWebsocket) { |
|
196
|
|
|
unset($options['task_worker_num']); |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
static::$server->set($options); |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* Register manager. |
|
204
|
|
|
* |
|
205
|
|
|
* @return void |
|
206
|
|
|
*/ |
|
207
|
|
|
protected function registerServer() |
|
208
|
|
|
{ |
|
209
|
|
|
$this->app->singleton(Server::class, function () { |
|
210
|
|
|
if (is_null(static::$server)) { |
|
211
|
|
|
$this->createSwooleServer(); |
|
212
|
|
|
$this->configureSwooleServer(); |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
return static::$server; |
|
216
|
|
|
}); |
|
217
|
|
|
$this->app->alias(Server::class, 'swoole.server'); |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
/** |
|
221
|
|
|
* Register database driver for coroutine mysql. |
|
222
|
|
|
*/ |
|
223
|
|
|
protected function registerDatabaseDriver() |
|
224
|
|
|
{ |
|
225
|
|
|
$this->app->extend(DatabaseManager::class, function (DatabaseManager $db) { |
|
226
|
|
|
$db->extend('mysql-coroutine', function ($config, $name) { |
|
227
|
|
|
$config['name'] = $name; |
|
228
|
|
|
|
|
229
|
|
|
$connection = new MySqlConnection( |
|
230
|
|
|
$this->getNewMySqlConnection($config, 'write'), |
|
231
|
|
|
$config['database'], |
|
232
|
|
|
$config['prefix'], |
|
233
|
|
|
$config |
|
234
|
|
|
); |
|
235
|
|
|
|
|
236
|
|
|
if (isset($config['read'])) { |
|
237
|
|
|
$connection->setReadPdo($this->getNewMySqlConnection($config, 'read')); |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
return $connection; |
|
241
|
|
|
}); |
|
242
|
|
|
|
|
243
|
|
|
return $db; |
|
244
|
|
|
}); |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
/** |
|
248
|
|
|
* Get a new mysql connection. |
|
249
|
|
|
* |
|
250
|
|
|
* @param array $config |
|
251
|
|
|
* @param string $connection |
|
252
|
|
|
* |
|
253
|
|
|
* @return \PDO |
|
254
|
|
|
*/ |
|
255
|
|
|
protected function getNewMySqlConnection(array $config, string $connection = null) |
|
256
|
|
|
{ |
|
257
|
|
|
if ($connection && isset($config[$connection])) { |
|
258
|
|
|
$config = array_merge($config, $config[$connection]); |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
return ConnectorFactory::make(FW::version())->connect($config); |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
/** |
|
265
|
|
|
* Register queue driver for swoole async task. |
|
266
|
|
|
*/ |
|
267
|
|
|
protected function registerSwooleQueueDriver() |
|
268
|
|
|
{ |
|
269
|
|
|
$this->app->afterResolving('queue', function (QueueManager $manager) { |
|
270
|
|
|
$manager->addConnector('swoole', function () { |
|
271
|
|
|
return new SwooleTaskConnector($this->app->make(Server::class)); |
|
|
|
|
|
|
272
|
|
|
}); |
|
273
|
|
|
}); |
|
274
|
|
|
} |
|
275
|
|
|
} |
|
276
|
|
|
|