From 79e5907765152f69f382ddc664e0899b24802aa6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Fri, 10 Sep 2021 10:57:48 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- http-message/Response.php | 22 +++++++---------- http-server/ExceptionHandlerDispatcher.php | 28 ++++++++++------------ 2 files changed, 22 insertions(+), 28 deletions(-) diff --git a/http-message/Response.php b/http-message/Response.php index d97a1cf0..6ea446ac 100644 --- a/http-message/Response.php +++ b/http-message/Response.php @@ -16,10 +16,7 @@ class Response implements ResponseInterface use Message; - const CONTENT_TYPE_JSON = 'application/json;charset=utf-8'; const CONTENT_TYPE_HTML = 'text/html;charset=utf-8'; - const CONTENT_TYPE_STREAM = 'octet-stream'; - const CONTENT_TYPE_XML = 'application/xml;charset=utf-8'; protected int $statusCode = 200; @@ -145,23 +142,23 @@ class Response implements ResponseInterface /** * @param $data + * @param string $contentType * @return static - * @throws Exception */ - public function json($data): static + public function json($data, string $contentType = 'application/json;charset=utf-8'): static { $this->stream->write(json_encode($data)); - return $this->withContentType(self::CONTENT_TYPE_JSON); + return $this->withContentType($contentType); } /** * @param $data + * @param string $contentType * @return static - * @throws Exception */ - public function html($data): static + public function html($data, string $contentType = 'text/html;charset=utf-8'): static { if (!is_string($data)) { $data = json_encode($data); @@ -169,21 +166,20 @@ class Response implements ResponseInterface $this->stream->write((string)$data); - return $this->withContentType(self::CONTENT_TYPE_HTML); + return $this->withContentType($contentType); } /** * @param $data + * @param string $contentType * @return static - * @throws Exception */ - public function xml($data): static + public function xml($data, string $contentType = 'application/xml;charset=utf-8'): static { - $this->stream->write(Help::toXml($data)); - return $this->withContentType(self::CONTENT_TYPE_XML); + return $this->withContentType($contentType); } diff --git a/http-server/ExceptionHandlerDispatcher.php b/http-server/ExceptionHandlerDispatcher.php index 3a1e466d..96de17cd 100644 --- a/http-server/ExceptionHandlerDispatcher.php +++ b/http-server/ExceptionHandlerDispatcher.php @@ -3,11 +3,10 @@ namespace Server; -use Server\Constrict\Response; -use Protocol\Message\Response as CResponse; -use Throwable; use Protocol\Message\Stream; +use Server\Constrict\Response; use Server\Constrict\ResponseInterface; +use Throwable; /** @@ -22,17 +21,16 @@ class ExceptionHandlerDispatcher implements ExceptionHandlerInterface * @param Response $response * @return ResponseInterface */ - public function emit(Throwable $exception, Response $response): ResponseInterface - { - if ($exception->getCode() == 404) { - return $response->withBody(new Stream($exception->getMessage())) - ->withContentType(CResponse::CONTENT_TYPE_HTML) - ->withStatus(404); - } - $code = $exception->getCode() == 0 ? 500 : $exception->getCode(); - return $response->withBody(new Stream(jTraceEx($exception, null, true))) - ->withContentType(CResponse::CONTENT_TYPE_HTML) - ->withStatus($code); - } + public function emit(Throwable $exception, Response $response): ResponseInterface + { + $response->withContentType('text/html;charset=utf-8'); + if ($exception->getCode() == 404) { + return $response->withBody(new Stream($exception->getMessage())) + ->withStatus(404); + } + $code = $exception->getCode() == 0 ? 500 : $exception->getCode(); + return $response->withBody(new Stream(jTraceEx($exception, null, true))) + ->withStatus($code); + } }