@@ -14,15 +14,29 @@ class QueryDetector
1414{
1515 /** @var Collection */
1616 private $ queries ;
17+ /**
18+ * @var bool
19+ */
20+ private $ booted = false ;
1721
18- public function __construct ()
22+ private function resetQueries ()
1923 {
2024 $ this ->queries = Collection::make ();
2125 }
2226
27+ public function __construct ()
28+ {
29+ $ this ->resetQueries ();
30+ }
31+
2332 public function boot ()
2433 {
25- DB ::listen (function ($ query ) {
34+ if ($ this ->booted ) {
35+ $ this ->resetQueries ();
36+ return ;
37+ }
38+
39+ DB ::listen (function ($ query ) {
2640 $ backtrace = collect (debug_backtrace (DEBUG_BACKTRACE_PROVIDE_OBJECT , 50 ));
2741
2842 $ this ->logQuery ($ query , $ backtrace );
@@ -32,6 +46,8 @@ public function boot()
3246 app ()->singleton ($ outputType );
3347 app ($ outputType )->boot ();
3448 }
49+
50+ $ this ->booted = true ;
3551 }
3652
3753 public function isEnabled (): bool
@@ -52,13 +68,13 @@ public function logQuery($query, Collection $backtrace)
5268 });
5369
5470 // The query is coming from an Eloquent model
55- if (! is_null ($ modelTrace )) {
71+ if (!is_null ($ modelTrace )) {
5672 /*
5773 * Relations get resolved by either calling the "getRelationValue" method on the model,
5874 * or if the class itself is a Relation.
5975 */
6076 $ relation = $ backtrace ->first (function ($ trace ) {
61- return Arr::get ($ trace , 'function ' ) === 'getRelationValue ' || Arr::get ($ trace , 'class ' ) === Relation::class ;
77+ return Arr::get ($ trace , 'function ' ) === 'getRelationValue ' || Arr::get ($ trace , 'class ' ) === Relation::class;
6278 });
6379
6480 // We try to access a relation
@@ -81,8 +97,8 @@ public function logQuery($query, Collection $backtrace)
8197
8298 $ key = md5 ($ query ->sql . $ model . $ relationName . $ sources [0 ]->name . $ sources [0 ]->line );
8399
84- $ count = Arr::get ($ this ->queries , $ key. '.count ' , 0 );
85- $ time = Arr::get ($ this ->queries , $ key. '.time ' , 0 );
100+ $ count = Arr::get ($ this ->queries , $ key . '.count ' , 0 );
101+ $ time = Arr::get ($ this ->queries , $ key . '.time ' , 0 );
86102
87103 $ this ->queries [$ key ] = [
88104 'count ' => ++$ count ,
@@ -110,7 +126,7 @@ protected function findSource($stack)
110126
111127 public function parseTrace ($ index , array $ trace )
112128 {
113- $ frame = (object ) [
129+ $ frame = (object )[
114130 'index ' => $ index ,
115131 'name ' => null ,
116132 'line ' => isset ($ trace ['line ' ]) ? $ trace ['line ' ] : '? ' ,
@@ -195,7 +211,7 @@ protected function getOutputTypes()
195211 {
196212 $ outputTypes = config ('querydetector.output ' );
197213
198- if (! is_array ($ outputTypes )) {
214+ if (!is_array ($ outputTypes )) {
199215 $ outputTypes = [$ outputTypes ];
200216 }
201217
0 commit comments