Skip to content

Commit

Permalink
[EventDispatcher] changed listener storage to use SplObjectStorage to…
Browse files Browse the repository at this point in the history
… avoid collisions
  • Loading branch information
fabpot committed Mar 18, 2011
1 parent e596931 commit bd8d2b8
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions src/Symfony/Component/EventDispatcher/EventDispatcher.php
Expand Up @@ -77,7 +77,7 @@ public function getListeners($eventName = null)
}

$sorted = array();
foreach ($this->listeners as $eventName => $listeners) {
foreach (array_keys($this->listeners) as $eventName) {
if (!isset($this->sorted[$eventName])) {
$this->sortListeners($eventName);
}
Expand All @@ -103,16 +103,15 @@ public function hasListeners($eventName = null)
*/
public function addListener($eventNames, $listener, $priority = 0)
{
$hash = spl_object_hash($listener);
foreach ((array) $eventNames as $eventName) {
if (!isset($this->listeners[$eventName][$priority])) {
if (!isset($this->listeners[$eventName])) {
$this->listeners[$eventName] = array();
}
$this->listeners[$eventName][$priority] = array();
$this->listeners[$eventName][$priority] = new \SplObjectStorage();
}

$this->listeners[$eventName][$priority][$hash] = $listener;
$this->listeners[$eventName][$priority]->attach($listener);
unset($this->sorted[$eventName]);
}
}
Expand All @@ -127,10 +126,9 @@ public function removeListener($eventNames, $listener)
continue;
}

$hash = spl_object_hash($listener);
foreach (array_keys($this->listeners[$eventName]) as $priority) {
if (isset($this->listeners[$eventName][$priority][$hash])) {
unset($this->listeners[$eventName][$priority][$hash], $this->sorted[$eventName]);
if (isset($this->listeners[$eventName][$priority][$listener])) {
unset($this->listeners[$eventName][$priority][$listener], $this->sorted[$eventName]);
}
}
}
Expand Down Expand Up @@ -180,10 +178,13 @@ protected function triggerListener($listener, $eventName, Event $event)
private function sortListeners($eventName)
{
$this->sorted[$eventName] = array();

if (isset($this->listeners[$eventName])) {
krsort($this->listeners[$eventName]);
$this->sorted[$eventName] = array_values(call_user_func_array('array_merge', $this->listeners[$eventName]));
foreach ($this->listeners[$eventName] as $listeners) {
foreach ($listeners as $listener) {
$this->sorted[$eventName][] = $listener;
}
}
}
}
}

0 comments on commit bd8d2b8

Please sign in to comment.