Skip to content

Commit

Permalink
renamed some methods in the event dispatcher
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Mar 17, 2011
1 parent 99f433b commit d014b34
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 69 deletions.
107 changes: 54 additions & 53 deletions EventDispatcher.php
Expand Up @@ -23,7 +23,8 @@
namespace Symfony\Component\EventDispatcher;

/**
* The EventDispatcherInterface is the central point of Doctrine's event listener system.
* The EventDispatcherInterface is the central point of Symfony's event listener system.
*
* Listeners are registered on the manager and events are dispatched through the
* manager.
*
Expand Down Expand Up @@ -65,9 +66,9 @@ class EventDispatcher implements EventDispatcherInterface
private $sorted = array();

/**
* @see EventDispatcherInterface::dispatchEvent
* @see EventDispatcherInterface::dispatch
*/
public function dispatchEvent($eventName, Event $event = null)
public function dispatch($eventName, Event $event = null)
{
if (isset($this->listeners[$eventName])) {
if (null === $event) {
Expand All @@ -86,48 +87,6 @@ public function dispatchEvent($eventName, Event $event = null)
}
}

/**
* Triggers the listener method for an event.
*
* This method can be overridden to add functionality that is executed
* for each listener.
*
* @param object $listener The event listener on which to invoke the listener method.
* @param string $eventName The name of the event to dispatch. The name of the event is
* the name of the method that is invoked on listeners.
* @param Event $event The event arguments to pass to the event handlers/listeners.
*/
protected function triggerListener($listener, $eventName, Event $event)
{
if ($listener instanceof \Closure) {
$listener->__invoke($event);
} else {
$listener->$eventName($event);
}
}

/**
* Sorts the internal list of listeners for the given event by priority.
*
* Calling this method multiple times will not cause overhead unless you
* add new listeners. As long as no listener is added, the list for an
* event name won't be sorted twice.
*
* @param string $event The name of the event.
*/
private function sortListeners($eventName)
{
if (!$this->sorted[$eventName]) {
$p = $this->priorities[$eventName];

uasort($this->listeners[$eventName], function ($a, $b) use ($p) {
return $p[spl_object_hash($b)] - $p[spl_object_hash($a)];
});

$this->sorted[$eventName] = true;
}
}

/**
* @see EventDispatcherInterface::getListeners
*/
Expand Down Expand Up @@ -155,9 +114,9 @@ public function hasListeners($eventName)
}

/**
* @see EventDispatcherInterface::addEventListener
* @see EventDispatcherInterface::addListener
*/
public function addEventListener($eventNames, $listener, $priority = 0)
public function addListener($eventNames, $listener, $priority = 0)
{
// Picks the hash code related to that listener
$hash = spl_object_hash($listener);
Expand All @@ -176,9 +135,9 @@ public function addEventListener($eventNames, $listener, $priority = 0)
}

/**
* @see EventDispatcherInterface::removeEventListener
* @see EventDispatcherInterface::removeListener
*/
public function removeEventListener($eventNames, $listener)
public function removeListener($eventNames, $listener)
{
// Picks the hash code related to that listener
$hash = spl_object_hash($listener);
Expand All @@ -193,10 +152,52 @@ public function removeEventListener($eventNames, $listener)
}

/**
* @see EventDispatcherInterface::addEventSubscriber
* @see EventDispatcherInterface::addSubscriber
*/
public function addEventSubscriber(EventSubscriberInterface $subscriber, $priority = 0)
public function addSubscriber(EventSubscriberInterface $subscriber, $priority = 0)
{
$this->addEventListener($subscriber->getSubscribedEvents(), $subscriber, $priority);
$this->addListener($subscriber->getSubscribedEvents(), $subscriber, $priority);
}

/**
* Triggers the listener method for an event.
*
* This method can be overridden to add functionality that is executed
* for each listener.
*
* @param object $listener The event listener on which to invoke the listener method.
* @param string $eventName The name of the event to dispatch. The name of the event is
* the name of the method that is invoked on listeners.
* @param Event $event The event arguments to pass to the event handlers/listeners.
*/
protected function triggerListener($listener, $eventName, Event $event)
{
if ($listener instanceof \Closure) {
$listener->__invoke($event);
} else {
$listener->$eventName($event);
}
}

/**
* Sorts the internal list of listeners for the given event by priority.
*
* Calling this method multiple times will not cause overhead unless you
* add new listeners. As long as no listener is added, the list for an
* event name won't be sorted twice.
*
* @param string $event The name of the event.
*/
private function sortListeners($eventName)
{
if (!$this->sorted[$eventName]) {
$p = $this->priorities[$eventName];

uasort($this->listeners[$eventName], function ($a, $b) use ($p) {
return $p[spl_object_hash($b)] - $p[spl_object_hash($a)];
});

$this->sorted[$eventName] = true;
}
}
}
}
32 changes: 16 additions & 16 deletions EventDispatcherInterface.php
Expand Up @@ -12,14 +12,25 @@
namespace Symfony\Component\EventDispatcher;

/**
* The EventDispatcherInterface is the central point of Doctrine's event listener system.
* The EventDispatcherInterface is the central point of Symfony's event listener system.
* Listeners are registered on the manager and events are dispatched through the
* manager.
*
* @author Bernhard Schussek <bschussek@gmail.com>
*/
interface EventDispatcherInterface
{
/**
* Dispatches an event to all registered listeners.
*
* @param string $eventName The name of the event to dispatch. The name of
* the event is the name of the method that is
* invoked on listeners.
* @param Event $event The event to pass to the event handlers/listeners.
* If not supplied, an empty Event instance is created.
*/
function dispatch($eventName, Event $event = null);

/**
* Adds an event listener that listens on the specified events.
*
Expand All @@ -29,7 +40,7 @@ interface EventDispatcherInterface
* listener will be triggered in the chain.
* Defaults to 0.
*/
function addEventListener($eventNames, $listener, $priority = 0);
function addListener($eventNames, $listener, $priority = 0);

/**
* Adds an event subscriber. The subscriber is asked for all the events he is
Expand All @@ -40,26 +51,15 @@ function addEventListener($eventNames, $listener, $priority = 0);
* listener will be triggered in the chain.
* Defaults to 0.
*/
function addEventSubscriber(EventSubscriberInterface $subscriber, $priority = 0);
function addSubscriber(EventSubscriberInterface $subscriber, $priority = 0);

/**
* Removes an event listener from the specified events.
*
* @param string|array $eventNames The event(s) to remove a listener from.
* @param object $listener The listener object to remove.
*/
function removeEventListener($eventNames, $listener);

/**
* Dispatches an event to all registered listeners.
*
* @param string $eventName The name of the event to dispatch. The name of
* the event is the name of the method that is
* invoked on listeners.
* @param Event $event The event to pass to the event handlers/listeners.
* If not supplied, an empty Event instance is created.
*/
function dispatchEvent($eventName, Event $event = null);
function removeListener($eventNames, $listener);

/**
* Gets the listeners of a specific event or all listeners.
Expand All @@ -80,4 +80,4 @@ function getListeners($eventName = null);
* otherwise.
*/
function hasListeners($eventName);
}
}

0 comments on commit d014b34

Please sign in to comment.