111
This commit is contained in:
@@ -31,28 +31,51 @@ class TestRequest
|
|||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
[$PsrRequest, $PsrResponse] = $this->initRequestResponse($request);
|
[$PsrRequest, $PsrResponse] = $this->initRequestResponse($request);
|
||||||
|
|
||||||
/** @var Handler $handler */
|
/** @var Handler $handler */
|
||||||
$handler = HandlerManager::get($request->server['request_uri'], $request->getMethod());
|
$handler = HandlerManager::get($request->server['request_uri'], $request->getMethod());
|
||||||
if (is_null($handler)) {
|
if (is_integer($handler)) {
|
||||||
$PsrResponse->withStatus(404)->withBody(null);
|
|
||||||
} else if (is_integer($handler)) {
|
|
||||||
$PsrResponse->withStatus($handler)->withBody(null);
|
$PsrResponse->withStatus($handler)->withBody(null);
|
||||||
|
} else if (is_null($handler)) {
|
||||||
|
$PsrResponse->withStatus(404)->withBody(null);
|
||||||
} else {
|
} else {
|
||||||
$middlewares = MiddlewareManager::get($handler->callback[0]::class, $handler->callback[1]);
|
$PsrResponse = $this->handler($handler, $PsrRequest);
|
||||||
|
|
||||||
$stream = new Stream((new Dispatcher($handler, $middlewares))->handle($PsrRequest));
|
|
||||||
|
|
||||||
$PsrResponse->withStatus(200)->withBody($stream);
|
|
||||||
}
|
}
|
||||||
} catch (\Throwable $throwable) {
|
} catch (\Throwable $throwable) {
|
||||||
|
$PsrResponse = $this->throwable($throwable);
|
||||||
} finally {
|
} finally {
|
||||||
$this->response->sender($response, $PsrResponse);
|
$this->response->sender($response, $PsrResponse);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param \Throwable $throwable
|
||||||
|
* @return \Psr\Http\Message\ResponseInterface
|
||||||
|
*/
|
||||||
|
protected function throwable(\Throwable $throwable): ResponseInterface
|
||||||
|
{
|
||||||
|
return \response()->withStatus($throwable->getCode())
|
||||||
|
->withContentType(\Http\Message\Response::CONTENT_TYPE_HTML)
|
||||||
|
->withBody(new Stream(jTraceEx($throwable)));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param \Http\Handler\Handler $handler
|
||||||
|
* @param $PsrRequest
|
||||||
|
* @return \Psr\Http\Message\ResponseInterface
|
||||||
|
* @throws \Exception
|
||||||
|
*/
|
||||||
|
protected function handler(Handler $handler, $PsrRequest): ResponseInterface
|
||||||
|
{
|
||||||
|
$middlewares = MiddlewareManager::get($handler->callback);
|
||||||
|
|
||||||
|
$dispatcher = new Dispatcher($handler, $middlewares);
|
||||||
|
|
||||||
|
return $dispatcher->handle($PsrRequest);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Request $request
|
* @param Request $request
|
||||||
* @return array<ServerRequestInterface, ResponseInterface>
|
* @return array<ServerRequestInterface, ResponseInterface>
|
||||||
@@ -60,7 +83,7 @@ class TestRequest
|
|||||||
*/
|
*/
|
||||||
private function initRequestResponse(Request $request): array
|
private function initRequestResponse(Request $request): array
|
||||||
{
|
{
|
||||||
$PsrResponse = Context::setContext(\Server\Constrict\ResponseInterface::class, new \Http\Message\Response());
|
$PsrResponse = Context::setContext(ResponseInterface::class, new \Http\Message\Response());
|
||||||
|
|
||||||
$PsrRequest = Context::setContext(RequestInterface::class, ServerRequest::createServerRequest($request));
|
$PsrRequest = Context::setContext(RequestInterface::class, ServerRequest::createServerRequest($request));
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user