This commit is contained in:
2021-08-27 17:06:17 +08:00
parent 2f76d86069
commit daef261403
8 changed files with 62 additions and 37 deletions
+12 -1
View File
@@ -3,6 +3,7 @@ declare(strict_types=1);
namespace Kiri\Jwt; namespace Kiri\Jwt;
use Annotation\Inject;
use Exception; use Exception;
use Kiri\Abstracts\Component; use Kiri\Abstracts\Component;
use Kiri\Abstracts\Config; use Kiri\Abstracts\Config;
@@ -21,9 +22,19 @@ class Jwt extends Component
use JwtHelper; use JwtHelper;
#[Inject(Request::class)]
private Request $request; private Request $request;
/**
* @param Request $request
*/
public function setRequest(Request $request): void
{
$this->request = $request;
}
/** /**
* @throws ConfigException * @throws ConfigException
* @throws Exception * @throws Exception
@@ -96,7 +107,7 @@ class Jwt extends Component
*/ */
private function jwtBody($unionId): string 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); openssl_private_encrypt($json, $encode, $this->private);
return base64_encode($encode); return base64_encode($encode);
} }
-27
View File
@@ -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')) { 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')) { if (!function_exists('name')) {
/** /**
+3 -3
View File
@@ -181,10 +181,10 @@ class Request extends HttpService implements RequestInterface
/** /**
* @param $value * @param AuthIdentity $value
* @return mixed * @return AuthIdentity
*/ */
public function setGrantAuthorization($value): mixed public function setGrantAuthorization(AuthIdentity $value): AuthIdentity
{ {
return $this->_grant = $value; return $this->_grant = $value;
} }
+1 -1
View File
@@ -287,7 +287,7 @@ class Node
public function checkSuffix(): bool public function checkSuffix(): bool
{ {
if ($this->enableHtmlSuffix) { if ($this->enableHtmlSuffix) {
$url = request()->getUri(); $url = request()->getUri()->getPath();
$nowLength = strlen($this->htmlSuffix); $nowLength = strlen($this->htmlSuffix);
if (strpos($url, $this->htmlSuffix) !== strlen($url) - $nowLength) { if (strpos($url, $this->htmlSuffix) !== strlen($url) - $nowLength) {
return false; return false;
+12 -3
View File
@@ -3,6 +3,7 @@ declare(strict_types=1);
namespace Http\Route; namespace Http\Route;
use Annotation\Inject;
use Closure; use Closure;
use Exception; use Exception;
use Http\Abstracts\HttpService; use Http\Abstracts\HttpService;
@@ -46,6 +47,13 @@ class Router extends HttpService implements RouterInterface
public ?Response $response = null; public ?Response $response = null;
/**
* @var RequestInterface
*/
#[Inject(RequestInterface::class)]
public RequestInterface $request;
/** /**
* @param Closure $middleware * @param Closure $middleware
*/ */
@@ -495,7 +503,7 @@ class Router extends HttpService implements RouterInterface
{ {
$method = $request->getMethod(); $method = $request->getMethod();
$methods = $this->nodes[$method][\request()->getUri()] ?? null; $methods = $this->nodes[$method][\request()->getUri()->getPath()] ?? null;
if (!is_null($methods)) { if (!is_null($methods)) {
return $methods; return $methods;
} }
@@ -548,11 +556,12 @@ class Router extends HttpService implements RouterInterface
*/ */
public function Branch_search(RequestInterface $request): ?Node 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) { if ($node instanceof Node) {
return $node; return $node;
} }
if (!$request->isOption) { if (!$request->isMethod('OPTIONS')) {
return null; return null;
} }
$node = $this->tree_search(['*'], $request->getMethod()); $node = $this->tree_search(['*'], $request->getMethod());
+14 -1
View File
@@ -23,7 +23,10 @@ class Request implements RequestInterface
public string $method; public string $method;
public UriInterface $uri; /**
* @var Uri
*/
private Uri $uri;
private \Swoole\Http\Request $serverRequest; 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 * @param string $method
* @return RequestInterface * @return RequestInterface
+18
View File
@@ -33,6 +33,24 @@ class Uri implements UriInterface
public string $password = ''; 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 * @return string
*/ */
+2 -1
View File
@@ -3,7 +3,8 @@
namespace Server; namespace Server;
use Http\Context\Request;
use Server\Message\Request;
/** /**
* *