eee
This commit is contained in:
+22
-5
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user