Skip to content

Commit f0dbc4b

Browse files
committed
Optimize boot
1 parent ad93f23 commit f0dbc4b

File tree

1 file changed

+40
-29
lines changed

1 file changed

+40
-29
lines changed

src/Bootstrapper.php

Lines changed: 40 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -57,35 +57,8 @@ public function boot(Container $app): void
5757
}
5858

5959
$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);
8962
}
9063

9164
public function isBooted(): bool
@@ -231,4 +204,42 @@ public function formatExplain(?array $explain): array
231204

232205
return $explain;
233206
}
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+
}
234245
}

0 commit comments

Comments
 (0)