@@ -31,6 +31,7 @@ class ContainerAwareEventManager extends EventManager
3131 private array $ listeners = [];
3232 private array $ initialized = [];
3333 private bool $ initializedSubscribers = false ;
34+ private array $ initializedHashMapping = [];
3435 private array $ methods = [];
3536 private ContainerInterface $ container ;
3637
@@ -119,6 +120,7 @@ public function addEventListener($events, $listener): void
119120
120121 if (\is_string ($ listener )) {
121122 unset($ this ->initialized [$ event ]);
123+ unset($ this ->initializedHashMapping [$ event ][$ hash ]);
122124 } else {
123125 $ this ->methods [$ event ][$ hash ] = $ this ->getMethod ($ listener , $ event );
124126 }
@@ -134,6 +136,11 @@ public function removeEventListener($events, $listener): void
134136 $ hash = $ this ->getHash ($ listener );
135137
136138 foreach ((array ) $ events as $ event ) {
139+ if (isset ($ this ->initializedHashMapping [$ event ][$ hash ])) {
140+ $ hash = $ this ->initializedHashMapping [$ event ][$ hash ];
141+ unset($ this ->initializedHashMapping [$ event ][$ hash ]);
142+ }
143+
137144 // Check if we actually have this listener associated
138145 if (isset ($ this ->listeners [$ event ][$ hash ])) {
139146 unset($ this ->listeners [$ event ][$ hash ]);
@@ -166,13 +173,25 @@ public function removeEventSubscriber(EventSubscriber $subscriber): void
166173 private function initializeListeners (string $ eventName ): void
167174 {
168175 $ this ->initialized [$ eventName ] = true ;
176+
177+ // We'll refill the whole array in order to keep the same order
178+ $ listeners = [];
169179 foreach ($ this ->listeners [$ eventName ] as $ hash => $ listener ) {
170180 if (\is_string ($ listener )) {
171- $ this ->listeners [$ eventName ][$ hash ] = $ listener = $ this ->container ->get ($ listener );
181+ $ listener = $ this ->container ->get ($ listener );
182+ $ newHash = $ this ->getHash ($ listener );
172183
173- $ this ->methods [$ eventName ][$ hash ] = $ this ->getMethod ($ listener , $ eventName );
184+ $ this ->initializedHashMapping [$ eventName ][$ hash ] = $ newHash ;
185+
186+ $ listeners [$ newHash ] = $ listener ;
187+
188+ $ this ->methods [$ eventName ][$ newHash ] = $ this ->getMethod ($ listener , $ eventName );
189+ } else {
190+ $ listeners [$ hash ] = $ listener ;
174191 }
175192 }
193+
194+ $ this ->listeners [$ eventName ] = $ listeners ;
176195 }
177196
178197 private function initializeSubscribers (): void
0 commit comments