@@ -31,6 +31,7 @@ class ContainerAwareEventManager extends EventManager
31
31
private array $ listeners = [];
32
32
private array $ initialized = [];
33
33
private bool $ initializedSubscribers = false ;
34
+ private array $ initializedHashMapping = [];
34
35
private array $ methods = [];
35
36
private ContainerInterface $ container ;
36
37
@@ -119,6 +120,7 @@ public function addEventListener($events, $listener): void
119
120
120
121
if (\is_string ($ listener )) {
121
122
unset($ this ->initialized [$ event ]);
123
+ unset($ this ->initializedHashMapping [$ event ][$ hash ]);
122
124
} else {
123
125
$ this ->methods [$ event ][$ hash ] = $ this ->getMethod ($ listener , $ event );
124
126
}
@@ -134,6 +136,11 @@ public function removeEventListener($events, $listener): void
134
136
$ hash = $ this ->getHash ($ listener );
135
137
136
138
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
+
137
144
// Check if we actually have this listener associated
138
145
if (isset ($ this ->listeners [$ event ][$ hash ])) {
139
146
unset($ this ->listeners [$ event ][$ hash ]);
@@ -166,13 +173,25 @@ public function removeEventSubscriber(EventSubscriber $subscriber): void
166
173
private function initializeListeners (string $ eventName ): void
167
174
{
168
175
$ this ->initialized [$ eventName ] = true ;
176
+
177
+ // We'll refill the whole array in order to keep the same order
178
+ $ listeners = [];
169
179
foreach ($ this ->listeners [$ eventName ] as $ hash => $ listener ) {
170
180
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 );
172
183
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 ;
174
191
}
175
192
}
193
+
194
+ $ this ->listeners [$ eventName ] = $ listeners ;
176
195
}
177
196
178
197
private function initializeSubscribers (): void
0 commit comments