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 663b0a9 commit 1219b98
Show file tree
Hide file tree
Showing 18 changed files with 136 additions and 135 deletions.
2 changes: 1 addition & 1 deletion src/Symfony/Bundle/AsseticBundle/Command/DumpCommand.php
Expand Up @@ -48,7 +48,7 @@ protected function execute(InputInterface $input, OutputInterface $output)

// notify an event so custom stream wrappers can be registered lazily
$event = new WriteEvent($basePath);
$this->container->get('event_dispatcher')->dispatchEvent(Events::onAsseticWrite, $writeEvent);
$this->container->get('event_dispatcher')->dispatch(Events::onAsseticWrite, $writeEvent);

if ($input->getOption('watch')) {
return $this->watch($am, $basePath, $output, $this->container->getParameter('kernel.debug'));
Expand Down
Expand Up @@ -32,7 +32,7 @@ protected function registerSubscribers($prefix, $definition)
);

foreach ($subscribers as $id => $instances) {
$definition->addMethodCall('addEventSubscriber', array(new Reference($id)));
$definition->addMethodCall('addSubscriber', array(new Reference($id)));
}
}

Expand All @@ -52,7 +52,7 @@ protected function registerListeners($prefix, $definition)
}

if (0 < count($events)) {
$definition->addMethodCall('addEventListener', array(
$definition->addMethodCall('addListener', array(
$events,
new Reference($listenerId),
));
Expand Down
Expand Up @@ -29,7 +29,7 @@ protected function registerSubscribers($prefix, $definition)
);

foreach ($subscribers as $id => $instances) {
$definition->addMethodCall('addEventSubscriber', array(new Reference($id)));
$definition->addMethodCall('addSubscriber', array(new Reference($id)));
}
}

Expand All @@ -49,7 +49,7 @@ protected function registerListeners($prefix, $definition)
}

if (0 < count($events)) {
$definition->addMethodCall('addEventListener', array(
$definition->addMethodCall('addListener', array(
$events,
new Reference($listenerId),
));
Expand Down
Expand Up @@ -56,7 +56,7 @@ public function __construct(ContainerInterface $container)
* listener will be triggered in the chain.
* Defaults to 0.
*/
public function addEventListenerService($events, $serviceId, $priority = 0)
public function addListenerService($events, $serviceId, $priority = 0)
{
if (!is_string($serviceId)) {
throw new \InvalidArgumentException('Expected a string argument');
Expand All @@ -74,14 +74,14 @@ public function addEventListenerService($events, $serviceId, $priority = 0)
* Lazily loads listeners for this event from the dependency injection
* container.
*/
public function dispatchEvent($eventName, Event $event = null)
public function dispatch($eventName, Event $event = null)
{
if (isset($this->listenerIds[$eventName])) {
foreach ($this->listenerIds[$eventName] as $serviceId => $priority) {
$this->addEventListener($eventName, $this->container->get($serviceId), $priority);
$this->addListener($eventName, $this->container->get($serviceId), $priority);
}
}

parent::dispatchEvent($eventName, $event);
parent::dispatch($eventName, $event);
}
}
Expand Up @@ -34,7 +34,7 @@ public function process(ContainerBuilder $container)
throw new \InvalidArgumentException(sprintf('Service "%s" must define the "event" attribute on "kernel.listener" tags.', $id));
}

$definition->addMethodCall('addEventListenerService', array($event['event'], $id, $priority));
$definition->addMethodCall('addListenerService', array($event['event'], $id, $priority));
}
}
}
Expand Down
107 changes: 54 additions & 53 deletions src/Symfony/Component/EventDispatcher/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 src/Symfony/Component/EventDispatcher/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);
}
}
10 changes: 5 additions & 5 deletions src/Symfony/Component/HttpKernel/HttpKernel.php
Expand Up @@ -89,7 +89,7 @@ protected function handleRaw(Request $request, $type = self::MASTER_REQUEST)
{
// request
$event = new GetResponseEvent($this, $request, $type);
$this->dispatcher->dispatchEvent(Events::onCoreRequest, $event);
$this->dispatcher->dispatch(Events::onCoreRequest, $event);

if ($event->hasResponse()) {
return $this->filterResponse($event->getResponse(), $request, $type);
Expand All @@ -101,7 +101,7 @@ protected function handleRaw(Request $request, $type = self::MASTER_REQUEST)
}

$event = new FilterControllerEvent($this, $controller, $request, $type);
$this->dispatcher->dispatchEvent(Events::filterCoreController, $event);
$this->dispatcher->dispatch(Events::filterCoreController, $event);
$controller = $event->getController();

// controller arguments
Expand All @@ -113,7 +113,7 @@ protected function handleRaw(Request $request, $type = self::MASTER_REQUEST)
// view
if (!$response instanceof Response) {
$event = new GetResponseForControllerResultEvent($this, $request, $type, $response);
$this->dispatcher->dispatchEvent(Events::onCoreView, $event);
$this->dispatcher->dispatch(Events::onCoreView, $event);

if ($event->hasResponse()) {
$response = $event->getResponse();
Expand Down Expand Up @@ -142,7 +142,7 @@ protected function filterResponse(Response $response, Request $request, $type)
{
$event = new FilterResponseEvent($this, $request, $type, $response);

$this->dispatcher->dispatchEvent(Events::filterCoreResponse, $event);
$this->dispatcher->dispatch(Events::filterCoreResponse, $event);

return $event->getResponse();
}
Expand All @@ -159,7 +159,7 @@ protected function filterResponse(Response $response, Request $request, $type)
protected function handleException(\Exception $e, $request, $type)
{
$event = new GetResponseForExceptionEvent($this, $request, $type, $e);
$this->dispatcher->dispatchEvent(Events::onCoreException, $event);
$this->dispatcher->dispatch(Events::onCoreException, $event);

if (!$event->hasResponse()) {
throw $e;
Expand Down
Expand Up @@ -213,7 +213,7 @@ private function onSuccess(GetResponseEvent $event, Request $request, TokenInter

if (null !== $this->dispatcher) {
$loginEvent = new InteractiveLoginEvent($request, $token);
$this->dispatcher->dispatchEvent(Events::onSecurityInteractiveLogin, $loginEvent);
$this->dispatcher->dispatch(Events::onSecurityInteractiveLogin, $loginEvent);
}

if (null !== $this->successHandler) {
Expand Down
Expand Up @@ -82,7 +82,7 @@ public final function handle(GetResponseEvent $event)

if (null !== $this->dispatcher) {
$loginEvent = new InteractiveLoginEvent($request, $token);
$this->dispatcher->dispatchEvent(Events::onSecurityInteractiveLogin, $loginEvent);
$this->dispatcher->dispatch(Events::onSecurityInteractiveLogin, $loginEvent);
}
} catch (AuthenticationException $failed) {
$this->securityContext->setToken(null);
Expand Down
Expand Up @@ -49,7 +49,7 @@ public function __construct(SecurityContext $context, array $userProviders, $con
$this->contextKey = $contextKey;

if (null !== $dispatcher) {
$dispatcher->addEventListener(Events::onCoreResponse, $this);
$dispatcher->addListener(Events::onCoreResponse, $this);
}
}

Expand Down
Expand Up @@ -58,7 +58,7 @@ public function __construct(SecurityContextInterface $context, AuthenticationTru
*/
public function register(EventDispatcherInterface $dispatcher)
{
$dispatcher->addEventListener(Events::onCoreException, $this);
$dispatcher->addListener(Events::onCoreException, $this);
}

/**
Expand Down
Expand Up @@ -79,7 +79,7 @@ public function handle(GetResponseEvent $event)

if (null !== $this->dispatcher) {
$loginEvent = new InteractiveLoginEvent($request, $token);
$this->dispatcher->dispatchEvent(Events::onSecurityInteractiveLogin, $loginEvent);
$this->dispatcher->dispatch(Events::onSecurityInteractiveLogin, $loginEvent);
}

if (null !== $this->logger) {
Expand Down

0 comments on commit 1219b98

Please sign in to comment.