Skip to content

Commit 43afe80

Browse files
authored
Merge pull request #14 from ichynul/main
使用容器时通过bind替换类,让`think\facade\Db`可正常使用
2 parents d84fad9 + b207dad commit 43afe80

4 files changed

Lines changed: 13 additions & 6 deletions

File tree

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"license": "MIT",
55
"require": {
66
"php": ">=8.1",
7+
"topthink/think-container": "^2.0|^3.0",
78
"topthink/think-orm": "^2.0.53 || ^3.0.0 || ^4.0.0 || dev-master",
89
"workerman/webman-framework": "^2.1 || dev-master"
910
},

src/DbManager.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace Webman\ThinkOrm;
66

7-
use Illuminate\Events\Dispatcher;
87
use Webman\Context;
98
use Workerman\Coroutine\Pool;
109
use Throwable;

src/ThinkOrm.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
namespace Webman\ThinkOrm;
44

5+
use think\Container;
56
use think\Paginator;
67
use support\think\Db;
78
use Webman\Bootstrap;
9+
use support\think\Cache;
810

911
class ThinkOrm implements Bootstrap
1012
{
@@ -28,13 +30,15 @@ public static function start($worker): void
2830
return;
2931
}
3032

31-
if (!class_exists(\think\facade\Db::class, false)) {
32-
class_alias(\support\think\Db::class, \think\facade\Db::class);
33-
}
33+
Container::getInstance()->bind('think\DbManager', DbManager::class);
3434

3535
// 配置
3636
Db::setConfig($config);
3737

38+
if (class_exists(Cache::class)) {
39+
Db::setCache(Cache::store());
40+
}
41+
3842
Paginator::currentPageResolver(function ($pageName = 'page') {
3943
$request = request();
4044
if (!$request) {
@@ -48,7 +52,7 @@ class_alias(\support\think\Db::class, \think\facade\Db::class);
4852
});
4953

5054
// 设置分页url中域名与参数之间的path字符串
51-
Paginator::currentPathResolver(function (){
55+
Paginator::currentPathResolver(function () {
5256
$request = request();
5357
return $request ? $request->path() : '/';
5458
});

src/support/think/Db.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@
1313
namespace support\think;
1414

1515
use think\db\BaseQuery;
16-
use think\Event;
1716
use think\Facade;
1817
use Webman\ThinkOrm\DbManager;
1918

2019
/**
2120
* Class Db
21+
*
22+
* @mixin DbManager
23+
*
2224
* 数据库操作类
2325
*
2426
* @method static BaseQuery name(string $name) 指定当前数据表名(不含前缀)
@@ -33,6 +35,7 @@
3335
* @method static BaseQuery connect(string|null $name = null, bool $force = false) 创建/切换数据库连接查询
3436
* @method static void setConfig($config) 设置配置对象
3537
* @method static mixed getConfig(string $name = '', $default = null) 获取配置参数
38+
* @method static void setCache($config) 设置缓存对象
3639
* @method static void setEvent(Event $event) 设置Event对象
3740
* @method static void event(string $event, callable $callback) 注册回调方法
3841
* @method static mixed trigger(string $event, $params = null, bool $once = false) 触发事件

0 commit comments

Comments
 (0)