From 91e529a4c532b9e3d5c0ff2d14467414b3d01ff9 Mon Sep 17 00:00:00 2001
From: Death-Satan <2771717608@qq.com>
Date: Mon, 22 Jan 2024 11:28:28 +0800
Subject: [PATCH 1/6] fix: ip2region
---
.gitignore | 3 ++-
tests/Helpers/Ip2regionTest.php | 17 +++++++++++++++
tests/bootstrap.php | 38 +++++++++++++++++++++++++--------
3 files changed, 48 insertions(+), 10 deletions(-)
create mode 100644 tests/Helpers/Ip2regionTest.php
diff --git a/.gitignore b/.gitignore
index 48b60833..7cb10a6f 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,4 +1,5 @@
composer.lock
.idea
vendor
-*.cache
\ No newline at end of file
+*.cache
+runtime
\ No newline at end of file
diff --git a/tests/Helpers/Ip2regionTest.php b/tests/Helpers/Ip2regionTest.php
new file mode 100644
index 00000000..ed2bb06f
--- /dev/null
+++ b/tests/Helpers/Ip2regionTest.php
@@ -0,0 +1,17 @@
+get(StdoutLoggerInterface::class));
+ $this->assertInstanceOf(Ip2region::class,$ip2region);
+ }
+}
\ No newline at end of file
diff --git a/tests/bootstrap.php b/tests/bootstrap.php
index e5dc55a0..ee59c462 100644
--- a/tests/bootstrap.php
+++ b/tests/bootstrap.php
@@ -2,14 +2,34 @@
declare(strict_types=1);
/**
- * This file is part of MineAdmin.
+ * This file is part of Hyperf.
*
- * @link https://www.mineadmin.com
- * @document https://doc.mineadmin.com
- * @contact root@imoi.cn
- * @license https://github.com/mineadmin/MineAdmin/blob/master/LICENSE
+ * @link https://www.hyperf.io
+ * @document https://hyperf.wiki
+ * @contact group@hyperf.io
+ * @license https://github.com/hyperf/hyperf/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', __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());
\ No newline at end of file
From f2b54504aba9884de9f11e99c9fcb8c5a6e2a22a Mon Sep 17 00:00:00 2001
From: Death-Satan <2771717608@qq.com>
Date: Mon, 22 Jan 2024 11:41:07 +0800
Subject: [PATCH 2/6] feat: testing
---
phpunit.xml | 2 ++
src/mine-helpers/src/Ip2region.php | 21 ++++++++++++++++++---
tests/Helpers/Ip2regionTest.php | 11 +++++++++--
tests/bootstrap.php | 4 ++--
4 files changed, 31 insertions(+), 7 deletions(-)
diff --git a/phpunit.xml b/phpunit.xml
index 62ff8958..c7ca800a 100644
--- a/phpunit.xml
+++ b/phpunit.xml
@@ -8,6 +8,7 @@
./src/office
./src/gateway
./src/translatable
+ ./tests
diff --git a/src/mine-helpers/src/Ip2region.php b/src/mine-helpers/src/Ip2region.php
index c7c0f2a1..34b0744f 100644
--- a/src/mine-helpers/src/Ip2region.php
+++ b/src/mine-helpers/src/Ip2region.php
@@ -12,20 +12,35 @@
namespace Mine\Helper;
+use Composer\Autoload\ClassLoader;
use Hyperf\Contract\StdoutLoggerInterface;
use Hyperf\Support\Composer;
+use RuntimeException;
class Ip2region
{
protected \XdbSearcher $searcher;
+ 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.');
+ }
+
/**
* @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';
@@ -33,7 +48,7 @@ public function __construct(protected StdoutLoggerInterface $logger)
// 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 创建带完全基于内存的查询对象。
diff --git a/tests/Helpers/Ip2regionTest.php b/tests/Helpers/Ip2regionTest.php
index ed2bb06f..bcf29fda 100644
--- a/tests/Helpers/Ip2regionTest.php
+++ b/tests/Helpers/Ip2regionTest.php
@@ -7,11 +7,18 @@
use Hyperf\Testing\TestCase;
use Mine\Helper\Ip2region;
-class Ip2regionTest extends TestCase
+class Ip2regionTest extends \PHPUnit\Framework\TestCase
{
public function testMake(): void
{
- $ip2region = new Ip2region(ApplicationContext::getContainer()->get(StdoutLoggerInterface::class));
+ $ip2region = new Ip2region();
$this->assertInstanceOf(Ip2region::class,$ip2region);
}
+
+ public function testSearch()
+ {
+ $ip2region = new Ip2region();
+ $result = $ip2region->search('114.114.114.114');
+ $this->assertIsString($result);
+ }
}
\ No newline at end of file
diff --git a/tests/bootstrap.php b/tests/bootstrap.php
index ee59c462..81dfd81a 100644
--- a/tests/bootstrap.php
+++ b/tests/bootstrap.php
@@ -20,10 +20,10 @@
//
// error_reporting(E_ALL);
-! defined('BASE_PATH') && define('BASE_PATH', __DIR__);
+! 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';
+require_once BASE_PATH . '/vendor/autoload.php';
// Register AST visitors to the collector.
AstVisitorRegistry::insert(PropertyHandlerVisitor::class);
From 2639cb20ead228ff1262cdc96b87aaafcceb450b Mon Sep 17 00:00:00 2001
From: Death-Satan <2771717608@qq.com>
Date: Mon, 22 Jan 2024 11:47:43 +0800
Subject: [PATCH 3/6] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9Etoken=E8=BF=87?=
=?UTF-8?q?=E6=9C=9F=20exception?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
src/jwt-auth/src/Exception/TokenExpireException.php | 8 ++++++++
src/jwt-auth/src/Util/JWTUtil.php | 6 ++++--
2 files changed, 12 insertions(+), 2 deletions(-)
create mode 100644 src/jwt-auth/src/Exception/TokenExpireException.php
diff --git a/src/jwt-auth/src/Exception/TokenExpireException.php b/src/jwt-auth/src/Exception/TokenExpireException.php
new file mode 100644
index 00000000..cc084f71
--- /dev/null
+++ b/src/jwt-auth/src/Exception/TokenExpireException.php
@@ -0,0 +1,8 @@
+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']));
From cb6da7a4f6336846ba28f2e2609e5862b9b5d415 Mon Sep 17 00:00:00 2001
From: Death-Satan <2771717608@qq.com>
Date: Mon, 22 Jan 2024 11:48:44 +0800
Subject: [PATCH 4/6] style: code style
---
.../src/Exception/TokenExpireException.php | 15 +++++++---
src/mine-helpers/src/Ip2region.php | 28 +++++++++----------
tests/Helpers/Ip2regionTest.php | 21 ++++++++++----
tests/bootstrap.php | 12 ++++----
4 files changed, 46 insertions(+), 30 deletions(-)
diff --git a/src/jwt-auth/src/Exception/TokenExpireException.php b/src/jwt-auth/src/Exception/TokenExpireException.php
index cc084f71..da059816 100644
--- a/src/jwt-auth/src/Exception/TokenExpireException.php
+++ b/src/jwt-auth/src/Exception/TokenExpireException.php
@@ -1,8 +1,15 @@
assertInstanceOf(Ip2region::class,$ip2region);
+ $this->assertInstanceOf(Ip2region::class, $ip2region);
}
public function testSearch()
@@ -21,4 +32,4 @@ public function testSearch()
$result = $ip2region->search('114.114.114.114');
$this->assertIsString($result);
}
-}
\ No newline at end of file
+}
diff --git a/tests/bootstrap.php b/tests/bootstrap.php
index 81dfd81a..34ac0ca1 100644
--- a/tests/bootstrap.php
+++ b/tests/bootstrap.php
@@ -2,12 +2,12 @@
declare(strict_types=1);
/**
- * This file is part of Hyperf.
+ * This file is part of MineAdmin.
*
- * @link https://www.hyperf.io
- * @document https://hyperf.wiki
- * @contact group@hyperf.io
- * @license https://github.com/hyperf/hyperf/blob/master/LICENSE
+ * @link https://www.mineadmin.com
+ * @document https://doc.mineadmin.com
+ * @contact root@imoi.cn
+ * @license https://github.com/mineadmin/MineAdmin/blob/master/LICENSE
*/
use Hyperf\Config\Listener\RegisterPropertyHandlerListener;
use Hyperf\Di\Aop\AstVisitorRegistry;
@@ -32,4 +32,4 @@
// Register Property Handler.
RegisterInjectPropertyHandler::register();
-(new RegisterPropertyHandlerListener())->process(new \stdClass());
\ No newline at end of file
+(new RegisterPropertyHandlerListener())->process(new stdClass());
From 155948c9542976e30b8dc8ea259c258b316ae6da Mon Sep 17 00:00:00 2001
From: Death-Satan <2771717608@qq.com>
Date: Mon, 22 Jan 2024 11:54:25 +0800
Subject: [PATCH 5/6] style: code style
---
tests/Helpers/Ip2regionTest.php | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/tests/Helpers/Ip2regionTest.php b/tests/Helpers/Ip2regionTest.php
index ce62f11f..266b99b4 100644
--- a/tests/Helpers/Ip2regionTest.php
+++ b/tests/Helpers/Ip2regionTest.php
@@ -13,12 +13,13 @@
namespace Mine\Tests\Helpers;
use Mine\Helper\Ip2region;
+use PHPUnit\Framework\TestCase;
/**
* @internal
* @coversNothing
*/
-class Ip2regionTest extends \PHPUnit\Framework\TestCase
+class Ip2regionTest extends TestCase
{
public function testMake(): void
{
From 535c0b1d2b4d8c9fec9437e85b7b110f66986c23 Mon Sep 17 00:00:00 2001
From: Death-Satan <2771717608@qq.com>
Date: Mon, 22 Jan 2024 12:11:08 +0800
Subject: [PATCH 6/6] =?UTF-8?q?fix:=20=E8=8E=B7=E5=8F=96=E5=BD=93=E5=89=8D?=
=?UTF-8?q?=E8=AF=AD=E8=A8=80=E6=8D=A2=E6=88=90=20psr=20=E5=AF=B9=E8=B1=A1?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
src/mine-helpers/src/functions.php | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
diff --git a/src/mine-helpers/src/functions.php b/src/mine-helpers/src/functions.php
index 156b619f..13c859b4 100644
--- a/src/mine-helpers/src/functions.php
+++ b/src/mine-helpers/src/functions.php
@@ -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;
@@ -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');
}
}