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 b458745

Browse filesBrowse files
committed
bug #51061 [DoctrineBridge] Bugfix - Allow to remove LazyLoaded listeners by object (VincentLanglet)
This PR was squashed before being merged into the 5.4 branch. Discussion ---------- [DoctrineBridge] Bugfix - Allow to remove LazyLoaded listeners by object | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tickets | Fix #51057 | License | MIT | Doc PR | symfony/symfony-docs#... <!-- required for new features --> <!-- Replace this notice by a short README for your feature/bugfix. This will help reviewers and should be a good start for the documentation. Additionally (see https://symfony.com/releases): - Always add tests and ensure they pass. - Bug fixes must be submitted against the lowest maintained branch where they apply (lowest branches are regularly merged to upper ones so they get the fixes too). - Features and deprecations must be submitted against the latest branch. - For new features, provide some code snippets to help understand usage. - Changelog entry should follow https://symfony.com/doc/current/contributing/code/conventions.html#writing-a-changelog-entry - Never break backward compatibility (see https://symfony.com/bc). --> cc `@dmaicher` Commits ------- 8965c0c [DoctrineBridge] Bugfix - Allow to remove LazyLoaded listeners by object
2 parents 2290340 + 8965c0c commit b458745
Copy full SHA for b458745

File tree

2 files changed

+36
-2
lines changed
Filter options

2 files changed

+36
-2
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Doctrine/ContainerAwareEventManager.php
+21-2Lines 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()

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

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Doctrine/Tests/ContainerAwareEventManagerTest.php
+15Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.