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
+9 -2
View File
@@ -2,6 +2,7 @@
namespace Kiri\Router\Format; namespace Kiri\Router\Format;
use Kiri\Di\Inject\Container;
use Kiri\Router\Constrict\Stream; use Kiri\Router\Constrict\Stream;
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ResponseInterface;
@@ -9,14 +10,20 @@ class ArrayFormat implements IFormat
{ {
/**
* @var ResponseInterface
*/
#[Container(ResponseInterface::class)]
public ResponseInterface $response;
/** /**
* @param $result * @param $result
* @return ResponseInterface * @return ResponseInterface
*/ */
public function call($result): ResponseInterface public function call($result): ResponseInterface
{ {
$result = json_encode($result, JSON_UNESCAPED_UNICODE); return $this->response->withBody(new Stream(json_encode($result, JSON_UNESCAPED_UNICODE)));
return \response()->withBody(new Stream($result));
} }
+11 -3
View File
@@ -2,6 +2,7 @@
namespace Kiri\Router\Format; namespace Kiri\Router\Format;
use Kiri\Di\Inject\Container;
use Kiri\Router\Constrict\Stream; use Kiri\Router\Constrict\Stream;
use Kiri\Router\ContentType; use Kiri\Router\ContentType;
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ResponseInterface;
@@ -9,6 +10,13 @@ use Psr\Http\Message\ResponseInterface;
class MixedFormat implements IFormat class MixedFormat implements IFormat
{ {
/**
* @var ResponseInterface
*/
#[Container(ResponseInterface::class)]
public ResponseInterface $response;
/** /**
* @param mixed $result * @param mixed $result
@@ -17,12 +25,12 @@ class MixedFormat implements IFormat
public function call(mixed $result): ResponseInterface public function call(mixed $result): ResponseInterface
{ {
if (is_object($result)) { if (is_object($result)) {
return \response()->withBody(new Stream('[object]')); return $this->response->withBody(new Stream('[object]'));
} }
if (is_array($result)) { 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 { } else {
return \response()->withBody(new Stream((string)$result)); return $this->response->withBody(new Stream((string)$result));
} }
} }
+8 -1
View File
@@ -2,12 +2,19 @@
namespace Kiri\Router\Format; namespace Kiri\Router\Format;
use Kiri\Di\Inject\Container;
use Kiri\Router\Constrict\Stream; use Kiri\Router\Constrict\Stream;
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ResponseInterface;
class OtherFormat implements IFormat class OtherFormat implements IFormat
{ {
/**
* @var ResponseInterface
*/
#[Container(ResponseInterface::class)]
public ResponseInterface $response;
/** /**
* @param mixed $result * @param mixed $result
@@ -15,7 +22,7 @@ class OtherFormat implements IFormat
*/ */
public function call(mixed $result): ResponseInterface public function call(mixed $result): ResponseInterface
{ {
return \response()->withBody(new Stream($result)); return $this->response->withBody(new Stream($result));
} }
+10 -1
View File
@@ -2,12 +2,21 @@
namespace Kiri\Router\Format; namespace Kiri\Router\Format;
use Kiri\Di\Inject\Container;
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ResponseInterface;
class VoidFormat implements IFormat class VoidFormat implements IFormat
{ {
/**
* @var ResponseInterface
*/
#[Container(ResponseInterface::class)]
public ResponseInterface $response;
/** /**
* @param $result * @param $result
* @return ResponseInterface * @return ResponseInterface
@@ -15,7 +24,7 @@ class VoidFormat implements IFormat
public function call($result): ResponseInterface public function call($result): ResponseInterface
{ {
// TODO: Implement call() method. // TODO: Implement call() method.
return response(); return $this->response;
} }
} }