1313
1414use Psr \EventDispatcher \StoppableEventInterface ;
1515use Psr \Log \LoggerInterface ;
16+ use Symfony \Component \BrowserKit \Request ;
1617use Symfony \Component \EventDispatcher \Event ;
1718use Symfony \Component \EventDispatcher \EventDispatcherInterface ;
1819use Symfony \Component \EventDispatcher \EventSubscriberInterface ;
1920use Symfony \Component \EventDispatcher \LegacyEventDispatcherProxy ;
21+ use Symfony \Component \HttpFoundation \RequestStack ;
2022use Symfony \Component \Stopwatch \Stopwatch ;
2123use Symfony \Contracts \EventDispatcher \Event as ContractsEvent ;
2224
@@ -36,14 +38,17 @@ class TraceableEventDispatcher implements TraceableEventDispatcherInterface
3638 private $ dispatcher ;
3739 private $ wrappedListeners ;
3840 private $ orphanedEvents ;
41+ private $ requestStack ;
42+ private $ currentRequestHash = '' ;
3943
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 )
4145 {
4246 $ this ->dispatcher = LegacyEventDispatcherProxy::decorate ($ dispatcher );
4347 $ this ->stopwatch = $ stopwatch ;
4448 $ this ->logger = $ logger ;
4549 $ this ->wrappedListeners = [];
4650 $ this ->orphanedEvents = [];
51+ $ this ->requestStack = $ requestStack ;
4752 }
4853
4954 /**
@@ -133,6 +138,7 @@ public function dispatch($event/*, string $eventName = null*/)
133138 $ this ->callStack = new \SplObjectStorage ();
134139 }
135140
141+ $ currentRequestHash = $ this ->currentRequestHash = $ this ->requestStack && ($ request = $ this ->requestStack ->getCurrentRequest ()) ? spl_object_hash ($ request ) : '' ;
136142 $ eventName = 1 < \func_num_args () ? \func_get_arg (1 ) : null ;
137143
138144 if (\is_object ($ event )) {
@@ -168,6 +174,7 @@ public function dispatch($event/*, string $eventName = null*/)
168174 $ this ->afterDispatch ($ eventName , $ event );
169175 }
170176 } finally {
177+ $ this ->currentRequestHash = $ currentRequestHash ;
171178 $ this ->postProcess ($ eventName );
172179 }
173180
@@ -176,27 +183,33 @@ public function dispatch($event/*, string $eventName = null*/)
176183
177184 /**
178185 * {@inheritdoc}
186+ *
187+ * @param Request|null $request The request to get listeners for
179188 */
180- public function getCalledListeners ()
189+ public function getCalledListeners (/* Request $request = null */ )
181190 {
182191 if (null === $ this ->callStack ) {
183192 return [];
184193 }
185194
195+ $ hash = 1 <= \func_num_args () && null !== ($ request = \func_get_arg (0 )) ? spl_object_hash ($ request ) : null ;
186196 $ called = [];
187197 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+ }
191202 }
192203
193204 return $ called ;
194205 }
195206
196207 /**
197208 * {@inheritdoc}
209+ *
210+ * @param Request|null $request The request to get listeners for
198211 */
199- public function getNotCalledListeners ()
212+ public function getNotCalledListeners (/* Request $request = null */ )
200213 {
201214 try {
202215 $ allListeners = $ this ->getListeners ();
@@ -209,13 +222,15 @@ public function getNotCalledListeners()
209222 return [];
210223 }
211224
225+ $ hash = 1 <= \func_num_args () && null !== ($ request = \func_get_arg (0 )) ? spl_object_hash ($ request ) : null ;
212226 $ notCalled = [];
213227 foreach ($ allListeners as $ eventName => $ listeners ) {
214228 foreach ($ listeners as $ listener ) {
215229 $ called = false ;
216230 if (null !== $ this ->callStack ) {
217231 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 ) {
219234 $ called = true ;
220235
221236 break ;
@@ -237,15 +252,27 @@ public function getNotCalledListeners()
237252 return $ notCalled ;
238253 }
239254
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
241259 {
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 ));
243269 }
244270
245271 public function reset ()
246272 {
247273 $ this ->callStack = null ;
248274 $ this ->orphanedEvents = [];
275+ $ this ->currentRequestHash = '' ;
249276 }
250277
251278 /**
@@ -298,7 +325,7 @@ protected function postDispatch($eventName, Event $event)
298325 private function preProcess ($ eventName )
299326 {
300327 if (!$ this ->dispatcher ->hasListeners ($ eventName )) {
301- $ this ->orphanedEvents [] = $ eventName ;
328+ $ this ->orphanedEvents [$ this -> currentRequestHash ][ ] = $ eventName ;
302329
303330 return ;
304331 }
@@ -309,7 +336,7 @@ private function preProcess($eventName)
309336 $ this ->wrappedListeners [$ eventName ][] = $ wrappedListener ;
310337 $ this ->dispatcher ->removeListener ($ eventName , $ listener );
311338 $ this ->dispatcher ->addListener ($ eventName , $ wrappedListener , $ priority );
312- $ this ->callStack ->attach ($ wrappedListener , [$ eventName ]);
339+ $ this ->callStack ->attach ($ wrappedListener , [$ eventName, $ this -> currentRequestHash ]);
313340 }
314341 }
315342
@@ -334,10 +361,6 @@ private function postProcess($eventName)
334361 if (null !== $ this ->logger ) {
335362 $ this ->logger ->debug ('Notified event "{event}" to listener "{listener}". ' , $ context );
336363 }
337-
338- if (!isset ($ this ->called [$ eventName ])) {
339- $ this ->called [$ eventName ] = new \SplObjectStorage ();
340- }
341364 } else {
342365 $ this ->callStack ->detach ($ listener );
343366 }
0 commit comments