Skip to content

Commit fd81067

Browse files
Merge branch '6.4' into 7.0
* 6.4: (31 commits) [HttpKernel] Strip exception file paths from log messages [Validator] Add ability to validate time without seconds [Process] Fix test case [Process] Support finding executables independently of open_basedir [Mime] Update mimetypes Add some PHPDoc [Workflow] Add a profiler [Form] Removing self-closing slash from `<input>` [FrameworkBundle][Serializer] Add TranslatableNormalizer [Uid] Fix example [HttpKernel] Add `reset()` implementation in DataCollector [Uid] Add more PHP doc to "export" functions [HttpKernel] RequestPayloadValueResolver Add support for custom http status code [Serializer] Add support for seld/jsonlint in order to enhance error messages [Workflow] fix MermaidDumper when place contains special char [Translation] Phrase translation provider [Workflow] Add support for storing the marking in a property [Crawler] Fix regression where cdata nodes will return empty string [Scheduler] make `ScheduledStamp` "send-able" [Serializer] Groups annotation/attribute on class ...
2 parents dcd0d7a + 5ce5bbd commit fd81067

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
@@ -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

@@ -114,6 +115,7 @@ public function addEventListener(string|array $events, object|string $listener):
114115

115116
if (\is_string($listener)) {
116117
unset($this->initialized[$event]);
118+
unset($this->initializedHashMapping[$event][$hash]);
117119
} else {
118120
$this->methods[$event][$hash] = $this->getMethod($listener, $event);
119121
}
@@ -129,6 +131,11 @@ public function removeEventListener(string|array $events, object|string $listene
129131
$hash = $this->getHash($listener);
130132

131133
foreach ((array) $events as $event) {
134+
if (isset($this->initializedHashMapping[$event][$hash])) {
135+
$hash = $this->initializedHashMapping[$event][$hash];
136+
unset($this->initializedHashMapping[$event][$hash]);
137+
}
138+
132139
// Check if we actually have this listener associated
133140
if (isset($this->listeners[$event][$hash])) {
134141
unset($this->listeners[$event][$hash]);
@@ -161,13 +168,25 @@ public function removeEventSubscriber(EventSubscriber $subscriber): void
161168
private function initializeListeners(string $eventName): void
162169
{
163170
$this->initialized[$eventName] = true;
171+
172+
// We'll refill the whole array in order to keep the same order
173+
$listeners = [];
164174
foreach ($this->listeners[$eventName] as $hash => $listener) {
165175
if (\is_string($listener)) {
166-
$this->listeners[$eventName][$hash] = $listener = $this->container->get($listener);
176+
$listener = $this->container->get($listener);
177+
$newHash = $this->getHash($listener);
178+
179+
$this->initializedHashMapping[$eventName][$hash] = $newHash;
167180

168-
$this->methods[$eventName][$hash] = $this->getMethod($listener, $eventName);
181+
$listeners[$newHash] = $listener;
182+
183+
$this->methods[$eventName][$newHash] = $this->getMethod($listener, $eventName);
184+
} else {
185+
$listeners[$hash] = $listener;
169186
}
170187
}
188+
189+
$this->listeners[$eventName] = $listeners;
171190
}
172191

173192
private function initializeSubscribers(): void

Tests/ContainerAwareEventManagerTest.php

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

158+
public function testRemoveAllEventListener()
159+
{
160+
$this->container->set('lazy', new MyListener());
161+
$this->evm->addEventListener('foo', 'lazy');
162+
$this->evm->addEventListener('foo', new MyListener());
163+
164+
foreach ($this->evm->getAllListeners() as $event => $listeners) {
165+
foreach ($listeners as $listener) {
166+
$this->evm->removeEventListener($event, $listener);
167+
}
168+
}
169+
170+
$this->assertSame([], $this->evm->getListeners('foo'));
171+
}
172+
158173
public function testRemoveEventListenerAfterDispatchEvent()
159174
{
160175
$this->container->set('lazy', $listener1 = new MyListener());

0 commit comments

Comments
 (0)