@@ -14,15 +14,29 @@ class QueryDetector
14
14
{
15
15
/** @var Collection */
16
16
private $ queries ;
17
+ /**
18
+ * @var bool
19
+ */
20
+ private $ booted = false ;
17
21
18
- public function __construct ()
22
+ private function resetQueries ()
19
23
{
20
24
$ this ->queries = Collection::make ();
21
25
}
22
26
27
+ public function __construct ()
28
+ {
29
+ $ this ->resetQueries ();
30
+ }
31
+
23
32
public function boot ()
24
33
{
25
- DB ::listen (function ($ query ) {
34
+ if ($ this ->booted ) {
35
+ $ this ->resetQueries ();
36
+ return ;
37
+ }
38
+
39
+ DB ::listen (function ($ query ) {
26
40
$ backtrace = collect (debug_backtrace (DEBUG_BACKTRACE_PROVIDE_OBJECT , 50 ));
27
41
28
42
$ this ->logQuery ($ query , $ backtrace );
@@ -32,6 +46,8 @@ public function boot()
32
46
app ()->singleton ($ outputType );
33
47
app ($ outputType )->boot ();
34
48
}
49
+
50
+ $ this ->booted = true ;
35
51
}
36
52
37
53
public function isEnabled (): bool
@@ -52,13 +68,13 @@ public function logQuery($query, Collection $backtrace)
52
68
});
53
69
54
70
// The query is coming from an Eloquent model
55
- if (! is_null ($ modelTrace )) {
71
+ if (!is_null ($ modelTrace )) {
56
72
/*
57
73
* Relations get resolved by either calling the "getRelationValue" method on the model,
58
74
* or if the class itself is a Relation.
59
75
*/
60
76
$ 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;
62
78
});
63
79
64
80
// We try to access a relation
@@ -81,8 +97,8 @@ public function logQuery($query, Collection $backtrace)
81
97
82
98
$ key = md5 ($ query ->sql . $ model . $ relationName . $ sources [0 ]->name . $ sources [0 ]->line );
83
99
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 );
86
102
87
103
$ this ->queries [$ key ] = [
88
104
'count ' => ++$ count ,
@@ -110,7 +126,7 @@ protected function findSource($stack)
110
126
111
127
public function parseTrace ($ index , array $ trace )
112
128
{
113
- $ frame = (object ) [
129
+ $ frame = (object )[
114
130
'index ' => $ index ,
115
131
'name ' => null ,
116
132
'line ' => isset ($ trace ['line ' ]) ? $ trace ['line ' ] : '? ' ,
@@ -195,7 +211,7 @@ protected function getOutputTypes()
195
211
{
196
212
$ outputTypes = config ('querydetector.output ' );
197
213
198
- if (! is_array ($ outputTypes )) {
214
+ if (!is_array ($ outputTypes )) {
199
215
$ outputTypes = [$ outputTypes ];
200
216
}
201
217
0 commit comments