This commit is contained in:
2021-07-21 18:50:40 +08:00
parent b99551e326
commit 1e6333a18e
3 changed files with 22 additions and 16 deletions
+5 -5
View File
@@ -16,7 +16,6 @@ use Snowflake\Snowflake;
use Swoole\Error;
use Swoole\Http\Request;
use Swoole\Http\Response;
use Swoole\Http\Status;
use Throwable;
/**
@@ -49,7 +48,7 @@ class OnRequest extends Callback
* @return void
* @throws Exception
*/
public function onHandler(Request $request, Response $response): mixed
public function onHandler(Request $request, Response $response): void
{
try {
defer(fn() => fire(Event::SYSTEM_RESOURCE_RELEASES));
@@ -58,12 +57,13 @@ class OnRequest extends Callback
[$request, $response] = OnRequest::createContext($request, $response);
if ($request->is('favicon.ico')) {
return $response->close(404);
$response->close(404);
} else {
$this->router->dispatch();
}
return $this->router->dispatch();
} catch (ExitException | Error | Throwable $exception) {
$this->addError($exception, 'throwable');
return $this->sendErrorMessage($request, $response, $exception);
$this->sendErrorMessage($request, $response, $exception);
}
}
+2 -4
View File
@@ -82,7 +82,6 @@ class Request extends HttpService
$this->fd = $fd;
}
/**
* @return array|null
* @throws Exception
@@ -243,12 +242,11 @@ class Request extends HttpService
* @return mixed
* @throws Exception
*/
public function adapter(): mixed
public function adapter(): void
{
if (!$this->isHead()) {
return router()->dispatch();
router()->dispatch();
}
return '';
}
+15 -7
View File
@@ -8,6 +8,7 @@ use Exception;
use HttpServer\Abstracts\HttpService;
use HttpServer\Controller;
use HttpServer\Http\Request;
use HttpServer\Http\Response;
use HttpServer\IInterface\Middleware;
use HttpServer\IInterface\RouterInterface;
use JetBrains\PhpStorm\Pure;
@@ -43,6 +44,9 @@ class Router extends HttpService implements RouterInterface
public int $useTree = ROUTER_TREE;
public ?Response $response = null;
/**
* @param Closure $middleware
*/
@@ -54,11 +58,14 @@ class Router extends HttpService implements RouterInterface
/**
* @throws ConfigException
* @throws Exception
* 初始化函数路径
*/
public function init()
{
$this->dir = Config::get('http.namespace', $this->dir);
$this->response = Snowflake::app()->get('response');
}
@@ -513,17 +520,18 @@ class Router extends HttpService implements RouterInterface
* @return mixed
* @throws
*/
public function dispatch(): mixed
public function dispatch(): void
{
$node = $this->find_path(\request());
if (!($node instanceof Node)) {
return send(\request()->getUri() . ' -> ' . self::NOT_FOUND);
$this->response->setFormat(Response::HTML);
$this->response->send('<h1>404</h1>');
} else {
$this->response->send(($response = $node->dispatch()), 200);
if ($node->hasAfter()) {
$node->afterDispatch($response);
}
}
send(($response = $node->dispatch()), 200);
if (!$node->hasAfter()) {
return null;
}
return $node->afterDispatch($response);
}