diff --git a/HttpServer/Http/Request.php b/HttpServer/Http/Request.php index 3d2aa28a..716a4a45 100644 --- a/HttpServer/Http/Request.php +++ b/HttpServer/Http/Request.php @@ -426,6 +426,8 @@ class Request extends HttpService Context::setContext(\Swoole\Http\Request::class, $request); + Context::setContext(Response::class, new Response()); + return Snowflake::getDi()->get(Request::class); } diff --git a/HttpServer/Http/Response.php b/HttpServer/Http/Response.php index 29ff5c27..aabdcd84 100644 --- a/HttpServer/Http/Response.php +++ b/HttpServer/Http/Response.php @@ -55,26 +55,10 @@ class Response extends HttpService */ public function setFormat($format): static { - /** @var SResponse $response */ - $response = Context::getContext(SResponse::class); - if ($format == self::HTML) { - $response->header('Content-Type', 'text/html;charset=utf-8'); - } else if ($format == self::XML) { - $response->header('Content-Type', 'application/xml;charset=utf-8'); - } else { - $response->header('Content-Type', 'application/json;charset=utf-8'); - } - return $this; + $this->format = $format; + return $this; } - /** - * 清理无用数据 - */ - public function clear(): void - { - $this->fd = 0; - $this->format = null; - } /** * @param $content @@ -83,11 +67,6 @@ class Response extends HttpService public function toHtml($content): string { $this->format = self::HTML; - - /** @var SResponse $response */ - $response = Context::getContext(SResponse::class); - $response->header('Content-Type', 'text/html;charset=utf-8'); - return (string)$content; } @@ -99,11 +78,6 @@ class Response extends HttpService public function toJson($content): string|bool { $this->format = self::JSON; - - /** @var SResponse $response */ - $response = Context::getContext(SResponse::class); - $response->header('Content-Type', 'application/json;charset=utf-8'); - return json_encode($content, JSON_UNESCAPED_UNICODE); } @@ -115,24 +89,10 @@ class Response extends HttpService public function toXml($content): mixed { $this->format = self::XML; - - /** @var SResponse $response */ - $response = Context::getContext(SResponse::class); - $response->header('Content-Type', 'application/xml;charset=utf-8'); - return $content; } - /** - * @return mixed - * @throws Exception - */ - public function sender(): mixed - { - return $this->send(func_get_args()); - } - /** * @param $key * @param $value @@ -140,9 +100,7 @@ class Response extends HttpService */ public function addHeader($key, $value): static { - /** @var SResponse $response */ - $response = Context::getContext(SResponse::class); - $response->header($key, $value); + $this->headers[$key] = $value; return $this; } @@ -160,28 +118,31 @@ class Response extends HttpService */ public function addCookie($name, $value = null, $expires = null, $path = null, $domain = null, $secure = null, $httponly = null, $samesite = null, $priority = null): static { - /** @var SResponse $response */ - $response = Context::getContext(SResponse::class); - $response->cookie(...func_get_args()); + $this->cookies[] = func_get_args(); return $this; } + + /** + * @param $statusCode + */ + public function setStatusCode($statusCode) + { + $this->statusCode = $statusCode; + } + + + /** * @param mixed $context * @param int $statusCode * @return bool * @throws Exception */ - public function send(mixed $context = '', int $statusCode = 200): mixed + public function send(mixed $context, int $statusCode, SResponse $response): mixed { $sendData = $this->parseData($context); - /** @var SResponse|null $response */ - $response = Context::getContext(SResponse::class); - if ($response === null) { - return $this->printResult($sendData); - } - $response->setStatusCode($statusCode); if (!isset($response->header['Content-Type'])) { $response->header('Content-Type', 'application/json;charset=utf-8'); diff --git a/Server/HTTPServerListener.php b/Server/HTTPServerListener.php index ec016322..f8dbb90b 100644 --- a/Server/HTTPServerListener.php +++ b/Server/HTTPServerListener.php @@ -6,6 +6,7 @@ use Annotation\Inject; use Exception; use HttpServer\Http\Context; use HttpServer\Http\Request as HSRequest; +use HttpServer\Route\Node; use HttpServer\Route\Router; use Server\Events\OnAfterRequest; use Snowflake\Event; @@ -39,6 +40,7 @@ class HTTPServerListener extends Abstracts\Server public \HttpServer\Http\Response $response; + /** @var EventDispatch */ #[Inject(EventDispatch::class)] public EventDispatch $eventDispatch; @@ -90,15 +92,20 @@ class HTTPServerListener extends Abstracts\Server public function onRequest(Request $request, Response $response) { try { - Context::setContext(Response::class, $response); - - HSRequest::create($request); -// $this->router->dispatch(HSRequest::create($request)); - } catch (Error | Throwable $exception) { -// $this->response->send(jTraceEx($exception), 500); + $node = $this->router->find_path(HSRequest::create($request)); + if (!($node instanceof Node)) { + $this->response->setFormat(\HttpServer\Http\Response::HTML); + $data = '

HTTP 404 Not Found


Powered by Swoole'; + } else { + $data= $node->dispatch(); + } + } catch (Error | Throwable $exception) { + $data = jTraceEx($exception); } finally { - $response->status(200); - $response->end('ok'); + if (Context::hasContext(Response::class)) { + return; + } + Context::getContext(Response::class)->send($data,200, $response); $this->eventDispatch->dispatch(new OnAfterRequest()); } } diff --git a/function.php b/function.php index 32eabfce..0206f3fe 100644 --- a/function.php +++ b/function.php @@ -4,6 +4,7 @@ defined('APP_PATH') or define('APP_PATH', realpath(__DIR__ . '/../../')); use Annotation\Annotation; +use HttpServer\Http\Context; use HttpServer\Http\HttpParams; use HttpServer\Http\Request; use HttpServer\Http\Response; @@ -594,24 +595,10 @@ if (!function_exists('response')) { */ function response(): Response|stdClass { - return Snowflake::getDi()->get(Response::class); - } - -} - -if (!function_exists('send')) { - - /** - * @param $context - * @param int $statusCode - * @return mixed - * @throws Exception - */ - function send($context, int $statusCode = 404): mixed - { - if (is_array($context)) $context = Json::encode($context); - - return \response()->send($context, $statusCode); + if (!Context::hasContext(Response::class)){ + return Context::setContext(Response::class, new Response()); + } + return Context::getContext(Response::class); } }