1 | <?php |
||
13 | class Server |
||
14 | { |
||
15 | const VERSION = 'lumen-swoole 0.1.0'; |
||
16 | |||
17 | /** |
||
18 | * @var \Laravel\Lumen\Application |
||
19 | */ |
||
20 | protected $app; |
||
21 | |||
22 | /** |
||
23 | * @var string |
||
24 | */ |
||
25 | protected $host = '127.0.0.1'; |
||
26 | |||
27 | /** |
||
28 | * @var int |
||
29 | */ |
||
30 | protected $port = 8083; |
||
31 | |||
32 | /** |
||
33 | * @var string |
||
34 | */ |
||
35 | protected $pidFile = ''; |
||
36 | |||
37 | /** |
||
38 | * @var HttpServer |
||
39 | */ |
||
40 | protected $httpServer; |
||
41 | |||
42 | /** |
||
43 | * Http server options. |
||
44 | * |
||
45 | * @var array |
||
46 | */ |
||
47 | protected $options = []; |
||
48 | |||
49 | protected $appSnapshot = null; |
||
50 | |||
51 | /** |
||
52 | * @var array |
||
53 | */ |
||
54 | public static $validServerOptions = [ |
||
55 | 'reactor_num', |
||
56 | 'worker_num', |
||
57 | 'max_request', |
||
58 | 'max_conn', |
||
59 | 'task_worker_num', |
||
60 | 'task_ipc_mode', |
||
61 | 'task_max_request', |
||
62 | 'task_tmpdir', |
||
63 | 'dispatch_mode', |
||
64 | 'message_queue_key', |
||
65 | 'daemonize', |
||
66 | 'backlog', |
||
67 | 'log_file', |
||
68 | 'log_level', |
||
69 | 'heartbeat_check_interval', |
||
70 | 'heartbeat_idle_time', |
||
71 | 'open_eof_check', |
||
72 | 'open_eof_split', |
||
73 | 'package_eof', |
||
74 | 'open_length_check', |
||
75 | 'package_length_type', |
||
76 | 'package_max_length', |
||
77 | 'open_cpu_affinity', |
||
78 | 'cpu_affinity_ignore', |
||
79 | 'open_tcp_nodelay', |
||
80 | 'tcp_defer_accept', |
||
81 | 'ssl_cert_file', |
||
82 | 'ssl_method', |
||
83 | 'user', |
||
84 | 'group', |
||
85 | 'chroot', |
||
86 | 'pipe_buffer_size', |
||
87 | 'buffer_output_size', |
||
88 | 'socket_buffer_size', |
||
89 | 'enable_unsafe_event', |
||
90 | 'discard_timeout_request', |
||
91 | 'enable_reuse_port', |
||
92 | 'ssl_ciphers', |
||
93 | 'enable_delay_receive', |
||
94 | ]; |
||
95 | |||
96 | /** |
||
97 | * Create a new Server instance. |
||
98 | * |
||
99 | * @param string $host |
||
100 | * @param int $port |
||
101 | */ |
||
102 | public function __construct($host = '127.0.0.1', $port = 8083) |
||
107 | |||
108 | /** |
||
109 | * Initialize the server. |
||
110 | * |
||
111 | * @return $this |
||
112 | */ |
||
113 | public function initHttpServer() |
||
127 | |||
128 | /** |
||
129 | * Set application. |
||
130 | * |
||
131 | * @param \Laravel\Lumen\Application $app |
||
132 | * |
||
133 | * @return $this |
||
134 | */ |
||
135 | public function setApplication($app) |
||
141 | |||
142 | /** |
||
143 | * Resolve application. |
||
144 | * |
||
145 | * @return void |
||
146 | */ |
||
147 | protected function resolveApplication() |
||
155 | |||
156 | protected function snapshotApplication() |
||
162 | |||
163 | /** |
||
164 | * Get the base path for the application. |
||
165 | * |
||
166 | * @param string|null $path |
||
167 | * |
||
168 | * @return string |
||
169 | */ |
||
170 | public function basePath($path = null) |
||
174 | |||
175 | /** |
||
176 | * Start the server. |
||
177 | * |
||
178 | * @return void |
||
179 | */ |
||
180 | public function run() |
||
198 | |||
199 | /** |
||
200 | * Determine if server is running. |
||
201 | * |
||
202 | * @return bool |
||
203 | */ |
||
204 | public function isRunning() |
||
214 | |||
215 | /** |
||
216 | * Set http server options. |
||
217 | * |
||
218 | * @param array $options |
||
219 | * |
||
220 | * @return $this |
||
221 | */ |
||
222 | public function options($options = []) |
||
228 | |||
229 | /** |
||
230 | * On request callback. |
||
231 | * |
||
232 | * @param \swoole_http_request $request |
||
233 | * @param \swoole_http_response $response |
||
234 | */ |
||
235 | public function onRequest($request, $response) |
||
258 | |||
259 | /** |
||
260 | * Build global variables. |
||
261 | * |
||
262 | * @param \swoole_http_request $request |
||
263 | * @return void |
||
264 | */ |
||
265 | protected function buildGlobals($request) |
||
293 | |||
294 | /** |
||
295 | * Append ob contents to response. |
||
296 | * |
||
297 | * @param Response $lumenResponse |
||
298 | * @param string $obContents |
||
299 | * @return $this |
||
300 | */ |
||
301 | protected function appendObContents(Response $lumenResponse, $obContents) |
||
305 | |||
306 | /** |
||
307 | * Handle uncaught exception. |
||
308 | * |
||
309 | * @param Request $request |
||
310 | * @param \Exception $e |
||
311 | * @return Response |
||
312 | */ |
||
313 | public function handleLumenException($request, $e) |
||
317 | |||
318 | /** |
||
319 | * Handle lumen shutdown. |
||
320 | * |
||
321 | * @param Request $request |
||
322 | * @param \swoole_http_response $response |
||
323 | * @return void |
||
324 | */ |
||
325 | public function handleLumenShutdown($request, $response) |
||
337 | |||
338 | /** |
||
339 | * Response handler. |
||
340 | * |
||
341 | * @param \swoole_http_response $swooleResponse |
||
342 | * @param Response $response |
||
343 | * |
||
344 | * @return void |
||
345 | */ |
||
346 | protected function handleResponse($swooleResponse, Response $response) |
||
374 | |||
375 | /** |
||
376 | * Server start event callback. |
||
377 | * |
||
378 | * @param $server |
||
379 | */ |
||
380 | public function onStart($server) |
||
384 | |||
385 | /** |
||
386 | * Server shutdown event callback. |
||
387 | */ |
||
388 | public function onShutdown() |
||
392 | } |
||
393 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: