This commit is contained in:
2023-04-16 15:57:50 +08:00
parent 2c3eed0b7a
commit cbcf457e7b
4 changed files with 129 additions and 5 deletions
+70 -2
View File
@@ -3,7 +3,10 @@ declare(strict_types=1);
namespace Kiri\Router\Constrict; namespace Kiri\Router\Constrict;
use Kiri\Core\Help;
use Kiri\Core\Xml;
use Kiri\Router\ContentType; use Kiri\Router\ContentType;
use Kiri\Router\StreamResponse;
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ResponseInterface;
class ConstrictResponse extends Message implements ResponseInterface class ConstrictResponse extends Message implements ResponseInterface
@@ -26,6 +29,72 @@ class ConstrictResponse extends Message implements ResponseInterface
return $this; 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. * Gets the response status code.
* *
@@ -88,12 +157,11 @@ class ConstrictResponse extends Message implements ResponseInterface
} }
/** /**
* @param object $response * @param object $response
* @return void * @return void
*/ */
public function write(object $response): void public function end(object $response): void
{ {
$response->end($this->getBody()->getContents()); $response->end($this->getBody()->getContents());
} }
+2 -2
View File
@@ -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; $this->content = $content;
} }
+1 -1
View File
@@ -23,7 +23,7 @@ class HttpResponseEmitter implements ResponseEmitter
// TODO: Implement sender() method. // TODO: Implement sender() method.
$this->writeParams($proxy, $response); $this->writeParams($proxy, $response);
$proxy->write($response); $proxy->end($response);
} }
+56
View File
@@ -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 string $method
* @param mixed ...$params * @param mixed ...$params