From dc561cec9b3569fbbb33f599000338562e407070 Mon Sep 17 00:00:00 2001 From: whwyy Date: Mon, 16 Dec 2024 16:29:35 +0800 Subject: [PATCH] eee --- src/Format/ArrayFormat.php | 26 ++++++++++++------ src/Format/MixedFormat.php | 18 +++++++++---- src/Format/NoBody.php | 55 ++++++++++++++++++++++---------------- src/Format/OtherFormat.php | 9 +++++-- src/Format/VoidFormat.php | 27 ++++++++++++------- src/Handler.php | 2 +- src/HttpRequestHandler.php | 1 - 7 files changed, 89 insertions(+), 49 deletions(-) diff --git a/src/Format/ArrayFormat.php b/src/Format/ArrayFormat.php index f77b75c..701191b 100644 --- a/src/Format/ArrayFormat.php +++ b/src/Format/ArrayFormat.php @@ -9,14 +9,24 @@ use Psr\Http\Message\ResponseInterface; class ArrayFormat implements IFormat { - /** - * @param $result - * @return ResponseInterface - */ - public function call($result): ResponseInterface - { - return di(ResponseInterface::class)->withBody(new Stream(json_encode($result, JSON_UNESCAPED_UNICODE))); - } + + /** + * @param ResponseInterface $response + */ + public function __construct(public ResponseInterface $response) + { + } + + + /** + * @param $result + * + * @return ResponseInterface + */ + public function call($result): ResponseInterface + { + return $this->response->withBody(new Stream(json_encode($result, JSON_UNESCAPED_UNICODE))); + } } \ No newline at end of file diff --git a/src/Format/MixedFormat.php b/src/Format/MixedFormat.php index ab69d92..72e364b 100644 --- a/src/Format/MixedFormat.php +++ b/src/Format/MixedFormat.php @@ -9,7 +9,16 @@ use Psr\Http\Message\ResponseInterface; class MixedFormat implements IFormat { - /** + + /** + * @param ResponseInterface $response + */ + public function __construct(public ResponseInterface $response) + { + } + + + /** * @param mixed $result * @return ResponseInterface */ @@ -18,14 +27,13 @@ class MixedFormat implements IFormat if ($result instanceof ResponseInterface) { return $result; } - $response = Kiri::getDi()->get(ResponseInterface::class); if (is_object($result)) { - return $response->withBody(new Stream('[object]')); + return $this->response->withBody(new Stream('[object]')); } if (is_array($result)) { - return $response->withBody(new Stream(json_encode($result, JSON_UNESCAPED_UNICODE))); + return $this->response->withBody(new Stream(json_encode($result, JSON_UNESCAPED_UNICODE))); } else { - return $response->withBody(new Stream((string)$result)); + return $this->response->withBody(new Stream((string)$result)); } } diff --git a/src/Format/NoBody.php b/src/Format/NoBody.php index 5132d79..2b78f30 100644 --- a/src/Format/NoBody.php +++ b/src/Format/NoBody.php @@ -9,27 +9,36 @@ use Psr\Http\Message\ResponseInterface; class NoBody implements IFormat { - /** - * @param $result - * @return ResponseInterface - */ - public function call($result): ResponseInterface - { - // TODO: Implement call() method. - $response = Kiri::getDi()->get(ResponseInterface::class); - if (request()->getMethod() === 'HEAD') { - return $response->withBody(new Stream()); - } - if ($result instanceof ResponseInterface) { - return $result; - } - if (is_object($result)) { - return $response->withBody(new Stream('[object]')); - } - if (is_array($result)) { - return $response->withBody(new Stream(json_encode($result, JSON_UNESCAPED_UNICODE))); - } else { - return $response->withBody(new Stream((string)$result)); - } - } + + /** + * @param ResponseInterface $response + */ + public function __construct(public ResponseInterface $response) + { + } + + + /** + * @param $result + * + * @return ResponseInterface + */ + public function call($result): ResponseInterface + { + // TODO: Implement call() method. + if (request()->getMethod() === 'HEAD') { + return $this->response->withBody(new Stream()); + } + if ($result instanceof ResponseInterface) { + return $result; + } + if (is_object($result)) { + return $this->response->withBody(new Stream('[object]')); + } + if (is_array($result)) { + return $this->response->withBody(new Stream(json_encode($result, JSON_UNESCAPED_UNICODE))); + } else { + return $this->response->withBody(new Stream((string)$result)); + } + } } \ No newline at end of file diff --git a/src/Format/OtherFormat.php b/src/Format/OtherFormat.php index 7ea028b..997d4eb 100644 --- a/src/Format/OtherFormat.php +++ b/src/Format/OtherFormat.php @@ -9,13 +9,18 @@ use Psr\Http\Message\ResponseInterface; class OtherFormat implements IFormat { - /** + + public function __construct(public ResponseInterface $response) + { + } + + /** * @param mixed $result * @return ResponseInterface */ public function call(mixed $result): ResponseInterface { - return di(ResponseInterface::class)->withBody(new Stream($result)); + return $this->response->withBody(new Stream($result)); } diff --git a/src/Format/VoidFormat.php b/src/Format/VoidFormat.php index 8fbca32..db1bb73 100644 --- a/src/Format/VoidFormat.php +++ b/src/Format/VoidFormat.php @@ -8,14 +8,23 @@ use Psr\Http\Message\ResponseInterface; class VoidFormat implements IFormat { - /** - * @param $result - * @return ResponseInterface - */ - public function call($result): ResponseInterface - { - // TODO: Implement call() method. - return di(ResponseInterface::class); - } + + /** + * @param ResponseInterface $response + */ + public function __construct(public ResponseInterface $response) + { + } + + /** + * @param $result + * + * @return ResponseInterface + */ + public function call($result): ResponseInterface + { + // TODO: Implement call() method. + return $this->response; + } } \ No newline at end of file diff --git a/src/Handler.php b/src/Handler.php index b026afc..cb42409 100644 --- a/src/Handler.php +++ b/src/Handler.php @@ -49,7 +49,7 @@ class Handler implements RequestHandlerInterface */ public function setRequestMethod(string $method): void { - if ($method == 'HEAD') { + if ($method == 'HEAD' || $method == 'OPTIONS') { $this->format = Kiri::getDi()->get(NoBody::class); } } diff --git a/src/HttpRequestHandler.php b/src/HttpRequestHandler.php index dc1fdfe..bc434e9 100644 --- a/src/HttpRequestHandler.php +++ b/src/HttpRequestHandler.php @@ -7,7 +7,6 @@ use Kiri\Router\Base\AbstractHandler; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\RequestHandlerInterface; -use ReflectionException; class HttpRequestHandler extends AbstractHandler implements RequestHandlerInterface {