@@ -57,35 +57,8 @@ public function boot(Container $app): void
57
57
}
58
58
59
59
$ this ->booted = true ;
60
-
61
- // 记录 SQL
62
- $ app ['events ' ]->listen (QueryExecuted::class, function (QueryExecuted $ queryExecuted ) {
63
- if (
64
- isset ($ this ->queries [$ queryExecuted ->sql ]) ||
65
- $ this ->isExcludedSql ($ queryExecuted ->sql ) ||
66
- $ this ->isExcludedSql ($ sql = $ this ->transformToSql ($ queryExecuted ))
67
- ) {
68
- return ;
69
- }
70
-
71
- $ this ->queries [$ queryExecuted ->sql ] = [
72
- 'sql ' => $ sql ,
73
- 'time ' => $ this ->transformToHumanTime ($ queryExecuted ->time ),
74
- 'connection ' => $ queryExecuted ->connectionName ,
75
- 'driver ' => $ queryExecuted ->connection ->getDriverName (),
76
- 'backtraces ' => $ this ->getBacktraces (),
77
- ];
78
- });
79
-
80
- // 注册输出监听
81
- $ app ['events ' ]->listen (CommandFinished::class, function (CommandFinished $ commandFinished ) use ($ app ) {
82
- $ app ->make (OutputManager::class)->output ($ this ->getScores (), $ commandFinished );
83
- });
84
-
85
- // 注册输出中间件
86
- is_lumen ()
87
- ? $ app ->middleware (OutputSoarScoreMiddleware::class)
88
- : $ app ->make (Kernel::class)->pushMiddleware (OutputSoarScoreMiddleware::class);
60
+ $ this ->logQuery ($ app ['events ' ]);
61
+ $ this ->registerOutputMonitor ($ app );
89
62
}
90
63
91
64
public function isBooted (): bool
@@ -231,4 +204,42 @@ public function formatExplain(?array $explain): array
231
204
232
205
return $ explain ;
233
206
}
207
+
208
+ protected function logQuery (\Illuminate \Events \Dispatcher $ dispatcher ): void
209
+ {
210
+ // 记录 SQL
211
+ $ dispatcher ->listen (QueryExecuted::class, function (QueryExecuted $ queryExecuted ) {
212
+ if (
213
+ isset ($ this ->queries [$ queryExecuted ->sql ]) ||
214
+ $ this ->isExcludedSql ($ queryExecuted ->sql ) ||
215
+ $ this ->isExcludedSql ($ sql = $ this ->transformToSql ($ queryExecuted ))
216
+ ) {
217
+ return ;
218
+ }
219
+
220
+ $ this ->queries [$ queryExecuted ->sql ] = [
221
+ 'sql ' => $ sql ,
222
+ 'time ' => $ this ->transformToHumanTime ($ queryExecuted ->time ),
223
+ 'connection ' => $ queryExecuted ->connectionName ,
224
+ 'driver ' => $ queryExecuted ->connection ->getDriverName (),
225
+ 'backtraces ' => $ this ->getBacktraces (),
226
+ ];
227
+ });
228
+ }
229
+
230
+ /**
231
+ * @throws \Illuminate\Contracts\Container\BindingResolutionException
232
+ */
233
+ protected function registerOutputMonitor (Container $ app ): void
234
+ {
235
+ // 注册输出监听
236
+ $ app ['events ' ]->listen (CommandFinished::class, function (CommandFinished $ commandFinished ) use ($ app ) {
237
+ $ app ->make (OutputManager::class)->output ($ this ->getScores (), $ commandFinished );
238
+ });
239
+
240
+ // 注册输出中间件
241
+ is_lumen ()
242
+ ? $ app ->middleware (OutputSoarScoreMiddleware::class)
243
+ : $ app ->make (Kernel::class)->pushMiddleware (OutputSoarScoreMiddleware::class);
244
+ }
234
245
}
0 commit comments