Skip to content

Commit

Permalink
Merge pull request #4 from mineadmin/2.0-dev
Browse files Browse the repository at this point in the history
2.0 dev
  • Loading branch information
zds-s authored Jan 22, 2024
2 parents 4d4b6a8 + 535c0b1 commit bb1b4b9
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 12 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
composer.lock
.idea
vendor
*.cache
*.cache
runtime
2 changes: 2 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<directory suffix="Test.php">./src/office</directory>
<directory suffix="Test.php">./src/gateway</directory>
<directory suffix="Test.php">./src/translatable</directory>
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
<source>
Expand All @@ -17,6 +18,7 @@
<directory suffix=".php">./src/office/tests</directory>
<directory suffix=".php">./src/gateway/tests</directory>
<directory suffix=".php">./src/translatable/tests</directory>
<directory suffix=".php">./tests</directory>
</include>
</source>
</phpunit>
15 changes: 15 additions & 0 deletions src/jwt-auth/src/Exception/TokenExpireException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);
/**
* This file is part of MineAdmin.
*
* @link https://www.mineadmin.com
* @document https://doc.mineadmin.com
* @contact [email protected]
* @license https://github.com/mineadmin/MineAdmin/blob/master/LICENSE
*/

namespace Xmo\JWTAuth\Exception;

class TokenExpireException extends \RuntimeException {}
6 changes: 4 additions & 2 deletions src/jwt-auth/src/Util/JWTUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Lcobucci\JWT\Validation\Constraint\IdentifiedBy;
use Lcobucci\JWT\Validation\Constraint\SignedWith;
use Lcobucci\JWT\ValidationData;
use Xmo\JWTAuth\Exception\TokenExpireException;

/**
* JWT工具类.
Expand Down Expand Up @@ -83,15 +84,16 @@ public static function getParser(Signer $signer, Key $key)
/**
* @return ValidationData
*/
public static function getValidationData(Signer $signer, Key $key, string $token)
public static function getValidationData(Signer $signer, Key $key, string $token): bool
{
$config = self::getConfiguration($signer, $key);
$parser = $config->parser()->parse($token);
$claims = $parser->claims()->all();
$now = new \DateTimeImmutable();

// 这是基于用户给定的。不是后端解析后来判断的
if ($claims['nbf'] > $now || $claims['exp'] < $now) {
return false;
throw new TokenExpireException('Token has expired');
}

$config->setValidationConstraints(new IdentifiedBy($claims['jti']));
Expand Down
21 changes: 17 additions & 4 deletions src/mine-helpers/src/Ip2region.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@

namespace Mine\Helper;

use Composer\Autoload\ClassLoader;
use Hyperf\Contract\StdoutLoggerInterface;
use Hyperf\Support\Composer;

class Ip2region
{
Expand All @@ -23,17 +23,17 @@ class Ip2region
* @see https://github.com/zoujingli/ip2region
* @throws \Exception
*/
public function __construct(protected StdoutLoggerInterface $logger)
public function __construct(protected ?StdoutLoggerInterface $logger = null)
{
$composerLoader = Composer::getLoader();
$composerLoader = $this->getLoader();
$path = $composerLoader->findFile(\XdbSearcher::class);

$dbFile = dirname(realpath($path)) . '/ip2region.xdb';

// 1、从 dbPath 加载整个 xdb 到内存。
$cBuff = \XdbSearcher::loadContentFromFile($dbFile);
if ($cBuff === null) {
$this->logger->error('failed to load content buffer from {db_file}', ['db_file' => $dbFile]);
$this->logger?->error('failed to load content buffer from {db_file}', ['db_file' => $dbFile]);
return;
}
// 2、使用全局的 cBuff 创建带完全基于内存的查询对象。
Expand All @@ -59,4 +59,17 @@ public function search(string $ip): string
}
return $country;
}

private function getLoader(): ClassLoader
{
$loaders = spl_autoload_functions();

foreach ($loaders as $loader) {
if (is_array($loader) && $loader[0] instanceof ClassLoader) {
return $loader[0];
}
}

throw new \RuntimeException('Composer loader not found.');
}
}
5 changes: 4 additions & 1 deletion src/mine-helpers/src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Hyperf\Context\ApplicationContext;
use Hyperf\Context\Context;
use Hyperf\Contract\StdoutLoggerInterface;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\Logger\LoggerFactory;
use Hyperf\Redis\Redis;
use Hyperf\Snowflake\IdGeneratorInterface;
Expand Down Expand Up @@ -103,7 +104,9 @@ function format_size(int $size): string
*/
function lang(): string
{
$acceptLanguage = container()->get(MineRequest::class)->getHeaderLine('accept-language');
$acceptLanguage = container()
->get(RequestInterface::class)
->getHeaderLine('accept-language');
return str_replace('-', '_', ! empty($acceptLanguage) ? explode(',', $acceptLanguage)[0] : 'zh_CN');
}
}
Expand Down
36 changes: 36 additions & 0 deletions tests/Helpers/Ip2regionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);
/**
* This file is part of MineAdmin.
*
* @link https://www.mineadmin.com
* @document https://doc.mineadmin.com
* @contact [email protected]
* @license https://github.com/mineadmin/MineAdmin/blob/master/LICENSE
*/

namespace Mine\Tests\Helpers;

use Mine\Helper\Ip2region;
use PHPUnit\Framework\TestCase;

/**
* @internal
* @coversNothing
*/
class Ip2regionTest extends TestCase
{
public function testMake(): void
{
$ip2region = new Ip2region();
$this->assertInstanceOf(Ip2region::class, $ip2region);
}

public function testSearch()
{
$ip2region = new Ip2region();
$result = $ip2region->search('114.114.114.114');
$this->assertIsString($result);
}
}
28 changes: 24 additions & 4 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,27 @@
* @contact [email protected]
* @license https://github.com/mineadmin/MineAdmin/blob/master/LICENSE
*/
ini_set('display_errors', 'on');
ini_set('display_startup_errors', 'on');
error_reporting(E_ALL);
date_default_timezone_set('PRC');
use Hyperf\Config\Listener\RegisterPropertyHandlerListener;
use Hyperf\Di\Aop\AstVisitorRegistry;
use Hyperf\Di\Aop\PropertyHandlerVisitor;
use Hyperf\Di\Aop\ProxyCallVisitor;
use Hyperf\Di\Aop\RegisterInjectPropertyHandler;

// ini_set('display_errors', 'on');
// ini_set('display_startup_errors', 'on');
//
// error_reporting(E_ALL);

! defined('BASE_PATH') && define('BASE_PATH', dirname(__DIR__));
! defined('SWOOLE_HOOK_FLAGS') && define('SWOOLE_HOOK_FLAGS', SWOOLE_HOOK_ALL);

require_once BASE_PATH . '/vendor/autoload.php';

// Register AST visitors to the collector.
AstVisitorRegistry::insert(PropertyHandlerVisitor::class);
AstVisitorRegistry::insert(ProxyCallVisitor::class);

// Register Property Handler.
RegisterInjectPropertyHandler::register();

(new RegisterPropertyHandlerListener())->process(new stdClass());

0 comments on commit bb1b4b9

Please sign in to comment.