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
+18 -8
View File
@@ -9,14 +9,24 @@ use Psr\Http\Message\ResponseInterface;
class ArrayFormat implements IFormat class ArrayFormat implements IFormat
{ {
/**
* @param $result /**
* @return ResponseInterface * @param ResponseInterface $response
*/ */
public function call($result): ResponseInterface public function __construct(public ResponseInterface $response)
{ {
return di(ResponseInterface::class)->withBody(new Stream(json_encode($result, JSON_UNESCAPED_UNICODE))); }
}
/**
* @param $result
*
* @return ResponseInterface
*/
public function call($result): ResponseInterface
{
return $this->response->withBody(new Stream(json_encode($result, JSON_UNESCAPED_UNICODE)));
}
} }
+13 -5
View File
@@ -9,7 +9,16 @@ 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));
} }
} }
+32 -23
View File
@@ -9,27 +9,36 @@ use Psr\Http\Message\ResponseInterface;
class NoBody implements IFormat class NoBody implements IFormat
{ {
/**
* @param $result /**
* @return ResponseInterface * @param ResponseInterface $response
*/ */
public function call($result): ResponseInterface public function __construct(public ResponseInterface $response)
{ {
// TODO: Implement call() method. }
$response = Kiri::getDi()->get(ResponseInterface::class);
if (request()->getMethod() === 'HEAD') {
return $response->withBody(new Stream()); /**
} * @param $result
if ($result instanceof ResponseInterface) { *
return $result; * @return ResponseInterface
} */
if (is_object($result)) { public function call($result): ResponseInterface
return $response->withBody(new Stream('[object]')); {
} // TODO: Implement call() method.
if (is_array($result)) { if (request()->getMethod() === 'HEAD') {
return $response->withBody(new Stream(json_encode($result, JSON_UNESCAPED_UNICODE))); return $this->response->withBody(new Stream());
} else { }
return $response->withBody(new Stream((string)$result)); if ($result instanceof ResponseInterface) {
} return $result;
} }
if (is_object($result)) {
return $this->response->withBody(new Stream('[object]'));
}
if (is_array($result)) {
return $this->response->withBody(new Stream(json_encode($result, JSON_UNESCAPED_UNICODE)));
} else {
return $this->response->withBody(new Stream((string)$result));
}
}
} }
+7 -2
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));
} }
+18 -9
View File
@@ -8,14 +8,23 @@ use Psr\Http\Message\ResponseInterface;
class VoidFormat implements IFormat class VoidFormat implements IFormat
{ {
/**
* @param $result /**
* @return ResponseInterface * @param ResponseInterface $response
*/ */
public function call($result): ResponseInterface public function __construct(public ResponseInterface $response)
{ {
// TODO: Implement call() method. }
return di(ResponseInterface::class);
} /**
* @param $result
*
* @return ResponseInterface
*/
public function call($result): ResponseInterface
{
// TODO: Implement call() method.
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
{ {