This commit is contained in:
2024-12-16 16:29:35 +08:00
parent daa02a6408
commit dc561cec9b
7 changed files with 89 additions and 49 deletions
+11 -1
View File
@@ -9,13 +9,23 @@ use Psr\Http\Message\ResponseInterface;
class ArrayFormat implements IFormat class ArrayFormat implements IFormat
{ {
/**
* @param ResponseInterface $response
*/
public function __construct(public ResponseInterface $response)
{
}
/** /**
* @param $result * @param $result
*
* @return ResponseInterface * @return ResponseInterface
*/ */
public function call($result): ResponseInterface public function call($result): ResponseInterface
{ {
return di(ResponseInterface::class)->withBody(new Stream(json_encode($result, JSON_UNESCAPED_UNICODE))); return $this->response->withBody(new Stream(json_encode($result, JSON_UNESCAPED_UNICODE)));
} }
+12 -4
View File
@@ -9,6 +9,15 @@ use Psr\Http\Message\ResponseInterface;
class MixedFormat implements IFormat class MixedFormat implements IFormat
{ {
/**
* @param ResponseInterface $response
*/
public function __construct(public ResponseInterface $response)
{
}
/** /**
* @param mixed $result * @param mixed $result
* @return ResponseInterface * @return ResponseInterface
@@ -18,14 +27,13 @@ class MixedFormat implements IFormat
if ($result instanceof ResponseInterface) { if ($result instanceof ResponseInterface) {
return $result; return $result;
} }
$response = Kiri::getDi()->get(ResponseInterface::class);
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->withBody(new Stream(json_encode($result, JSON_UNESCAPED_UNICODE))); return $this->response->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));
} }
} }
+14 -5
View File
@@ -9,27 +9,36 @@ use Psr\Http\Message\ResponseInterface;
class NoBody implements IFormat class NoBody implements IFormat
{ {
/**
* @param ResponseInterface $response
*/
public function __construct(public ResponseInterface $response)
{
}
/** /**
* @param $result * @param $result
*
* @return ResponseInterface * @return ResponseInterface
*/ */
public function call($result): ResponseInterface public function call($result): ResponseInterface
{ {
// TODO: Implement call() method. // TODO: Implement call() method.
$response = Kiri::getDi()->get(ResponseInterface::class);
if (request()->getMethod() === 'HEAD') { if (request()->getMethod() === 'HEAD') {
return $response->withBody(new Stream()); return $this->response->withBody(new Stream());
} }
if ($result instanceof ResponseInterface) { if ($result instanceof ResponseInterface) {
return $result; return $result;
} }
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->withBody(new Stream(json_encode($result, JSON_UNESCAPED_UNICODE))); return $this->response->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));
} }
} }
} }
+6 -1
View File
@@ -9,13 +9,18 @@ use Psr\Http\Message\ResponseInterface;
class OtherFormat implements IFormat class OtherFormat implements IFormat
{ {
public function __construct(public ResponseInterface $response)
{
}
/** /**
* @param mixed $result * @param mixed $result
* @return ResponseInterface * @return ResponseInterface
*/ */
public function call(mixed $result): ResponseInterface public function call(mixed $result): ResponseInterface
{ {
return di(ResponseInterface::class)->withBody(new Stream($result)); return $this->response->withBody(new Stream($result));
} }
+10 -1
View File
@@ -8,14 +8,23 @@ use Psr\Http\Message\ResponseInterface;
class VoidFormat implements IFormat class VoidFormat implements IFormat
{ {
/**
* @param ResponseInterface $response
*/
public function __construct(public ResponseInterface $response)
{
}
/** /**
* @param $result * @param $result
*
* @return ResponseInterface * @return ResponseInterface
*/ */
public function call($result): ResponseInterface public function call($result): ResponseInterface
{ {
// TODO: Implement call() method. // TODO: Implement call() method.
return di(ResponseInterface::class); return $this->response;
} }
} }
+1 -1
View File
@@ -49,7 +49,7 @@ class Handler implements RequestHandlerInterface
*/ */
public function setRequestMethod(string $method): void public function setRequestMethod(string $method): void
{ {
if ($method == 'HEAD') { if ($method == 'HEAD' || $method == 'OPTIONS') {
$this->format = Kiri::getDi()->get(NoBody::class); $this->format = Kiri::getDi()->get(NoBody::class);
} }
} }
-1
View File
@@ -7,7 +7,6 @@ use Kiri\Router\Base\AbstractHandler;
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface; use Psr\Http\Server\RequestHandlerInterface;
use ReflectionException;
class HttpRequestHandler extends AbstractHandler implements RequestHandlerInterface class HttpRequestHandler extends AbstractHandler implements RequestHandlerInterface
{ {