@@ -29,7 +29,7 @@ class TraceableEventDispatcher implements TraceableEventDispatcherInterface
29
29
protected $ logger ;
30
30
protected $ stopwatch ;
31
31
32
- private $ called ;
32
+ private $ callStack ;
33
33
private $ dispatcher ;
34
34
private $ wrappedListeners ;
35
35
@@ -38,7 +38,6 @@ public function __construct(EventDispatcherInterface $dispatcher, Stopwatch $sto
38
38
$ this ->dispatcher = $ dispatcher ;
39
39
$ this ->stopwatch = $ stopwatch ;
40
40
$ this ->logger = $ logger ;
41
- $ this ->called = array ();
42
41
$ this ->wrappedListeners = array ();
43
42
}
44
43
@@ -123,6 +122,10 @@ public function hasListeners($eventName = null)
123
122
*/
124
123
public function dispatch ($ eventName , Event $ event = null )
125
124
{
125
+ if (null === $ this ->callStack ) {
126
+ $ this ->callStack = new \SplObjectStorage ();
127
+ }
128
+
126
129
if (null === $ event ) {
127
130
$ event = new Event ();
128
131
}
@@ -158,11 +161,15 @@ public function dispatch($eventName, Event $event = null)
158
161
*/
159
162
public function getCalledListeners ()
160
163
{
164
+ if (null === $ this ->callStack ) {
165
+ return array ();
166
+ }
167
+
161
168
$ called = array ();
162
- foreach ($ this ->called as $ eventName => $ listeners ) {
163
- foreach ( $ listeners as $ listener ) {
164
- $ called [ $ eventName . ' . ' . $ listener -> getPretty ()] = $ listener -> getInfo ( $ eventName );
165
- }
169
+ foreach ($ this ->callStack as $ listener ) {
170
+ list ( $ eventName ) = $ this -> callStack -> getInfo ();
171
+
172
+ $ called [] = $ listener -> getInfo ( $ eventName );
166
173
}
167
174
168
175
return $ called ;
@@ -188,9 +195,9 @@ public function getNotCalledListeners()
188
195
foreach ($ allListeners as $ eventName => $ listeners ) {
189
196
foreach ($ listeners as $ listener ) {
190
197
$ called = false ;
191
- if (isset ( $ this ->called [ $ eventName ]) ) {
192
- foreach ($ this ->called [ $ eventName ] as $ l ) {
193
- if ($ l ->getWrappedListener () === $ listener ) {
198
+ if (null !== $ this ->callStack ) {
199
+ foreach ($ this ->callStack as $ calledListener ) {
200
+ if ($ calledListener ->getWrappedListener () === $ listener ) {
194
201
$ called = true ;
195
202
196
203
break ;
@@ -202,19 +209,19 @@ public function getNotCalledListeners()
202
209
if (!$ listener instanceof WrappedListener) {
203
210
$ listener = new WrappedListener ($ listener , null , $ this ->stopwatch , $ this );
204
211
}
205
- $ notCalled [$ eventName . ' . ' . $ listener -> getPretty () ] = $ listener ->getInfo ($ eventName );
212
+ $ notCalled [] = $ listener ->getInfo ($ eventName );
206
213
}
207
214
}
208
215
}
209
216
210
- uasort ($ notCalled , array ($ this , 'sortListenersByPriority ' ));
217
+ uasort ($ notCalled , array ($ this , 'sortNotCalledListeners ' ));
211
218
212
219
return $ notCalled ;
213
220
}
214
221
215
222
public function reset ()
216
223
{
217
- $ this ->called = array ();
224
+ $ this ->callStack = array ();
218
225
}
219
226
220
227
/**
@@ -258,6 +265,7 @@ private function preProcess($eventName)
258
265
$ this ->wrappedListeners [$ eventName ][] = $ wrappedListener ;
259
266
$ this ->dispatcher ->removeListener ($ eventName , $ listener );
260
267
$ this ->dispatcher ->addListener ($ eventName , $ wrappedListener , $ priority );
268
+ $ this ->callStack ->attach ($ wrappedListener , array ($ eventName ));
261
269
}
262
270
}
263
271
@@ -286,8 +294,8 @@ private function postProcess($eventName)
286
294
if (!isset ($ this ->called [$ eventName ])) {
287
295
$ this ->called [$ eventName ] = new \SplObjectStorage ();
288
296
}
289
-
290
- $ this ->called [ $ eventName ]-> attach ($ listener );
297
+ } else {
298
+ $ this ->callStack -> detach ($ listener );
291
299
}
292
300
293
301
if (null !== $ this ->logger && $ skipped ) {
@@ -304,8 +312,12 @@ private function postProcess($eventName)
304
312
}
305
313
}
306
314
307
- private function sortListenersByPriority ( $ a , $ b )
315
+ private function sortNotCalledListeners ( array $ a , array $ b )
308
316
{
317
+ if (0 !== $ cmp = strcmp ($ a ['event ' ], $ b ['event ' ])) {
318
+ return $ cmp ;
319
+ }
320
+
309
321
if (\is_int ($ a ['priority ' ]) && !\is_int ($ b ['priority ' ])) {
310
322
return 1 ;
311
323
}
0 commit comments