This commit is contained in:
xl
2024-08-29 17:01:08 +08:00
parent 337c52c744
commit c435af1156
15 changed files with 61 additions and 167 deletions
+6 -14
View File
@@ -2,22 +2,13 @@
namespace Kiri\Router\Format;
use Kiri\Di\Inject\Container;
use Kiri;
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
@@ -25,19 +16,20 @@ class NoBody implements IFormat
public function call($result): ResponseInterface
{
// TODO: Implement call() method.
$response = Kiri::getDi()->get(ResponseInterface::class);
if (request()->getMethod() === 'HEAD') {
return $this->response->withBody(new Stream());
return $response->withBody(new Stream());
}
if ($result instanceof ResponseInterface) {
return $result;
}
if (is_object($result)) {
return $this->response->withBody(new Stream('[object]'));
return $response->withBody(new Stream('[object]'));
}
if (is_array($result)) {
return $this->response->withBody(new Stream(json_encode($result, JSON_UNESCAPED_UNICODE)));
return $response->withBody(new Stream(json_encode($result, JSON_UNESCAPED_UNICODE)));
} else {
return $this->response->withBody(new Stream((string)$result));
return $response->withBody(new Stream((string)$result));
}
}
}