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;
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);
}
-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')) {
/**
@@ -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')) {
/**
+3 -3
View File
@@ -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;
}
+1 -1
View File
@@ -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;
+12 -3
View File
@@ -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());
+14 -1
View File
@@ -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
+18
View File
@@ -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
*/
+2 -1
View File
@@ -3,7 +3,8 @@
namespace Server;
use Http\Context\Request;
use Server\Message\Request;
/**
*