diff --git a/core/Jwt/Jwt.php b/core/Jwt/Jwt.php index 51fa4013..5792d617 100644 --- a/core/Jwt/Jwt.php +++ b/core/Jwt/Jwt.php @@ -3,6 +3,7 @@ declare(strict_types=1); namespace Kiri\Jwt; +use Annotation\Inject; use Exception; use Kiri\Abstracts\Component; use Kiri\Abstracts\Config; @@ -21,9 +22,19 @@ class Jwt extends Component use JwtHelper; + #[Inject(Request::class)] private Request $request; + /** + * @param Request $request + */ + public function setRequest(Request $request): void + { + $this->request = $request; + } + + /** * @throws ConfigException * @throws Exception @@ -96,7 +107,7 @@ class Jwt extends Component */ private function jwtBody($unionId): string { - $json = json_encode(['unionId' => $unionId, 'createTime' => time(), 'loginIp' => request()->getIp(), 'expire_at' => time() + $this->timeout]); + $json = json_encode(['unionId' => $unionId, 'createTime' => time(), 'expire_at' => time() + $this->timeout]); openssl_private_encrypt($json, $encode, $this->private); return base64_encode($encode); } diff --git a/function.php b/function.php index 53c2cf3d..6ddb9c94 100644 --- a/function.php +++ b/function.php @@ -710,19 +710,6 @@ if (!function_exists('request')) { } -if (!function_exists('Input')) { - - /** - * @return HttpParams - * @throws Exception - */ - function Input(): HttpParams - { - return request()->params; - } - -} - if (!function_exists('storage')) { /** @@ -771,20 +758,6 @@ if (!function_exists('event')) { } -if (!function_exists('alias')) { - - /** - * @param $class - * @param $name - */ - function alias($class, $name) - { - Kiri::setAlias($class, $name); - } - -} - - if (!function_exists('name')) { /** diff --git a/http-helper/Context/Request.php b/http-helper/Context/Request.php index 3f62352f..6610c88c 100644 --- a/http-helper/Context/Request.php +++ b/http-helper/Context/Request.php @@ -181,10 +181,10 @@ class Request extends HttpService implements RequestInterface /** - * @param $value - * @return mixed + * @param AuthIdentity $value + * @return AuthIdentity */ - public function setGrantAuthorization($value): mixed + public function setGrantAuthorization(AuthIdentity $value): AuthIdentity { return $this->_grant = $value; } diff --git a/http-helper/Route/Node.php b/http-helper/Route/Node.php index 82f2d124..48bc56d8 100644 --- a/http-helper/Route/Node.php +++ b/http-helper/Route/Node.php @@ -287,7 +287,7 @@ class Node public function checkSuffix(): bool { if ($this->enableHtmlSuffix) { - $url = request()->getUri(); + $url = request()->getUri()->getPath(); $nowLength = strlen($this->htmlSuffix); if (strpos($url, $this->htmlSuffix) !== strlen($url) - $nowLength) { return false; diff --git a/http-helper/Route/Router.php b/http-helper/Route/Router.php index 2fcc9b2d..0e641b10 100644 --- a/http-helper/Route/Router.php +++ b/http-helper/Route/Router.php @@ -3,6 +3,7 @@ declare(strict_types=1); namespace Http\Route; +use Annotation\Inject; use Closure; use Exception; use Http\Abstracts\HttpService; @@ -46,6 +47,13 @@ class Router extends HttpService implements RouterInterface public ?Response $response = null; + /** + * @var RequestInterface + */ + #[Inject(RequestInterface::class)] + public RequestInterface $request; + + /** * @param Closure $middleware */ @@ -495,7 +503,7 @@ class Router extends HttpService implements RouterInterface { $method = $request->getMethod(); - $methods = $this->nodes[$method][\request()->getUri()] ?? null; + $methods = $this->nodes[$method][\request()->getUri()->getPath()] ?? null; if (!is_null($methods)) { return $methods; } @@ -548,11 +556,12 @@ class Router extends HttpService implements RouterInterface */ public function Branch_search(RequestInterface $request): ?Node { - $node = $this->tree_search($request->getExplode(), $request->getMethod()); + $uri = $request->getUri(); + $node = $this->tree_search($uri->getExplode(), $request->getMethod()); if ($node instanceof Node) { return $node; } - if (!$request->isOption) { + if (!$request->isMethod('OPTIONS')) { return null; } $node = $this->tree_search(['*'], $request->getMethod()); diff --git a/http-server/Message/Request.php b/http-server/Message/Request.php index b442967f..65984b56 100644 --- a/http-server/Message/Request.php +++ b/http-server/Message/Request.php @@ -23,7 +23,10 @@ class Request implements RequestInterface public string $method; - public UriInterface $uri; + /** + * @var Uri + */ + private Uri $uri; private \Swoole\Http\Request $serverRequest; @@ -152,6 +155,16 @@ class Request implements RequestInterface } + /** + * @param string $method + * @return bool + */ + public function isMethod(string $method): bool + { + return $this->method === $method; + } + + /** * @param string $method * @return RequestInterface diff --git a/http-server/Message/Uri.php b/http-server/Message/Uri.php index 3559622c..fb07bcd3 100644 --- a/http-server/Message/Uri.php +++ b/http-server/Message/Uri.php @@ -33,6 +33,24 @@ class Uri implements UriInterface public string $password = ''; + private array $_explode = []; + + + /** + * @return string[] + */ + public function getExplode(): array + { + if ($this->path == '/' || $this->path == '') { + return ['']; + } + if (empty($this->_explode)) { + $this->_explode = array_filter(explode('/', $this->path)); + } + return $this->_explode; + } + + /** * @return string */ diff --git a/http-server/RequestInterface.php b/http-server/RequestInterface.php index c8536e4a..c06d465c 100644 --- a/http-server/RequestInterface.php +++ b/http-server/RequestInterface.php @@ -3,7 +3,8 @@ namespace Server; -use Http\Context\Request; + +use Server\Message\Request; /** *