Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 52e02bf

Browse filesBrowse files
Allow to remove LazyLoaded listeners by object
1 parent 25dc25c commit 52e02bf
Copy full SHA for 52e02bf

File tree

2 files changed

+53
-2
lines changed
Filter options

2 files changed

+53
-2
lines changed

‎src/Symfony/Bridge/Doctrine/ContainerAwareEventManager.php

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Doctrine/ContainerAwareEventManager.php
+25-2Lines changed: 25 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,15 @@ public function removeEventListener($events, $listener)
158160
$hash = $this->getHash($listener);
159161

160162
foreach ((array) $events as $event) {
163+
if (!isset($this->initialized[$event])) {
164+
$this->initializeListeners($event);
165+
}
166+
167+
if (isset($this->initializedHashMapping[$event][$hash])) {
168+
$hash = $this->initializedHashMapping[$event][$hash];
169+
unset($this->initializedHashMapping[$event][$hash]);
170+
}
171+
161172
// Check if we actually have this listener associated
162173
if (isset($this->listeners[$event][$hash])) {
163174
unset($this->listeners[$event][$hash]);
@@ -190,13 +201,25 @@ public function removeEventSubscriber(EventSubscriber $subscriber): void
190201
private function initializeListeners(string $eventName)
191202
{
192203
$this->initialized[$eventName] = true;
204+
205+
// We'll refill the whole array in order to keep the same order
206+
$listeners = [];
193207
foreach ($this->listeners[$eventName] as $hash => $listener) {
194208
if (\is_string($listener)) {
195-
$this->listeners[$eventName][$hash] = $listener = $this->container->get($listener);
209+
$listener = $this->container->get($listener);
210+
$newHash = $this->getHash($listener);
211+
212+
$this->initializedHashMapping[$eventName][$hash] = $newHash;
213+
214+
$listeners[$newHash] = $listener;
196215

197-
$this->methods[$eventName][$hash] = $this->getMethod($listener, $eventName);
216+
$this->methods[$eventName][$newHash] = $this->getMethod($listener, $eventName);
217+
} else {
218+
$listeners[$hash] = $listener;
198219
}
199220
}
221+
222+
$this->listeners[$eventName] = $listeners;
200223
}
201224

202225
private function initializeSubscribers()

‎src/Symfony/Bridge/Doctrine/Tests/ContainerAwareEventManagerTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Doctrine/Tests/ContainerAwareEventManagerTest.php
+28Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,34 @@ 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+
220+
public function testRemoveSameLazyListenerWithInstance()
221+
{
222+
$listener = new MyListener();
223+
$this->container->set('lazy', $listener);
224+
$this->container->set('lazy2', $listener);
225+
$this->evm->addEventListener('foo', 'lazy');
226+
$this->evm->addEventListener('foo', 'lazy2');
227+
228+
$this->evm->removeEventListener('foo', $listener);
229+
230+
$this->assertSame([], $this->evm->getListeners('foo'));
231+
}
232+
205233
public function testRemoveEventListenerAfterDispatchEvent()
206234
{
207235
$this->container->set('lazy', $listener1 = new MyListener());

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.