This commit is contained in:
2023-11-03 17:44:38 +08:00
parent 115d228158
commit 1582c0c241
4 changed files with 38 additions and 7 deletions
+11 -3
View File
@@ -2,6 +2,7 @@
namespace Kiri\Router\Format;
use Kiri\Di\Inject\Container;
use Kiri\Router\Constrict\Stream;
use Kiri\Router\ContentType;
use Psr\Http\Message\ResponseInterface;
@@ -9,6 +10,13 @@ use Psr\Http\Message\ResponseInterface;
class MixedFormat implements IFormat
{
/**
* @var ResponseInterface
*/
#[Container(ResponseInterface::class)]
public ResponseInterface $response;
/**
* @param mixed $result
@@ -17,12 +25,12 @@ class MixedFormat implements IFormat
public function call(mixed $result): ResponseInterface
{
if (is_object($result)) {
return \response()->withBody(new Stream('[object]'));
return $this->response->withBody(new Stream('[object]'));
}
if (is_array($result)) {
return \response()->withContentType(ContentType::JSON)->withBody(new Stream(json_encode($result, JSON_UNESCAPED_UNICODE)));
return $this->response->withContentType(ContentType::JSON)->withBody(new Stream(json_encode($result, JSON_UNESCAPED_UNICODE)));
} else {
return \response()->withBody(new Stream((string)$result));
return $this->response->withBody(new Stream((string)$result));
}
}