Files
kiri-core/http-handler/Abstracts/Handler.php
T

129 lines
2.9 KiB
PHP
Raw Normal View History

2021-09-17 18:55:08 +08:00
<?php
namespace Http\Handler\Abstracts;
2021-09-18 10:38:38 +08:00
use Http\Handler\Handler as CHl;
2021-09-18 11:44:42 +08:00
use Http\Message\ServerRequest;
2021-09-18 11:20:58 +08:00
use Kiri\Core\Help;
2021-09-18 10:38:38 +08:00
use Kiri\Kiri;
2021-09-19 16:42:29 +08:00
use Kiri\Proxy\AspectProxy;
2021-09-17 18:55:08 +08:00
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
abstract class Handler implements RequestHandlerInterface
{
private int $offset = 0;
2021-09-18 13:45:33 +08:00
protected CHl $handler;
protected ?array $middlewares;
2021-09-17 18:55:08 +08:00
/**
* @param CHl $handler
2021-09-18 13:45:33 +08:00
* @return $this
*/
public function setHandler(CHl $handler): static
{
$this->handler = $handler;
return $this;
}
/**
2021-09-17 18:55:08 +08:00
* @param array|null $middlewares
2021-09-18 13:45:33 +08:00
* @return $this
2021-09-17 18:55:08 +08:00
*/
2021-09-18 13:45:33 +08:00
public function setMiddlewares(?array $middlewares): static
2021-09-17 18:55:08 +08:00
{
2021-09-18 13:45:33 +08:00
$this->middlewares = $middlewares;
return $this;
2021-09-17 18:55:08 +08:00
}
/**
* @param ServerRequestInterface $request
* @return ResponseInterface
* @throws \Exception
*/
protected function execute(ServerRequestInterface $request): ResponseInterface
{
if (empty($this->middlewares) || !isset($this->middlewares[$this->offset])) {
2021-09-18 11:44:42 +08:00
return $this->dispatcher($request);
2021-09-17 18:55:08 +08:00
}
$middleware = $this->middlewares[$this->offset];
if (!($middleware instanceof MiddlewareInterface)) {
throw new \Exception('get_implements_class($middleware) not found method process.');
}
++$this->offset;
return $middleware->process($request, $this);
}
2021-09-18 10:50:59 +08:00
/**
2021-09-18 11:44:42 +08:00
* @param ServerRequestInterface $request
2021-09-18 10:50:59 +08:00
* @return mixed
2021-09-18 11:20:58 +08:00
* @throws \Exception
2021-09-18 10:50:59 +08:00
*/
2021-09-18 11:44:42 +08:00
protected function dispatcher(ServerRequestInterface $request): mixed
2021-09-18 10:50:59 +08:00
{
2021-09-19 16:42:29 +08:00
$aspect = Kiri::getDi()->get(AspectProxy::class);
if (!(($response = $aspect->proxy($this->handler)) instanceof ResponseInterface)) {
2021-09-18 11:20:58 +08:00
$response = $this->transferToResponse($response);
}
2021-09-18 11:44:42 +08:00
$response->withHeader('Run-Time', $this->_runTime($request));
2021-09-18 11:20:58 +08:00
return $response;
}
2021-09-18 11:44:42 +08:00
/**
* @param ServerRequest $request
* @return float
*/
private function _runTime(ServerRequestInterface $request): float
{
$float = microtime(true) - time();
$serverParams = $request->getServerParams();
$rTime = $serverParams['request_time_float'] - $serverParams['request_time'];
return round($float - $rTime, 6);
}
2021-09-18 11:20:58 +08:00
/**
* @param mixed $responseData
* @return \Server\Constrict\ResponseInterface
* @throws \Exception
*/
private function transferToResponse(mixed $responseData): ResponseInterface
{
$interface = response()->withStatus(200);
if (!$interface->hasContentType()) {
$interface->withContentType('application/json;charset=utf-8');
}
if (is_object($responseData)) {
$responseData = get_object_vars($responseData);
}
if ($interface->getContentType() == 'application/xml;charset=utf-8') {
$interface->getBody()->write(Help::toXml($responseData));
} else if (is_array($responseData)) {
$interface->getBody()->write(json_encode($responseData));
} else {
$interface->getBody()->write((string)$responseData);
}
return $interface;
2021-09-18 10:50:59 +08:00
}
2021-09-17 18:55:08 +08:00
}