@@ -32,6 +32,7 @@ class ContainerAwareEventManager extends EventManager
32
32
private $ subscribers ;
33
33
private $ initialized = [];
34
34
private $ initializedSubscribers = false ;
35
+ private $ initializedHashMapping = [];
35
36
private $ methods = [];
36
37
private $ container ;
37
38
@@ -138,6 +139,7 @@ public function addEventListener($events, $listener)
138
139
139
140
if (\is_string ($ listener )) {
140
141
unset($ this ->initialized [$ event ]);
142
+ unset($ this ->initializedHashMapping [$ event ][$ hash ]);
141
143
} else {
142
144
$ this ->methods [$ event ][$ hash ] = $ this ->getMethod ($ listener , $ event );
143
145
}
@@ -158,6 +160,11 @@ public function removeEventListener($events, $listener)
158
160
$ hash = $ this ->getHash ($ listener );
159
161
160
162
foreach ((array ) $ events as $ event ) {
163
+ if (isset ($ this ->initializedHashMapping [$ event ][$ hash ])) {
164
+ $ hash = $ this ->initializedHashMapping [$ event ][$ hash ];
165
+ unset($ this ->initializedHashMapping [$ event ][$ hash ]);
166
+ }
167
+
161
168
// Check if we actually have this listener associated
162
169
if (isset ($ this ->listeners [$ event ][$ hash ])) {
163
170
unset($ this ->listeners [$ event ][$ hash ]);
@@ -190,13 +197,25 @@ public function removeEventSubscriber(EventSubscriber $subscriber): void
190
197
private function initializeListeners (string $ eventName )
191
198
{
192
199
$ this ->initialized [$ eventName ] = true ;
200
+
201
+ // We'll refill the whole array in order to keep the same order
202
+ $ listeners = [];
193
203
foreach ($ this ->listeners [$ eventName ] as $ hash => $ listener ) {
194
204
if (\is_string ($ listener )) {
195
- $ this ->listeners [$ eventName ][$ hash ] = $ listener = $ this ->container ->get ($ listener );
205
+ $ listener = $ this ->container ->get ($ listener );
206
+ $ newHash = $ this ->getHash ($ listener );
207
+
208
+ $ this ->initializedHashMapping [$ eventName ][$ hash ] = $ newHash ;
196
209
197
- $ this ->methods [$ eventName ][$ hash ] = $ this ->getMethod ($ listener , $ eventName );
210
+ $ listeners [$ newHash ] = $ listener ;
211
+
212
+ $ this ->methods [$ eventName ][$ newHash ] = $ this ->getMethod ($ listener , $ eventName );
213
+ } else {
214
+ $ listeners [$ hash ] = $ listener ;
198
215
}
199
216
}
217
+
218
+ $ this ->listeners [$ eventName ] = $ listeners ;
200
219
}
201
220
202
221
private function initializeSubscribers ()
0 commit comments