Skip to content

Commit bd8d2b8

Browse files
committed
[EventDispatcher] changed listener storage to use SplObjectStorage to avoid collisions
1 parent e596931 commit bd8d2b8

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

src/Symfony/Component/EventDispatcher/EventDispatcher.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public function getListeners($eventName = null)
7777
}
7878

7979
$sorted = array();
80-
foreach ($this->listeners as $eventName => $listeners) {
80+
foreach (array_keys($this->listeners) as $eventName) {
8181
if (!isset($this->sorted[$eventName])) {
8282
$this->sortListeners($eventName);
8383
}
@@ -103,16 +103,15 @@ public function hasListeners($eventName = null)
103103
*/
104104
public function addListener($eventNames, $listener, $priority = 0)
105105
{
106-
$hash = spl_object_hash($listener);
107106
foreach ((array) $eventNames as $eventName) {
108107
if (!isset($this->listeners[$eventName][$priority])) {
109108
if (!isset($this->listeners[$eventName])) {
110109
$this->listeners[$eventName] = array();
111110
}
112-
$this->listeners[$eventName][$priority] = array();
111+
$this->listeners[$eventName][$priority] = new \SplObjectStorage();
113112
}
114113

115-
$this->listeners[$eventName][$priority][$hash] = $listener;
114+
$this->listeners[$eventName][$priority]->attach($listener);
116115
unset($this->sorted[$eventName]);
117116
}
118117
}
@@ -127,10 +126,9 @@ public function removeListener($eventNames, $listener)
127126
continue;
128127
}
129128

130-
$hash = spl_object_hash($listener);
131129
foreach (array_keys($this->listeners[$eventName]) as $priority) {
132-
if (isset($this->listeners[$eventName][$priority][$hash])) {
133-
unset($this->listeners[$eventName][$priority][$hash], $this->sorted[$eventName]);
130+
if (isset($this->listeners[$eventName][$priority][$listener])) {
131+
unset($this->listeners[$eventName][$priority][$listener], $this->sorted[$eventName]);
134132
}
135133
}
136134
}
@@ -180,10 +178,13 @@ protected function triggerListener($listener, $eventName, Event $event)
180178
private function sortListeners($eventName)
181179
{
182180
$this->sorted[$eventName] = array();
183-
184181
if (isset($this->listeners[$eventName])) {
185182
krsort($this->listeners[$eventName]);
186-
$this->sorted[$eventName] = array_values(call_user_func_array('array_merge', $this->listeners[$eventName]));
183+
foreach ($this->listeners[$eventName] as $listeners) {
184+
foreach ($listeners as $listener) {
185+
$this->sorted[$eventName][] = $listener;
186+
}
187+
}
187188
}
188189
}
189190
}

0 commit comments

Comments
 (0)