Files
swiftadmin/vendor/webman/think-orm/src/ThinkOrm.php

81 lines
2.8 KiB
PHP
Raw Normal View History

2022-08-19 19:48:37 +08:00
<?php
namespace Webman\ThinkOrm;
use Webman\Bootstrap;
use Workerman\Timer;
2022-11-28 19:11:12 +08:00
use Throwable;
2022-08-19 19:48:37 +08:00
use think\Paginator;
use think\facade\Db;
2022-11-28 19:11:12 +08:00
use think\DbManager;
use think\Container;
2022-08-19 19:48:37 +08:00
class ThinkOrm implements Bootstrap
{
// 进程启动时调用
public static function start($worker)
{
$config = config('thinkorm');
// 配置
Db::setConfig($config);
// 维持mysql心跳
if ($worker) {
2022-11-28 19:11:12 +08:00
if (class_exists(Container::class, false)) {
$manager_instance = Container::getInstance()->make(DbManager::class);
} else {
$reflect = new \ReflectionClass(Db::class);
$property = $reflect->getProperty('instance');
$property->setAccessible(true);
$manager_instance = $property->getValue();
}
Timer::add(55, function () use ($manager_instance) {
2023-04-25 20:11:49 +08:00
$instances = [];
if (method_exists($manager_instance, 'getInstance')) {
$instances = $manager_instance->getInstance();
} else {
$reflect = new \ReflectionClass($manager_instance);
$property = $reflect->getProperty('instance');
$property->setAccessible(true);
$instances = $property->getValue($manager_instance);
}
2022-11-28 19:11:12 +08:00
foreach ($instances as $connection) {
/* @var \think\db\connector\Mysql $connection */
if ($connection->getConfig('type') == 'mysql') {
2022-08-19 19:48:37 +08:00
try {
2022-11-28 19:11:12 +08:00
$connection->query('select 1');
2022-08-19 19:48:37 +08:00
} catch (Throwable $e) {}
}
}
Db::getDbLog(true);
});
}
2022-11-28 19:11:12 +08:00
// 自定义分页组件类
$bootstrap = $config['connections'][$config['default']]['bootstrap'] ?? false;
if($bootstrap && class_exists($bootstrap)){
Paginator::maker(function ($items, $listRows, $currentPage, $total, $simple, $options) use ($bootstrap){
return (new \ReflectionClass($bootstrap))->newInstanceArgs(func_get_args());
});
}
2022-08-19 19:48:37 +08:00
Paginator::currentPageResolver(function ($pageName = 'page') {
2022-11-28 19:11:12 +08:00
$request = request();
if (!$request) {
return 1;
}
$page = $request->input($pageName, 1);
2022-08-19 19:48:37 +08:00
if (filter_var($page, FILTER_VALIDATE_INT) !== false && (int)$page >= 1) {
return (int)$page;
}
return 1;
});
2022-11-28 19:11:12 +08:00
// 设置分页url中域名与参数之间的path字符串
Paginator::currentPathResolver(function (){
$request = request();
return $request ? $request->path() : '/';
});
2022-08-19 19:48:37 +08:00
}
}