Skip to content

Commit e404e40

Browse files
VincentLangletfabpot
authored andcommitted
[DoctrineBridge] Bugfix - Allow to remove LazyLoaded listeners by object
1 parent 726042e commit e404e40

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

ContainerAwareEventManager.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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()

Tests/ContainerAwareEventManagerTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,21 @@ public function testRemoveEventListener()
202202
$this->assertSame([], $this->evm->getListeners('foo'));
203203
}
204204

205+
public function testRemoveAllEventListener()
206+
{
207+
$this->container->set('lazy', new MyListener());
208+
$this->evm->addEventListener('foo', 'lazy');
209+
$this->evm->addEventListener('foo', new MyListener());
210+
211+
foreach ($this->evm->getAllListeners() as $event => $listeners) {
212+
foreach ($listeners as $listener) {
213+
$this->evm->removeEventListener($event, $listener);
214+
}
215+
}
216+
217+
$this->assertSame([], $this->evm->getListeners('foo'));
218+
}
219+
205220
public function testRemoveEventListenerAfterDispatchEvent()
206221
{
207222
$this->container->set('lazy', $listener1 = new MyListener());

0 commit comments

Comments
 (0)