This commit is contained in:
as2252258@163.com
2021-09-20 17:10:52 +08:00
parent e6cf4ec493
commit 4830ada35d
+100 -90
View File
@@ -18,115 +18,125 @@ abstract class Handler implements RequestHandlerInterface
{ {
private int $offset = 0; private int $offset = 0;
protected CHl $handler; protected CHl $handler;
#[Inject(AspectProxy::class)] #[Inject(AspectProxy::class)]
protected AspectProxy $aspectProxy; protected AspectProxy $aspectProxy;
protected ?array $middlewares; protected ?array $middlewares;
/**
* @param CHl $handler
* @return $this
*/
public function setHandler(CHl $handler): static
{
$this->handler = $handler;
return $this;
}
/** /**
* @param array|null $middlewares * @param \Kiri\Proxy\AspectProxy $aspectProxy
* @return $this */
*/ public function setAspectProxy(AspectProxy $aspectProxy)
public function setMiddlewares(?array $middlewares): static {
{ $this->aspectProxy = $aspectProxy;
$this->middlewares = $middlewares; }
return $this;
}
/** /**
* @param ServerRequestInterface $request * @param CHl $handler
* @return ResponseInterface * @return $this
* @throws \Exception */
*/ public function setHandler(CHl $handler): static
protected function execute(ServerRequestInterface $request): ResponseInterface {
{ $this->handler = $handler;
if (empty($this->middlewares) || !isset($this->middlewares[$this->offset])) { return $this;
return $this->dispatcher($request); }
}
$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);
}
/** /**
* @param ServerRequestInterface $request * @param array|null $middlewares
* @return mixed * @return $this
* @throws \Exception */
*/ public function setMiddlewares(?array $middlewares): static
protected function dispatcher(ServerRequestInterface $request): mixed {
{ $this->middlewares = $middlewares;
return $this;
}
/**
* @param ServerRequestInterface $request
* @return ResponseInterface
* @throws \Exception
*/
protected function execute(ServerRequestInterface $request): ResponseInterface
{
if (empty($this->middlewares) || !isset($this->middlewares[$this->offset])) {
return $this->dispatcher($request);
}
$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);
}
/**
* @param ServerRequestInterface $request
* @return mixed
* @throws \Exception
*/
protected function dispatcher(ServerRequestInterface $request): mixed
{
$response = $this->aspectProxy->proxy($this->handler); $response = $this->aspectProxy->proxy($this->handler);
if (!($response instanceof ResponseInterface)) { if (!($response instanceof ResponseInterface)) {
$response = $this->transferToResponse($response); $response = $this->transferToResponse($response);
} }
$response->withHeader('Run-Time', $this->_runTime($request)); $response->withHeader('Run-Time', $this->_runTime($request));
return $response; return $response;
} }
/** /**
* @param ServerRequest $request * @param ServerRequest $request
* @return float * @return float
*/ */
private function _runTime(ServerRequestInterface $request): float private function _runTime(ServerRequestInterface $request): float
{ {
$float = microtime(true) - time(); $float = microtime(true) - time();
$serverParams = $request->getServerParams(); $serverParams = $request->getServerParams();
$rTime = $serverParams['request_time_float'] - $serverParams['request_time']; $rTime = $serverParams['request_time_float'] - $serverParams['request_time'];
return round($float - $rTime, 6); return round($float - $rTime, 6);
} }
/** /**
* @param mixed $responseData * @param mixed $responseData
* @return \Server\Constrict\ResponseInterface * @return \Server\Constrict\ResponseInterface
* @throws \Exception * @throws \Exception
*/ */
private function transferToResponse(mixed $responseData): ResponseInterface private function transferToResponse(mixed $responseData): ResponseInterface
{ {
$interface = response()->withStatus(200); $interface = response()->withStatus(200);
if (!$interface->hasContentType()) { if (!$interface->hasContentType()) {
$interface->withContentType('application/json;charset=utf-8'); $interface->withContentType('application/json;charset=utf-8');
} }
if (is_object($responseData)) { if (is_object($responseData)) {
$responseData = get_object_vars($responseData); $responseData = get_object_vars($responseData);
} }
if ($interface->getContentType() == 'application/xml;charset=utf-8') { if ($interface->getContentType() == 'application/xml;charset=utf-8') {
$interface->getBody()->write(Help::toXml($responseData)); $interface->getBody()->write(Help::toXml($responseData));
} else if (is_array($responseData)) { } else if (is_array($responseData)) {
$interface->getBody()->write(json_encode($responseData)); $interface->getBody()->write(json_encode($responseData));
} else { } else {
$interface->getBody()->write((string)$responseData); $interface->getBody()->write((string)$responseData);
} }
return $interface; return $interface;
} }
} }