This commit is contained in:
2021-09-10 11:35:23 +08:00
parent 4753aee9c3
commit b497164f39
3 changed files with 256 additions and 261 deletions
+2 -1
View File
@@ -7,6 +7,7 @@ namespace Http\Route;
use Closure;
use Exception;
use Http\Context\Context;
use Http\Exception\RequestException;
use JetBrains\PhpStorm\Pure;
use Kiri\Events\EventProvider;
@@ -14,8 +15,8 @@ use Kiri\Exception\NotFindClassException;
use Kiri\Kiri;
use ReflectionException;
use Server\Constant;
use Server\Events\OnAfterWorkerStart;
use Server\Constrict\RequestInterface;
use Server\Events\OnAfterWorkerStart;
/**
* Class Node
+2 -2
View File
@@ -57,11 +57,11 @@ class Request implements RequestInterface
{
$serverRequest = ServerRequest::createServerRequest($request);
Context::setContext(ResponseInterface::class, $response = new Response());
Context::setContext(ResponseInterface::class, new Response());
Context::setContext(RequestInterface::class, $serverRequest);
return [Kiri::getDi()->get(Request::class), $response];
return Kiri::getDi()->get(Request::class);
}
+3 -9
View File
@@ -8,7 +8,7 @@ use Http\Exception\RequestException;
use Http\Route\Node;
use Kiri\Core\Help;
use Server\Constant;
use Server\Constrict\RequestInterface;
use Server\Constrict\Request as ScRequest;
use Server\Constrict\ResponseInterface;
use Server\Events\OnAfterRequest;
use Server\SInterface\OnClose;
@@ -42,22 +42,16 @@ class Http extends \Server\Abstracts\Http implements OnClose, OnConnect
public function onRequest(Request $request, Response $response): void
{
try {
[$request, $psr7Response] = \Server\Constrict\Request::create($request);
/** @var RequestInterface $request */
$node = $this->router->Branch_search($request);
$node = $this->router->Branch_search($Psr7Request = ScRequest::create($request));
if (!($node instanceof Node)) {
throw new RequestException(Constant::STATUS_404_MESSAGE, 404);
}
$psr7Response = $node->dispatch($request);
if (!($psr7Response instanceof ResponseInterface)) {
if (!(($psr7Response = $node->dispatch($Psr7Request)) instanceof ResponseInterface)) {
$psr7Response = $this->transferToResponse($psr7Response);
}
} catch (Error | \Throwable $exception) {
$psr7Response = $this->exceptionHandler->emit($exception, $this->response);
} finally {
if (!isset($psr7Response)) {
return;
}
$this->responseEmitter->sender($response, $psr7Response);
$this->eventDispatch->dispatch(new OnAfterRequest());
}