onRequest($request, $response); } /** * @param Request $request * @param Response $response * @return false|int|mixed|string|void * @throws Exception */ public function onRequest(Request $request, Response $response) { try { var_dump($request); if ($request->header['request_method'] === 'OPTIONS') { $response->status(200); return $response->end(); } /** @var HRequest $sRequest */ [$sRequest, $sResponse] = [HRequest::create($request), HResponse::create($response)]; if ($sRequest->is('favicon.ico')) { return $sResponse->send($sRequest->isNotFound(), 200); } return Snowflake::app()->getRouter()->dispatch(); } catch (Error | \Throwable $exception) { return $this->sendErrorMessage($exception); } finally { $logger = Snowflake::app()->getLogger(); $request = get_object_vars($request); $logger->write(JSON::encode($request), 'request'); } } /** * @param $response * @throws Exception */ public static function shutdown($response) { try { $error = error_get_last(); if (!isset($error['type'])) { return; } $types = [E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR]; if (!in_array($error['type'], $types)) { return; } $message = $error['message'] . ':' . microtime(true); if ($response instanceof Response) { $response->status(500); $response->end($message); } } catch (\ErrorException $exception) { $logger = Snowflake::app()->logger; $logger->write($exception->getMessage(), 'shutdown'); } finally { unset($response); } } /** * @param $exception * @return false|int|mixed|string * @throws ComponentException * @throws Exception */ protected function sendErrorMessage($exception) { $params = Snowflake::app()->getLogger()->exception($exception); return \response()->send($params, 200); } }