application = $application; } /** * @param array $settings * @param array $events * @param array $config * @return mixed|void * @throws NotFindClassException * @throws ReflectionException */ public function set(array $settings, $events = [], $config = []) { parent::set($settings); ServerManager::set($this, $settings, $this->application, $events, $config); } /** * @param Request $request * @param Response $response * @throws \Exception */ public function onHandler(Request $request, Response $response) { try { [$sRequest, $sResponse] = static::setContext($request, $response); $sResponse->send(Snowflake::get()->router->dispatch(), 200); } catch (Error | \Throwable $exception) { if (!isset($sResponse)) { $response->status(200); $response->end($exception->getMessage()); } else { $sResponse->send($this->format($exception), 200); } } finally { $dividing_line = str_pad('', 100, '-'); $this->application->debug($dividing_line, 'app'); } } /** * @param $exception * @return false|int|mixed|string * @throws Exception */ public function format($exception) { $errorInfo = [ 'message' => $exception->getMessage(), 'file' => $exception->getFile(), 'line' => $exception->getLine() ]; $this->application->error(var_export($errorInfo, true)); $code = $exception->getCode() ?? 500; $trance = array_slice($exception->getTrace(), 0, 10); Snowflake::get()->logger->write(print_r($trance, true), 'exception'); return JSON::to($code, $errorInfo['message']); } /** * @param $request * @param $response * @return array * @throws Exception */ public static function setContext($request, $response): array { $request = Context::setContext('request', HRequest::create($request)); $response = Context::setContext('response', HResponse::create($response)); return [$request, $response]; } }