@@ -32,6 +32,7 @@ class ContainerAwareEventManager extends EventManager
3232 private $ subscribers ;
3333 private $ initialized = [];
3434 private $ initializedSubscribers = false ;
35+ private $ initializedHashMapping = [];
3536 private $ methods = [];
3637 private $ container ;
3738
@@ -138,6 +139,7 @@ public function addEventListener($events, $listener)
138139
139140 if (\is_string ($ listener )) {
140141 unset($ this ->initialized [$ event ]);
142+ unset($ this ->initializedHashMapping [$ event ][$ hash ]);
141143 } else {
142144 $ this ->methods [$ event ][$ hash ] = $ this ->getMethod ($ listener , $ event );
143145 }
@@ -158,6 +160,11 @@ public function removeEventListener($events, $listener)
158160 $ hash = $ this ->getHash ($ listener );
159161
160162 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+
161168 // Check if we actually have this listener associated
162169 if (isset ($ this ->listeners [$ event ][$ hash ])) {
163170 unset($ this ->listeners [$ event ][$ hash ]);
@@ -190,13 +197,25 @@ public function removeEventSubscriber(EventSubscriber $subscriber): void
190197 private function initializeListeners (string $ eventName )
191198 {
192199 $ this ->initialized [$ eventName ] = true ;
200+
201+ // We'll refill the whole array in order to keep the same order
202+ $ listeners = [];
193203 foreach ($ this ->listeners [$ eventName ] as $ hash => $ listener ) {
194204 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 ;
196209
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 ;
198215 }
199216 }
217+
218+ $ this ->listeners [$ eventName ] = $ listeners ;
200219 }
201220
202221 private function initializeSubscribers ()
0 commit comments