| Total Complexity | 43 |
| Total Lines | 258 |
| Duplicated Lines | 0 % |
| Changes | 10 | ||
| Bugs | 2 | Features | 0 |
Complex classes like LaravelS often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use LaravelS, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 36 | class LaravelS extends Server |
||
| 37 | { |
||
| 38 | /** |
||
| 39 | * Fix conflicts of traits |
||
| 40 | */ |
||
| 41 | use InotifyTrait, LaravelTrait, LogTrait, ProcessTitleTrait, TimerTrait, CustomProcessTrait; |
||
| 42 | |||
| 43 | /**@var array */ |
||
| 44 | protected $laravelConf; |
||
| 45 | |||
| 46 | /**@var Laravel */ |
||
| 47 | protected $laravel; |
||
| 48 | |||
| 49 | public function __construct(array $svrConf, array $laravelConf) |
||
| 72 | } |
||
| 73 | } |
||
| 74 | |||
| 75 | protected function beforeWebSocketHandShake(SwooleRequest $request) |
||
| 76 | { |
||
| 77 | // Start Laravel's lifetime, then support session ...middleware. |
||
| 78 | $laravelRequest = $this->convertRequest($this->laravel, $request); |
||
| 79 | $this->laravel->bindRequest($laravelRequest); |
||
| 80 | $this->laravel->fireEvent('laravels.received_request', [$laravelRequest]); |
||
| 81 | $this->laravel->cleanProviders(); |
||
| 82 | $laravelResponse = $this->laravel->handleDynamic($laravelRequest); |
||
| 83 | $this->laravel->fireEvent('laravels.generated_response', [$laravelRequest, $laravelResponse]); |
||
| 84 | } |
||
| 85 | |||
| 86 | protected function afterWebSocketOpen(SwooleRequest $request) |
||
| 87 | { |
||
| 88 | // End Laravel's lifetime. |
||
| 89 | $this->laravel->saveSession(); |
||
| 90 | $this->laravel->clean(); |
||
| 91 | } |
||
| 92 | |||
| 93 | protected function triggerWebSocketEvent($event, array $params) |
||
| 94 | { |
||
| 95 | if ($event === 'onHandShake') { |
||
| 96 | $this->beforeWebSocketHandShake($params[0]); |
||
| 97 | if (!empty($this->conf['server'])) { |
||
| 98 | $params[1]->header('Server', $this->conf['server']); |
||
| 99 | } |
||
| 100 | } |
||
| 101 | |||
| 102 | parent::triggerWebSocketEvent($event, $params); |
||
| 103 | |||
| 104 | switch ($event) { |
||
| 105 | case 'onHandShake': |
||
| 106 | if (isset($params[1]->header['Sec-Websocket-Accept'])) { |
||
| 107 | // Successful handshake |
||
| 108 | parent::triggerWebSocketEvent('onOpen', [$this->swoole, $params[0]]); |
||
| 109 | } |
||
| 110 | $this->afterWebSocketOpen($params[0]); |
||
| 111 | break; |
||
| 112 | case 'onOpen': |
||
| 113 | $this->afterWebSocketOpen($params[1]); |
||
| 114 | break; |
||
| 115 | } |
||
| 116 | } |
||
| 117 | |||
| 118 | protected function triggerPortEvent(Port $port, $handlerClass, $event, array $params) |
||
| 119 | { |
||
| 120 | switch ($event) { |
||
| 121 | case 'onHandShake': |
||
| 122 | $this->beforeWebSocketHandShake($params[0]); |
||
| 123 | case 'onRequest': |
||
| 124 | if (!empty($this->conf['server'])) { |
||
| 125 | $params[1]->header('Server', $this->conf['server']); |
||
| 126 | } |
||
| 127 | break; |
||
| 128 | } |
||
| 129 | |||
| 130 | parent::triggerPortEvent($port, $handlerClass, $event, $params); |
||
| 131 | |||
| 132 | switch ($event) { |
||
| 133 | case 'onHandShake': |
||
| 134 | if (isset($params[1]->header['Sec-Websocket-Accept'])) { |
||
| 135 | // Successful handshake |
||
| 136 | parent::triggerPortEvent($port, $handlerClass, 'onOpen', [$this->swoole, $params[0]]); |
||
| 137 | } |
||
| 138 | $this->afterWebSocketOpen($params[0]); |
||
| 139 | break; |
||
| 140 | case 'onOpen': |
||
| 141 | $this->afterWebSocketOpen($params[1]); |
||
| 142 | break; |
||
| 143 | } |
||
| 144 | } |
||
| 145 | |||
| 146 | public function onShutdown(HttpServer $server) |
||
| 147 | { |
||
| 148 | parent::onShutdown($server); |
||
| 149 | |||
| 150 | // Fire ServerStop event |
||
| 151 | if (isset($this->conf['event_handlers']['ServerStop'])) { |
||
| 152 | $this->laravel = $this->initLaravel($this->laravelConf, $this->swoole); |
||
| 153 | $this->fireEvent('ServerStop', ServerStopInterface::class, [$server]); |
||
| 154 | } |
||
| 155 | } |
||
| 156 | |||
| 157 | public function onWorkerStart(HttpServer $server, $workerId) |
||
| 168 | } |
||
| 169 | |||
| 170 | public function onWorkerStop(HttpServer $server, $workerId) |
||
| 171 | { |
||
| 172 | parent::onWorkerStop($server, $workerId); |
||
| 173 | |||
| 174 | // Fire WorkerStop event |
||
| 175 | $this->fireEvent('WorkerStop', WorkerStopInterface::class, func_get_args()); |
||
| 176 | } |
||
| 177 | |||
| 178 | public function onWorkerError(HttpServer $server, $workerId, $workerPId, $exitCode, $signal) |
||
| 179 | { |
||
| 180 | parent::onWorkerError($server, $workerId, $workerPId, $exitCode, $signal); |
||
| 181 | |||
| 182 | Laravel::autoload($this->laravelConf['root_path']); |
||
| 183 | |||
| 184 | // Fire WorkerError event |
||
| 185 | $this->fireEvent('WorkerError', WorkerErrorInterface::class, func_get_args()); |
||
| 186 | } |
||
| 187 | |||
| 188 | protected function convertRequest(Laravel $laravel, SwooleRequest $request) |
||
| 189 | { |
||
| 190 | $rawGlobals = $laravel->getRawGlobals(); |
||
| 191 | return (new Request($request))->toIlluminateRequest($rawGlobals['_SERVER'], $rawGlobals['_ENV']); |
||
| 192 | } |
||
| 193 | |||
| 194 | public function onRequest(SwooleRequest $swooleRequest, SwooleResponse $swooleResponse) |
||
| 195 | { |
||
| 196 | try { |
||
| 197 | $laravelRequest = $this->convertRequest($this->laravel, $swooleRequest); |
||
| 198 | $this->laravel->bindRequest($laravelRequest); |
||
| 199 | $this->laravel->fireEvent('laravels.received_request', [$laravelRequest]); |
||
| 200 | $handleStaticSuccess = false; |
||
| 201 | if ($this->conf['handle_static']) { |
||
| 202 | // For Swoole < 1.9.17 |
||
| 203 | $handleStaticSuccess = $this->handleStaticResource($this->laravel, $laravelRequest, $swooleResponse); |
||
| 204 | } |
||
| 205 | if (!$handleStaticSuccess) { |
||
| 206 | $this->handleDynamicResource($this->laravel, $laravelRequest, $swooleResponse); |
||
| 207 | } |
||
| 208 | } catch (\Exception $e) { |
||
| 209 | $this->handleException($e, $swooleResponse); |
||
| 210 | } |
||
| 211 | } |
||
| 212 | |||
| 213 | /** |
||
| 214 | * @param \Exception $e |
||
| 215 | * @param SwooleResponse $response |
||
| 216 | */ |
||
| 217 | protected function handleException($e, SwooleResponse $response) |
||
| 218 | { |
||
| 219 | if ($e instanceof SuspiciousOperationException) { |
||
| 220 | try { |
||
| 221 | $response->status(400); |
||
| 222 | $response->end('Bad Request'); |
||
| 223 | } catch (\Exception $e) { |
||
| 224 | $this->logException($e); |
||
| 225 | } |
||
| 226 | return; |
||
| 227 | } |
||
| 228 | |||
| 229 | $msg = sprintf( |
||
| 230 | 'onRequest: Uncaught exception "%s"([%d]%s) at %s:%s, %s%s', |
||
| 231 | get_class($e), |
||
| 232 | $e->getCode(), |
||
| 233 | $e->getMessage(), |
||
| 234 | $e->getFile(), |
||
| 235 | $e->getLine(), |
||
| 236 | PHP_EOL, |
||
| 237 | $e->getTraceAsString() |
||
| 238 | ); |
||
| 239 | $this->error($msg); |
||
| 240 | try { |
||
| 241 | $response->status(500); |
||
| 242 | $response->end('Internal Server Error'); |
||
| 243 | } catch (\Exception $e) { |
||
| 244 | $this->logException($e); |
||
| 245 | } |
||
| 246 | } |
||
| 247 | |||
| 248 | protected function handleStaticResource(Laravel $laravel, IlluminateRequest $laravelRequest, SwooleResponse $swooleResponse) |
||
| 262 | } |
||
| 263 | |||
| 264 | protected function handleDynamicResource(Laravel $laravel, IlluminateRequest $laravelRequest, SwooleResponse $swooleResponse) |
||
| 281 | } |
||
| 282 | |||
| 283 | /**@var OutputStyle */ |
||
| 284 | protected static $outputStyle; |
||
| 285 | |||
| 286 | public static function setOutputStyle(OutputStyle $outputStyle) |
||
| 287 | { |
||
| 288 | static::$outputStyle = $outputStyle; |
||
| 289 | } |
||
| 290 | |||
| 291 | public static function getOutputStyle() |
||
| 294 | } |
||
| 295 | } |
||
| 296 |