13
13
14
14
use Psr \EventDispatcher \StoppableEventInterface ;
15
15
use Psr \Log \LoggerInterface ;
16
+ use Symfony \Component \BrowserKit \Request ;
16
17
use Symfony \Component \EventDispatcher \Event ;
17
18
use Symfony \Component \EventDispatcher \EventDispatcherInterface ;
18
19
use Symfony \Component \EventDispatcher \EventSubscriberInterface ;
19
20
use Symfony \Component \EventDispatcher \LegacyEventDispatcherProxy ;
21
+ use Symfony \Component \HttpFoundation \RequestStack ;
20
22
use Symfony \Component \Stopwatch \Stopwatch ;
21
23
use Symfony \Contracts \EventDispatcher \Event as ContractsEvent ;
22
24
@@ -36,14 +38,17 @@ class TraceableEventDispatcher implements TraceableEventDispatcherInterface
36
38
private $ dispatcher ;
37
39
private $ wrappedListeners ;
38
40
private $ orphanedEvents ;
41
+ private $ requestStack ;
42
+ private $ currentRequestHash = '' ;
39
43
40
- public function __construct (EventDispatcherInterface $ dispatcher , Stopwatch $ stopwatch , LoggerInterface $ logger = null )
44
+ public function __construct (EventDispatcherInterface $ dispatcher , Stopwatch $ stopwatch , LoggerInterface $ logger = null , RequestStack $ requestStack = null )
41
45
{
42
46
$ this ->dispatcher = LegacyEventDispatcherProxy::decorate ($ dispatcher );
43
47
$ this ->stopwatch = $ stopwatch ;
44
48
$ this ->logger = $ logger ;
45
49
$ this ->wrappedListeners = [];
46
50
$ this ->orphanedEvents = [];
51
+ $ this ->requestStack = $ requestStack ;
47
52
}
48
53
49
54
/**
@@ -133,6 +138,7 @@ public function dispatch($event/*, string $eventName = null*/)
133
138
$ this ->callStack = new \SplObjectStorage ();
134
139
}
135
140
141
+ $ currentRequestHash = $ this ->currentRequestHash = $ this ->requestStack && ($ request = $ this ->requestStack ->getCurrentRequest ()) ? spl_object_hash ($ request ) : '' ;
136
142
$ eventName = 1 < \func_num_args () ? \func_get_arg (1 ) : null ;
137
143
138
144
if (\is_object ($ event )) {
@@ -168,6 +174,7 @@ public function dispatch($event/*, string $eventName = null*/)
168
174
$ this ->afterDispatch ($ eventName , $ event );
169
175
}
170
176
} finally {
177
+ $ this ->currentRequestHash = $ currentRequestHash ;
171
178
$ this ->postProcess ($ eventName );
172
179
}
173
180
@@ -176,27 +183,33 @@ public function dispatch($event/*, string $eventName = null*/)
176
183
177
184
/**
178
185
* {@inheritdoc}
186
+ *
187
+ * @param Request|null $request The request to get listeners for
179
188
*/
180
- public function getCalledListeners ()
189
+ public function getCalledListeners (/* Request $request = null */ )
181
190
{
182
191
if (null === $ this ->callStack ) {
183
192
return [];
184
193
}
185
194
195
+ $ hash = 1 <= \func_num_args () && null !== ($ request = \func_get_arg (0 )) ? spl_object_hash ($ request ) : null ;
186
196
$ called = [];
187
197
foreach ($ this ->callStack as $ listener ) {
188
- list ($ eventName ) = $ this ->callStack ->getInfo ();
189
-
190
- $ called [] = $ listener ->getInfo ($ eventName );
198
+ list ($ eventName , $ requestHash ) = $ this ->callStack ->getInfo ();
199
+ if (null === $ hash || $ hash === $ requestHash ) {
200
+ $ called [] = $ listener ->getInfo ($ eventName );
201
+ }
191
202
}
192
203
193
204
return $ called ;
194
205
}
195
206
196
207
/**
197
208
* {@inheritdoc}
209
+ *
210
+ * @param Request|null $request The request to get listeners for
198
211
*/
199
- public function getNotCalledListeners ()
212
+ public function getNotCalledListeners (/* Request $request = null */ )
200
213
{
201
214
try {
202
215
$ allListeners = $ this ->getListeners ();
@@ -209,13 +222,15 @@ public function getNotCalledListeners()
209
222
return [];
210
223
}
211
224
225
+ $ hash = 1 <= \func_num_args () && null !== ($ request = \func_get_arg (0 )) ? spl_object_hash ($ request ) : null ;
212
226
$ notCalled = [];
213
227
foreach ($ allListeners as $ eventName => $ listeners ) {
214
228
foreach ($ listeners as $ listener ) {
215
229
$ called = false ;
216
230
if (null !== $ this ->callStack ) {
217
231
foreach ($ this ->callStack as $ calledListener ) {
218
- if ($ calledListener ->getWrappedListener () === $ listener ) {
232
+ list (, $ requestHash ) = $ this ->callStack ->getInfo ();
233
+ if ((null === $ hash || $ hash === $ requestHash ) && $ calledListener ->getWrappedListener () === $ listener ) {
219
234
$ called = true ;
220
235
221
236
break ;
@@ -237,15 +252,27 @@ public function getNotCalledListeners()
237
252
return $ notCalled ;
238
253
}
239
254
240
- public function getOrphanedEvents (): array
255
+ /**
256
+ * @param Request|null $request The request to get orphaned events for
257
+ */
258
+ public function getOrphanedEvents (/* Request $request = null */ ): array
241
259
{
242
- return $ this ->orphanedEvents ;
260
+ if (1 <= \func_num_args () && null !== $ request = \func_get_arg (0 )) {
261
+ return $ this ->orphanedEvents [spl_object_hash ($ request )] ?? [];
262
+ }
263
+
264
+ if (!$ this ->orphanedEvents ) {
265
+ return [];
266
+ }
267
+
268
+ return array_merge (...array_values ($ this ->orphanedEvents ));
243
269
}
244
270
245
271
public function reset ()
246
272
{
247
273
$ this ->callStack = null ;
248
274
$ this ->orphanedEvents = [];
275
+ $ this ->currentRequestHash = '' ;
249
276
}
250
277
251
278
/**
@@ -298,7 +325,7 @@ protected function postDispatch($eventName, Event $event)
298
325
private function preProcess ($ eventName )
299
326
{
300
327
if (!$ this ->dispatcher ->hasListeners ($ eventName )) {
301
- $ this ->orphanedEvents [] = $ eventName ;
328
+ $ this ->orphanedEvents [$ this -> currentRequestHash ][ ] = $ eventName ;
302
329
303
330
return ;
304
331
}
@@ -309,7 +336,7 @@ private function preProcess($eventName)
309
336
$ this ->wrappedListeners [$ eventName ][] = $ wrappedListener ;
310
337
$ this ->dispatcher ->removeListener ($ eventName , $ listener );
311
338
$ this ->dispatcher ->addListener ($ eventName , $ wrappedListener , $ priority );
312
- $ this ->callStack ->attach ($ wrappedListener , [$ eventName ]);
339
+ $ this ->callStack ->attach ($ wrappedListener , [$ eventName, $ this -> currentRequestHash ]);
313
340
}
314
341
}
315
342
@@ -334,10 +361,6 @@ private function postProcess($eventName)
334
361
if (null !== $ this ->logger ) {
335
362
$ this ->logger ->debug ('Notified event "{event}" to listener "{listener}". ' , $ context );
336
363
}
337
-
338
- if (!isset ($ this ->called [$ eventName ])) {
339
- $ this ->called [$ eventName ] = new \SplObjectStorage ();
340
- }
341
364
} else {
342
365
$ this ->callStack ->detach ($ listener );
343
366
}
0 commit comments