Skip to content

Commit

Permalink
Merge pull request #71 from phper666/70-call-to-undefined-function-ph…
Browse files Browse the repository at this point in the history
…per666jwtauthmake

70 call to undefined function phper666jwtauthmake
  • Loading branch information
phper666 authored Feb 2, 2024
2 parents 198a082 + abbb142 commit 0938a96
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 10 deletions.
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
"autoload": {
"psr-4": {
"Phper666\\JWTAuth\\": "src/"
}
},
"files": [
"src/Functions.php"
]
},
"autoload-dev": {},
"require": {
Expand Down
4 changes: 2 additions & 2 deletions src/AbstractJWT.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ abstract function matchRoute(string $scene, string $requestMethod, string $reque
* 获取jwt中的场景值
*
* @param string $token
* @return bool
* @return mixed
*/
abstract function getSceneByToken(string $token): bool;
abstract function getSceneByToken(string $token): array;

/**
* 刷新jwt token
Expand Down
36 changes: 36 additions & 0 deletions src/Functions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
declare(strict_types=1);
/**
* Created by PhpStorm.
* User: liyuzhao
* Date: 2024/2/2
* Time: 20:01
*/

namespace Phper666\JWTAuth;

if (!function_exists('make')) {
function make(string $name, array $parameters = [])
{
if (class_exists('\Hyperf\Context\ApplicationContext')) {
if (\Hyperf\Context\ApplicationContext::hasContainer()) {
/** @var \Hyperf\Di\Container $container */
$container = \Hyperf\Context\ApplicationContext::getContainer();
if (method_exists($container, 'make')) {
return $container->make($name, $parameters);
}
}
} elseif (class_exists('\Hyperf\Utils\ApplicationContext')) {
if (\Hyperf\Utils\ApplicationContext::hasContainer()) {
/** @var \Hyperf\Di\Container $container */
$container = \Hyperf\Utils\ApplicationContext::getContainer();
if (method_exists($container, 'make')) {
return $container->make($name, $parameters);
}
}
}

$parameters = array_values($parameters);
return new $name(...$parameters);
}
}
7 changes: 4 additions & 3 deletions src/JWT.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ public function getToken(string $scene, array $claims): Plain
*
* @return mixed
*/
public function getJwtSceneConfig(string $scene = null) {
public function getJwtSceneConfig(string $scene = null): array {
if ($scene == null) {
return $this->jwtConfig[$this->getScene()];
}
Expand Down Expand Up @@ -501,10 +501,11 @@ public function getTTL(string $token): int
return (int)$sceneConfig['ttl'];
}

public function getSceneByToken(string $token): bool
public function getSceneByToken(string $token): array
{
if($token == null) {
$token = JWTUtil::getToken($this->request);
if (!$token) return [];
}
$token = $this->tokenToPlain($token);
$scene = $this->getSceneByTokenPlain($token);
Expand Down Expand Up @@ -572,7 +573,7 @@ protected function validationConstraints(DataSet $claims, Configuration $configu
* 通过token获取当前场景的配置
*
* @param Plain $token
* @return string
* @return array
*/
protected function getSceneConfigByToken(Plain $token): array
{
Expand Down
7 changes: 3 additions & 4 deletions src/Util/JWTUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class JWTUtil
* @param $claims
* @return mixed
*/
public static function claimsToArray(DataSet $claims)
public static function claimsToArray(DataSet $claims): array
{
return $claims->all();
}
Expand All @@ -38,16 +38,15 @@ public static function claimsToArray(DataSet $claims)
public static function getToken(ServerRequestInterface $request)
{
$token = $request->getHeaderLine('Authorization') ?? '';
$token = self::handleToken($token);
return $token;
return self::handleToken($token);
}

/**
* 解析token
* @param ServerRequestInterface $request
* @return array
*/
public static function getParserData(ServerRequestInterface $request)
public static function getParserData(ServerRequestInterface $request): array
{
$token = $request->getHeaderLine('Authorization') ?? '';
$token = self::handleToken($token);
Expand Down

0 comments on commit 0938a96

Please sign in to comment.