This commit is contained in:
xl
2023-11-22 10:35:50 +08:00
parent 576e1c3ad8
commit 95b0822822
3 changed files with 41 additions and 5 deletions
+22 -5
View File
@@ -2,13 +2,22 @@
namespace Kiri\Router\Format;
use Kiri\Di\Inject\Container;
use Kiri\Router\Constrict\Stream;
use Kiri\Router\ContentType;
use Psr\Http\Message\ResponseInterface;
class NoBody implements IFormat
{
/**
* @var ResponseInterface
*/
#[Container(ResponseInterface::class)]
public ResponseInterface $response;
/**
* @param $result
* @return ResponseInterface
@@ -16,11 +25,19 @@ class NoBody implements IFormat
public function call($result): ResponseInterface
{
// TODO: Implement call() method.
if ($result instanceof ResponseInterface) {
$result->getBody()->write('');
} else {
$result = response()->withBody(new Stream());
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->withContentType(ContentType::JSON)->withBody(new Stream(json_encode($result, JSON_UNESCAPED_UNICODE)));
} else {
return $this->response->withBody(new Stream((string)$result));
}
return $result;
}
}