From 0d40f33b0efa8cf1871023950f9886cee44edb1e Mon Sep 17 00:00:00 2001 From: liujian Date: Wed, 24 Jan 2024 19:00:28 +0800 Subject: [PATCH] Update vaga --- src/Engine.php | 49 ++++++++++++++++++++++++++++++++----------------- src/Router.php | 16 +++++++++++----- 2 files changed, 43 insertions(+), 22 deletions(-) diff --git a/src/Engine.php b/src/Engine.php index 9fc65a9..f4532c6 100644 --- a/src/Engine.php +++ b/src/Engine.php @@ -137,14 +137,19 @@ public function run(): bool * @param array $args * @return bool */ - private static function isSwoole(array $args): bool + public static function isSwoole(array $args): bool { - if (count($args) != 2) { - return false; + if (count($args) == 1) { + list($request) = $args; + if ($request instanceof \Swoole\Http\Request) { + return true; + } } - list($request, $response) = $args; - if ($request instanceof \Swoole\Http\Request && $response instanceof \Swoole\Http\Response) { - return true; + if (count($args) == 2) { + list($request, $response) = $args; + if ($request instanceof \Swoole\Http\Request && $response instanceof \Swoole\Http\Response) { + return true; + } } return false; } @@ -153,14 +158,19 @@ private static function isSwoole(array $args): bool * @param array $args * @return bool */ - private static function isWorkerMan(array $args): bool + public static function isWorkerMan(array $args): bool { - if (count($args) != 2) { - return false; + if (count($args) == 1) { + list($connection) = $args; + if ($connection instanceof \Workerman\Connection\TcpConnection) { + return true; + } } - list($connection, $request) = $args; - if ($connection instanceof \Workerman\Connection\TcpConnection && $request instanceof \Workerman\Protocols\Http\Request) { - return true; + if (count($args) == 2) { + list($connection, $request) = $args; + if ($connection instanceof \Workerman\Connection\TcpConnection && $request instanceof \Workerman\Protocols\Http\Request) { + return true; + } } return false; } @@ -171,12 +181,17 @@ private static function isWorkerMan(array $args): bool */ private static function isSwow(array $args): bool { - if (count($args) != 2) { - return false; + if (count($args) == 1) { + list($request) = $args; + if ($request instanceof \Psr\Http\Message\RequestInterface) { + return true; + } } - list($request, $response) = $args; - if ($request instanceof \Psr\Http\Message\RequestInterface && $response instanceof \Swow\Psr7\Server\ServerConnection) { - return true; + if (count($args) == 2) { + list($request, $response) = $args; + if ($request instanceof \Psr\Http\Message\RequestInterface && $response instanceof \Swow\Psr7\Server\ServerConnection) { + return true; + } } return false; } diff --git a/src/Router.php b/src/Router.php index 7f4e557..de931d7 100644 --- a/src/Router.php +++ b/src/Router.php @@ -123,13 +123,19 @@ protected function dispatch(string $method, string $uri, Context $ctx): void break; case \FastRoute\Dispatcher::FOUND: $handler = $routeInfo[1]; - $vars = $routeInfo[2]; - // with $vars + $params = $routeInfo[2]; + // with $params if ($ctx->request instanceof ServerRequest) { - $ctx->request->withRouteParams($vars); + $ctx->request->withRouteParams($params); } else { - // 原本是为了在 FAST_MODE 下也可以获取到路由参数,但是php8.2废弃了动态增加属性,因此注释该功能 - // $ctx->request->param = $vars; + // 为了在 FAST_MODE 下也可以获取到路由参数,但是php8.2废弃了动态增加属性 + if (Engine::isSwoole([$ctx->request])) { + // 只能放在get里面 + $ctx->request->get = $params + $ctx->request->get; + } elseif (Engine::isWorkerMan([$ctx->request])) { + // WorkerMan有__set方法,可以动态增加属性 + $ctx->request->param = $params; + } } // call $handler $handler($ctx);