1 | <?php |
||
9 | class Server |
||
10 | { |
||
11 | /** |
||
12 | * @var \Laravel\Lumen\Application |
||
13 | */ |
||
14 | protected $app; |
||
15 | |||
16 | /** |
||
17 | * @var string |
||
18 | */ |
||
19 | protected $host; |
||
20 | |||
21 | /** |
||
22 | * @var string |
||
23 | */ |
||
24 | protected $port; |
||
25 | |||
26 | /** |
||
27 | * @var string |
||
28 | */ |
||
29 | protected $pidFile = ''; |
||
30 | |||
31 | /** |
||
32 | * @var HttpServer |
||
33 | */ |
||
34 | protected $httpServer; |
||
35 | |||
36 | /** |
||
37 | * Http server options. |
||
38 | * |
||
39 | * @var array |
||
40 | */ |
||
41 | protected $options = []; |
||
42 | |||
43 | /** |
||
44 | * @var array |
||
45 | */ |
||
46 | public static $validServerOptions = [ |
||
47 | 'reactor_num', |
||
48 | 'worker_num', |
||
49 | 'max_request', |
||
50 | 'max_conn', |
||
51 | 'task_worker_num', |
||
52 | 'task_ipc_mode', |
||
53 | 'task_max_request', |
||
54 | 'task_tmpdir', |
||
55 | 'dispatch_mode', |
||
56 | 'message_queue_key', |
||
57 | 'daemonize', |
||
58 | 'backlog', |
||
59 | 'log_file', |
||
60 | 'log_level', |
||
61 | 'heartbeat_check_interval', |
||
62 | 'heartbeat_idle_time', |
||
63 | 'open_eof_check', |
||
64 | 'open_eof_split', |
||
65 | 'package_eof', |
||
66 | 'open_length_check', |
||
67 | 'package_length_type', |
||
68 | 'package_max_length', |
||
69 | 'open_cpu_affinity', |
||
70 | 'cpu_affinity_ignore', |
||
71 | 'open_tcp_nodelay', |
||
72 | 'tcp_defer_accept', |
||
73 | 'ssl_cert_file', |
||
74 | 'ssl_method', |
||
75 | 'user', |
||
76 | 'group', |
||
77 | 'chroot', |
||
78 | 'pipe_buffer_size', |
||
79 | 'buffer_output_size', |
||
80 | 'socket_buffer_size', |
||
81 | 'enable_unsafe_event', |
||
82 | 'discard_timeout_request', |
||
83 | 'enable_reuse_port', |
||
84 | 'ssl_ciphers', |
||
85 | 'enable_delay_receive', |
||
86 | ]; |
||
87 | |||
88 | /** |
||
89 | * Create a new Server instance. |
||
90 | * |
||
91 | * @param string $host |
||
92 | * @param string $port |
||
93 | */ |
||
94 | public function __construct($host = 'localhost', $port = '8083') |
||
99 | |||
100 | /** |
||
101 | * Set application. |
||
102 | * |
||
103 | * @param \Laravel\Lumen\Application $app |
||
104 | * |
||
105 | * @return $this |
||
106 | */ |
||
107 | public function setApplication($app) |
||
113 | |||
114 | /** |
||
115 | * Get application. |
||
116 | * |
||
117 | * @return \Laravel\Lumen\Application |
||
118 | */ |
||
119 | public function getApplication() |
||
125 | |||
126 | /** |
||
127 | * Resolve application. |
||
128 | * |
||
129 | * @return void |
||
130 | */ |
||
131 | protected function resolveApplication() |
||
137 | |||
138 | /** |
||
139 | * Get the base path for the application. |
||
140 | * |
||
141 | * @param string|null $path |
||
142 | * |
||
143 | * @return string |
||
144 | */ |
||
145 | public function basePath($path = null) |
||
149 | |||
150 | /** |
||
151 | * Initialize the server. |
||
152 | * |
||
153 | * @return $this |
||
154 | */ |
||
155 | public function initHttpServer() |
||
169 | |||
170 | /** |
||
171 | * Start the server. |
||
172 | * |
||
173 | * @return void |
||
174 | */ |
||
175 | public function run() |
||
193 | |||
194 | /** |
||
195 | * Determine if server is running. |
||
196 | * |
||
197 | * @return bool |
||
198 | */ |
||
199 | public function serverIsRunning() |
||
209 | |||
210 | /** |
||
211 | * Set http server options. |
||
212 | * |
||
213 | * @param array $options |
||
214 | * |
||
215 | * @return $this |
||
216 | */ |
||
217 | public function options($options = []) |
||
223 | |||
224 | /** |
||
225 | * On request callback. |
||
226 | * |
||
227 | * @param \swoole_http_request $request |
||
228 | * @param \swoole_http_response $response |
||
229 | */ |
||
230 | public function onRequest($request, $response) |
||
260 | |||
261 | /** |
||
262 | * Server start event callback. |
||
263 | * |
||
264 | * @param $server |
||
265 | */ |
||
266 | public function onStart($server) |
||
270 | |||
271 | /** |
||
272 | * Server shutdown event callback. |
||
273 | * |
||
274 | * @param $server |
||
275 | */ |
||
276 | public function onShutdown($server) |
||
280 | |||
281 | /** |
||
282 | * Response handler. |
||
283 | * |
||
284 | * @param \swoole_http_response $swooleResponse |
||
285 | * @param Response $response |
||
286 | * |
||
287 | * @return void |
||
288 | */ |
||
289 | protected function handleResponse($swooleResponse, Response $response) |
||
315 | } |
||
316 |
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: