From cbcf457e7bf87a78a1ad0057026a931843f552e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=91=E6=9E=97?= Date: Sun, 16 Apr 2023 15:57:50 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=98=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Constrict/ConstrictResponse.php | 72 ++++++++++++++++++++++++++++- src/Constrict/Stream.php | 4 +- src/HttpResponseEmitter.php | 2 +- src/Response.php | 56 ++++++++++++++++++++++ 4 files changed, 129 insertions(+), 5 deletions(-) diff --git a/src/Constrict/ConstrictResponse.php b/src/Constrict/ConstrictResponse.php index 89779ff..236f357 100644 --- a/src/Constrict/ConstrictResponse.php +++ b/src/Constrict/ConstrictResponse.php @@ -3,7 +3,10 @@ declare(strict_types=1); namespace Kiri\Router\Constrict; +use Kiri\Core\Help; +use Kiri\Core\Xml; use Kiri\Router\ContentType; +use Kiri\Router\StreamResponse; use Psr\Http\Message\ResponseInterface; class ConstrictResponse extends Message implements ResponseInterface @@ -26,6 +29,72 @@ class ConstrictResponse extends Message implements ResponseInterface return $this; } + + /** + * @param mixed $data + * @param int $statusCode + * @param ContentType $type + * @return $this + */ + public function write(mixed $data, int $statusCode = 200, ContentType $type = ContentType::JSON): static + { + if ($data instanceof \Stringable) { + $this->getBody()->write($data->__toString()); + } else { + $this->getBody()->write((string)$data); + } + return $this->withContentType($type)->withStatus($statusCode); + } + + + /** + * @param mixed $data + * @param int $statusCode + * @return $this + */ + public function xml(array $data, int $statusCode = 200): static + { + $this->getBody()->write(Help::toXml($data)); + return $this->withContentType(ContentType::XML)->withStatus($statusCode); + } + + + /** + * @param array $content + * @param int $statusCode + * @return $this + */ + public function json(array $content, int $statusCode = 200): static + { + $this->getBody()->write(json_encode($content)); + return $this->withContentType(ContentType::JSON)->withStatus($statusCode); + } + + + /** + * @param string $content + * @param int $statusCode + * @return $this + */ + public function html(string $content, int $statusCode = 200): static + { + $this->getBody()->write($content); + return $this->withContentType(ContentType::HTML)->withStatus($statusCode); + } + + + /** + * @param string $content + * @param int $statusCode + * @return $this + */ + public function sendfile(string $content, int $statusCode = 200): ResponseInterface + { + $stream = new StreamResponse(); + $stream->withBody(new Stream(fopen($content, 'r+'))); + return $stream->withStatus($statusCode); + } + /** * Gets the response status code. * @@ -88,12 +157,11 @@ class ConstrictResponse extends Message implements ResponseInterface } - /** * @param object $response * @return void */ - public function write(object $response): void + public function end(object $response): void { $response->end($this->getBody()->getContents()); } diff --git a/src/Constrict/Stream.php b/src/Constrict/Stream.php index ec6d050..948898f 100644 --- a/src/Constrict/Stream.php +++ b/src/Constrict/Stream.php @@ -15,9 +15,9 @@ class Stream implements StreamInterface /** - * @param string $content + * @param mixed $content */ - public function __construct(string $content = '') + public function __construct(mixed $content = '') { $this->content = $content; } diff --git a/src/HttpResponseEmitter.php b/src/HttpResponseEmitter.php index 1385dea..de61b7c 100644 --- a/src/HttpResponseEmitter.php +++ b/src/HttpResponseEmitter.php @@ -23,7 +23,7 @@ class HttpResponseEmitter implements ResponseEmitter // TODO: Implement sender() method. $this->writeParams($proxy, $response); - $proxy->write($response); + $proxy->end($response); } diff --git a/src/Response.php b/src/Response.php index be79e6c..419399b 100644 --- a/src/Response.php +++ b/src/Response.php @@ -28,6 +28,62 @@ class Response implements ResponseInterface } + /** + * @param array $content + * @param int $statusCode + * @return ResponseInterface + */ + public function json(array $content, int $statusCode = 200): ResponseInterface + { + return $this->__call__(__FUNCTION__, $content, $statusCode); + } + + + /** + * @param array $content + * @param int $statusCode + * @return ResponseInterface + */ + public function xml(array $content, int $statusCode = 200): ResponseInterface + { + return $this->__call__(__FUNCTION__, $content, $statusCode); + } + + + /** + * @param string $content + * @param int $statusCode + * @return ResponseInterface + */ + public function html(string $content, int $statusCode = 200): ResponseInterface + { + return $this->__call__(__FUNCTION__, $content, $statusCode); + } + + + /** + * @param string $content + * @param int $statusCode + * @return ResponseInterface + */ + public function sendfile(string $content, int $statusCode = 200): ResponseInterface + { + return $this->__call__(__FUNCTION__, $content, $statusCode); + } + + + /** + * @param mixed $data + * @param int $statusCode + * @param ContentType $type + * @return Response + */ + public function write(mixed $data, int $statusCode = 200, ContentType $type = ContentType::JSON): static + { + return $this->__call__(__FUNCTION__, $data, $statusCode, $type); + } + + /** * @param string $method * @param mixed ...$params