33namespace WebmanTech \Debugbar \DataCollector ;
44
55use DebugBar \DataCollector \TimeDataCollector as DebugBarTimeDataCollector ;
6+ use Illuminate \Contracts \Events \Dispatcher ;
67use Illuminate \Database \Connection ;
78use Illuminate \Database \Events \QueryExecuted ;
89use Illuminate \Database \Events \TransactionBeginning ;
@@ -149,69 +150,91 @@ public function addListener(Connection $db): void
149150 return ;
150151 }
151152
152- $ event ->listen (function (QueryExecuted $ event ) use ($ db ): void {
153- $ connection = $ event ->connection ;
154- if (!$ this ->isEventConnectionEqual ($ db , $ connection )) {
155- return ;
156- }
157- $ bindings = $ event ->bindings ;
158- $ time = $ event ->time ;
159- $ query = $ event ->sql ;
160-
161- $ threshold = $ this ->config ['slow_threshold ' ];
162- if (!$ threshold || $ time > $ threshold ) {
163- if ($ collector = $ this ->getRequestThisCollector ()) {
164- $ collector ->addQuery ($ query , $ bindings , $ time , $ connection );
165- }
166- }
153+ $ this ->addEventListeners ($ event , $ db );
154+ }
155+
156+ public function addEventDispatcherListener (Dispatcher $ event ): void
157+ {
158+ $ this ->addEventListeners ($ event );
159+ }
160+
161+ private function addEventListeners (Dispatcher $ event , ?Connection $ connection = null ): void
162+ {
163+ $ event ->listen (function (QueryExecuted $ event ) use ($ connection ): void {
164+ $ this ->collectQueryEvent ($ event , $ connection );
167165 });
168166
169- $ event ->listen (TransactionBeginning::class, function (TransactionBeginning $ transaction ) use ($ db ): void {
170- if (!$ this ->isEventConnectionEqual ($ db , $ transaction ->connection )) {
171- return ;
172- }
173- if ($ collector = $ this ->getRequestThisCollector ()) {
174- $ collector ->collectTransactionEvent ('Begin Transaction ' , $ transaction ->connection );
175- }
167+ $ event ->listen (TransactionBeginning::class, function (TransactionBeginning $ transaction ) use ($ connection ): void {
168+ $ this ->collectTransactionConnectionEvent ('Begin Transaction ' , $ transaction ->connection , $ connection );
176169 });
177170
178- $ event ->listen (TransactionCommitted::class, function (TransactionCommitted $ transaction ) use ($ db ): void {
179- if (!$ this ->isEventConnectionEqual ($ db , $ transaction ->connection )) {
180- return ;
181- }
182- if ($ collector = $ this ->getRequestThisCollector ()) {
183- $ collector ->collectTransactionEvent ('Commit Transaction ' , $ transaction ->connection );
184- }
171+ $ event ->listen (TransactionCommitted::class, function (TransactionCommitted $ transaction ) use ($ connection ): void {
172+ $ this ->collectTransactionConnectionEvent ('Commit Transaction ' , $ transaction ->connection , $ connection );
185173 });
186174
187- $ event ->listen (TransactionRolledBack::class, function (TransactionRolledBack $ transaction ) use ($ db ): void {
188- if (!$ this ->isEventConnectionEqual ($ db , $ transaction ->connection )) {
189- return ;
190- }
191- if ($ collector = $ this ->getRequestThisCollector ()) {
192- $ collector ->collectTransactionEvent ('Rollback Transaction ' , $ transaction ->connection );
193- }
175+ $ event ->listen (TransactionRolledBack::class, function (TransactionRolledBack $ transaction ) use ($ connection ): void {
176+ $ this ->collectTransactionConnectionEvent ('Rollback Transaction ' , $ transaction ->connection , $ connection );
194177 });
195178
196- $ event ->listen ('connection.*.beganTransaction ' , function ($ event , $ params ): void {
197- if ($ collector = $ this ->getRequestThisCollector ()) {
198- $ collector ->collectTransactionEvent ('Begin Transaction ' , $ params [0 ]);
199- }
179+ $ event ->listen ('connection.*.beganTransaction ' , function ($ event , $ params ) use ($ connection ): void {
180+ $ this ->collectWildcardTransactionEvent ('Begin Transaction ' , $ params , $ connection );
200181 });
201182
202- $ event ->listen ('connection.*.committed ' , function ($ event , $ params ): void {
203- if ($ collector = $ this ->getRequestThisCollector ()) {
204- $ collector ->collectTransactionEvent ('Commit Transaction ' , $ params [0 ]);
205- }
183+ $ event ->listen ('connection.*.committed ' , function ($ event , $ params ) use ($ connection ): void {
184+ $ this ->collectWildcardTransactionEvent ('Commit Transaction ' , $ params , $ connection );
206185 });
207186
208- $ event ->listen ('connection.*.rollingBack ' , function ($ event , $ params ): void {
209- if ($ collector = $ this ->getRequestThisCollector ()) {
210- $ collector ->collectTransactionEvent ('Rollback Transaction ' , $ params [0 ]);
211- }
187+ $ event ->listen ('connection.*.rollingBack ' , function ($ event , $ params ) use ($ connection ): void {
188+ $ this ->collectWildcardTransactionEvent ('Rollback Transaction ' , $ params , $ connection );
212189 });
213190 }
214191
192+ private function collectQueryEvent (QueryExecuted $ event , ?Connection $ expectedConnection = null ): void
193+ {
194+ if (!$ this ->shouldCollectConnection ($ event ->connection , $ expectedConnection )) {
195+ return ;
196+ }
197+
198+ $ threshold = $ this ->config ['slow_threshold ' ];
199+ if ($ threshold && $ event ->time <= $ threshold ) {
200+ return ;
201+ }
202+
203+ if ($ collector = $ this ->getRequestThisCollector ()) {
204+ $ collector ->addQuery ($ event ->sql , $ event ->bindings , $ event ->time , $ event ->connection );
205+ }
206+ }
207+
208+ private function collectTransactionConnectionEvent (
209+ string $ label ,
210+ Connection $ eventConnection ,
211+ ?Connection $ expectedConnection = null ,
212+ ): void {
213+ if (!$ this ->shouldCollectConnection ($ eventConnection , $ expectedConnection )) {
214+ return ;
215+ }
216+
217+ if ($ collector = $ this ->getRequestThisCollector ()) {
218+ $ collector ->collectTransactionEvent ($ label , $ eventConnection );
219+ }
220+ }
221+
222+ /**
223+ * @param array<mixed> $params
224+ */
225+ private function collectWildcardTransactionEvent (
226+ string $ label ,
227+ array $ params ,
228+ ?Connection $ expectedConnection = null ,
229+ ): void {
230+ $ eventConnection = $ params [0 ] ?? null ;
231+ if (!$ eventConnection instanceof Connection) {
232+ return ;
233+ }
234+
235+ $ this ->collectTransactionConnectionEvent ($ label , $ eventConnection , $ expectedConnection );
236+ }
237+
215238 /**
216239 * 获取 request 下每次新的当前 collector 对象
217240 * @return static|null
@@ -238,4 +261,13 @@ protected function isEventConnectionEqual(Connection $connection, Connection $ev
238261 {
239262 return $ connection ->getName () === $ eventConnection ->getName ();
240263 }
264+
265+ private function shouldCollectConnection (Connection $ eventConnection , ?Connection $ expectedConnection = null ): bool
266+ {
267+ if ($ expectedConnection === null ) {
268+ return true ;
269+ }
270+
271+ return $ this ->isEventConnectionEqual ($ expectedConnection , $ eventConnection );
272+ }
241273}
0 commit comments