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