Skip to content

Commit c1b2e19

Browse files
authored
support topthink/think-orm 3.0.14
1 parent 28f7227 commit c1b2e19

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

src/Middleware.php

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Illuminate\Database\Events\QueryExecuted;
88
use Illuminate\Events\Dispatcher;
99
use Illuminate\Redis\Events\CommandExecuted;
10+
use support\Context;
1011
use Webman\Http\Request;
1112
use Webman\Http\Response;
1213
use Webman\MiddlewareInterface;
@@ -22,10 +23,6 @@
2223

2324
class Middleware implements MiddlewareInterface
2425
{
25-
/**
26-
* @var string
27-
*/
28-
public static $logs = '';
2926

3027
/**
3128
* @param Request $request
@@ -34,14 +31,14 @@ class Middleware implements MiddlewareInterface
3431
*/
3532
public function process(Request $request, callable $next): Response
3633
{
37-
static $initialized_db;
34+
static $initialized_db, $initialized_think_orm;
3835

3936
// 请求开始时间
4037
$start_time = microtime(true);
4138

4239
// 记录ip 请求等信息
4340
$logs = $request->getRealIp() . ' ' . $request->method() . ' ' . trim($request->fullUrl(), '/');
44-
static::$logs = '';
41+
Context::get()->webmanLogs = '';
4542

4643
// 清理think-orm的日志
4744
if (class_exists(ThinkDb::class, false) && class_exists(Mysql::class, false)) {
@@ -54,14 +51,24 @@ public function process(Request $request, callable $next): Response
5451
$this->initDbListen();
5552
}
5653

54+
// 初始化think-orm日志监听
55+
if (!$initialized_think_orm) {
56+
try {
57+
ThinkDb::setLog(function ($type, $log) {
58+
Context::get()->webmanLogs .= "[SQL]\t" . trim($log) . PHP_EOL;
59+
});
60+
} catch (Throwable $e) {}
61+
$initialized_think_orm = true;
62+
}
63+
5764
// 得到响应
5865
$response = $next($request);
5966
$time_diff = substr((microtime(true) - $start_time) * 1000, 0, 7);
6067
$logs .= " [{$time_diff}ms] [webman/log]" . PHP_EOL;
6168
if ($request->method() === 'POST') {
6269
$logs .= "[POST]\t" . var_export($request->post(), true) . PHP_EOL;
6370
}
64-
$logs .= static::$logs;
71+
$logs .= Context::get()->webmanLogs;
6572

6673
// think-orm如果被使用,则记录think-orm的日志
6774
if ($loaded_think_db = (class_exists(ThinkDb::class, false) && class_exists(Mysql::class, false))) {
@@ -158,7 +165,7 @@ protected function initDbListen()
158165
try {
159166
$log = vsprintf($sql, $query->bindings);
160167
} catch (\Throwable $e) {}
161-
static::$logs .= "[SQL]\t[connection:{$query->connectionName}] $log [{$query->time} ms]" . PHP_EOL;
168+
Context::get()->webmanLogs .= "[SQL]\t[connection:{$query->connectionName}] $log [{$query->time} ms]" . PHP_EOL;
162169
});
163170
$capsule->setEventDispatcher($dispatcher);
164171
} catch (\Throwable $e) {
@@ -191,7 +198,7 @@ protected function tryInitRedisListen(): array
191198
$item = implode('\', \'', $item);
192199
}
193200
}
194-
static::$logs .= "[Redis]\t[connection:{$command->connectionName}] Redis::{$command->command}('" . implode('\', \'', $command->parameters) . "') ({$command->time} ms)" . PHP_EOL;
201+
Context::get()->webmanLogs .= "[Redis]\t[connection:{$command->connectionName}] Redis::{$command->command}('" . implode('\', \'', $command->parameters) . "') ({$command->time} ms)" . PHP_EOL;
195202
});
196203
$listened_names[$name] = $name;
197204
$new_names[] = $name;

0 commit comments

Comments
 (0)