| Total Complexity | 41 |
| Total Lines | 248 |
| Duplicated Lines | 0 % |
| Changes | 9 | ||
| 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 |
||
| 35 | class LaravelS extends Server |
||
| 36 | { |
||
| 37 | /** |
||
| 38 | * Fix conflicts of traits |
||
| 39 | */ |
||
| 40 | use InotifyTrait, LaravelTrait, LogTrait, ProcessTitleTrait, TimerTrait, CustomProcessTrait; |
||
| 41 | |||
| 42 | /**@var array */ |
||
| 43 | protected $laravelConf; |
||
| 44 | |||
| 45 | /**@var Laravel */ |
||
| 46 | protected $laravel; |
||
| 47 | |||
| 48 | public function __construct(array $svrConf, array $laravelConf) |
||
| 71 | } |
||
| 72 | } |
||
| 73 | |||
| 74 | protected function beforeWebSocketHandShake(SwooleRequest $request) |
||
| 83 | } |
||
| 84 | |||
| 85 | protected function afterWebSocketOpen(SwooleRequest $request) |
||
| 90 | } |
||
| 91 | |||
| 92 | protected function triggerWebSocketEvent($event, array $params) |
||
| 93 | { |
||
| 94 | if ($event === 'onHandShake') { |
||
| 95 | $this->beforeWebSocketHandShake($params[0]); |
||
| 96 | if (!empty($this->conf['server'])) { |
||
| 97 | $params[1]->header('Server', $this->conf['server']); |
||
| 98 | } |
||
| 99 | } |
||
| 100 | |||
| 101 | parent::triggerWebSocketEvent($event, $params); |
||
| 102 | |||
| 103 | switch ($event) { |
||
| 104 | case 'onHandShake': |
||
| 105 | if (isset($params[1]->header['Sec-Websocket-Accept'])) { |
||
| 106 | // Successful handshake |
||
| 107 | parent::triggerWebSocketEvent('onOpen', [$this->swoole, $params[0]]); |
||
| 108 | } |
||
| 109 | $this->afterWebSocketOpen($params[0]); |
||
| 110 | break; |
||
| 111 | case 'onOpen': |
||
| 112 | $this->afterWebSocketOpen($params[1]); |
||
| 113 | break; |
||
| 114 | } |
||
| 115 | } |
||
| 116 | |||
| 117 | protected function triggerPortEvent(Port $port, $handlerClass, $event, array $params) |
||
| 118 | { |
||
| 119 | switch ($event) { |
||
| 120 | case 'onHandShake': |
||
| 121 | $this->beforeWebSocketHandShake($params[0]); |
||
| 122 | case 'onRequest': |
||
| 123 | if (!empty($this->conf['server'])) { |
||
| 124 | $params[1]->header('Server', $this->conf['server']); |
||
| 125 | } |
||
| 126 | break; |
||
| 127 | } |
||
| 128 | |||
| 129 | parent::triggerPortEvent($port, $handlerClass, $event, $params); |
||
| 130 | |||
| 131 | switch ($event) { |
||
| 132 | case 'onHandShake': |
||
| 133 | if (isset($params[1]->header['Sec-Websocket-Accept'])) { |
||
| 134 | // Successful handshake |
||
| 135 | parent::triggerPortEvent($port, $handlerClass, 'onOpen', [$this->swoole, $params[0]]); |
||
| 136 | } |
||
| 137 | $this->afterWebSocketOpen($params[0]); |
||
| 138 | break; |
||
| 139 | case 'onOpen': |
||
| 140 | $this->afterWebSocketOpen($params[1]); |
||
| 141 | break; |
||
| 142 | } |
||
| 143 | } |
||
| 144 | |||
| 145 | public function onShutdown(HttpServer $server) |
||
| 146 | { |
||
| 147 | parent::onShutdown($server); |
||
| 148 | |||
| 149 | // Fire ServerStop event |
||
| 150 | if (isset($this->conf['event_handlers']['ServerStop'])) { |
||
| 151 | $this->laravel = $this->initLaravel($this->laravelConf, $this->swoole); |
||
| 152 | $this->fireEvent('ServerStop', ServerStopInterface::class, [$server]); |
||
| 153 | } |
||
| 154 | } |
||
| 155 | |||
| 156 | public function onWorkerStart(HttpServer $server, $workerId) |
||
| 167 | } |
||
| 168 | |||
| 169 | public function onWorkerStop(HttpServer $server, $workerId) |
||
| 170 | { |
||
| 171 | parent::onWorkerStop($server, $workerId); |
||
| 172 | |||
| 173 | // Fire WorkerStop event |
||
| 174 | $this->fireEvent('WorkerStop', WorkerStopInterface::class, func_get_args()); |
||
| 175 | } |
||
| 176 | |||
| 177 | public function onWorkerError(HttpServer $server, $workerId, $workerPId, $exitCode, $signal) |
||
| 178 | { |
||
| 179 | parent::onWorkerError($server, $workerId, $workerPId, $exitCode, $signal); |
||
| 180 | |||
| 181 | Laravel::autoload($this->laravelConf['root_path']); |
||
| 182 | |||
| 183 | // Fire WorkerError event |
||
| 184 | $this->fireEvent('WorkerError', WorkerErrorInterface::class, func_get_args()); |
||
| 185 | } |
||
| 186 | |||
| 187 | protected function convertRequest(Laravel $laravel, SwooleRequest $request) |
||
| 188 | { |
||
| 189 | $rawGlobals = $laravel->getRawGlobals(); |
||
| 190 | return (new Request($request))->toIlluminateRequest($rawGlobals['_SERVER'], $rawGlobals['_ENV']); |
||
| 191 | } |
||
| 192 | |||
| 193 | public function onRequest(SwooleRequest $swooleRequest, SwooleResponse $swooleResponse) |
||
| 194 | { |
||
| 195 | try { |
||
| 196 | $laravelRequest = $this->convertRequest($this->laravel, $swooleRequest); |
||
| 197 | $this->laravel->bindRequest($laravelRequest); |
||
| 198 | $this->laravel->fireEvent('laravels.received_request', [$laravelRequest]); |
||
| 199 | $handleStaticSuccess = false; |
||
| 200 | if ($this->conf['handle_static']) { |
||
| 201 | // For Swoole < 1.9.17 |
||
| 202 | $handleStaticSuccess = $this->handleStaticResource($this->laravel, $laravelRequest, $swooleResponse); |
||
| 203 | } |
||
| 204 | if (!$handleStaticSuccess) { |
||
| 205 | $this->handleDynamicResource($this->laravel, $laravelRequest, $swooleResponse); |
||
| 206 | } |
||
| 207 | } catch (\Exception $e) { |
||
| 208 | $this->handleException($e, $swooleResponse); |
||
| 209 | } |
||
| 210 | } |
||
| 211 | |||
| 212 | /** |
||
| 213 | * @param \Exception $e |
||
| 214 | * @param SwooleResponse $response |
||
| 215 | */ |
||
| 216 | protected function handleException($e, SwooleResponse $response) |
||
| 217 | { |
||
| 218 | $error = sprintf( |
||
| 219 | 'onRequest: Uncaught exception "%s"([%d]%s) at %s:%s, %s%s', |
||
| 220 | get_class($e), |
||
| 221 | $e->getCode(), |
||
| 222 | $e->getMessage(), |
||
| 223 | $e->getFile(), |
||
| 224 | $e->getLine(), |
||
| 225 | PHP_EOL, |
||
| 226 | $e->getTraceAsString() |
||
| 227 | ); |
||
| 228 | $this->error($error); |
||
| 229 | try { |
||
| 230 | $response->status(500); |
||
| 231 | $response->end('Oops! An unexpected error occurred'); |
||
| 232 | } catch (\Exception $e) { |
||
| 233 | $this->logException($e); |
||
| 234 | } |
||
| 235 | } |
||
| 236 | |||
| 237 | protected function handleStaticResource(Laravel $laravel, IlluminateRequest $laravelRequest, SwooleResponse $swooleResponse) |
||
| 251 | } |
||
| 252 | |||
| 253 | protected function handleDynamicResource(Laravel $laravel, IlluminateRequest $laravelRequest, SwooleResponse $swooleResponse) |
||
| 270 | } |
||
| 271 | |||
| 272 | /**@var OutputStyle */ |
||
| 273 | protected static $outputStyle; |
||
| 274 | |||
| 275 | public static function setOutputStyle(OutputStyle $outputStyle) |
||
| 276 | { |
||
| 277 | static::$outputStyle = $outputStyle; |
||
| 278 | } |
||
| 279 | |||
| 280 | public static function getOutputStyle() |
||
| 283 | } |
||
| 284 | } |
||
| 285 |