Development

Symfony11Events

You must first sign up to be able to contribute.

Version 19 (modified by fabien, 10 years ago)
--

symfony 1.1 events

symfony 1.1 core classes notify events and listen to events.

An event is composed of a namespace and a name separated by a dot. So, the log event name in the application namespace is named application.log.

By default, the core framework have some built-in events:

event namespace event name type notifiers listeners argument names
application log notify lot of classes sfLogger priority
throw_exception notifyUntil sfException sfBrowser -
command log notify sfCommand* classes sfCommandLogger priority
pre_command notifyUntil sfTask arguments, options
post_command notify sfTask -
filter_options filter sfTask command_manager
component method_not_found notifyUntil sfComponent method, arguments
context load_factories notify sfContext -
controller change_action notify sfController sfI18N module, action
sfWebResponse
method_not_found notifyUntil sfController method, arguments
page_not_found notify sfController module, action
plugin pre_install notify sfPluginManager channel, plugin, is_package
post_install notify sfPluginManager sfSymfonyPluginManager channel, plugin
pre_uninstall notify sfPluginManager sfSymfonyPluginManager channel, plugin
post_uninstall notify sfPluginManager channel, plugin
request filter_parameters filter sfWebRequest sfRouting path_info
method_not_found notifyUntil sfRequest method, arguments
response method_not_found notifyUntil sfResponse method, arguments
filter_content filter sfResponse sfWebDebugLogger -
routing load_configuration notify sfRouting -
task cache.clear notifyUntil sfCacheClearTask app, type, env
template filter_parameters filter sfViewParameterHolder sfContext -
user change_culture notify sfUser sfI18N culture
sfRouting
method_not_found notifyUntil sfUser method, arguments
view configure_format notify sfView format, response, request
method_not_found notifyUntil sfView method, arguments

Examples

To notify an event:

$dispatcher->notify(new sfEvent($this, 'user.change_culture', array('culture' => $culture)));

To listen to an event:

$dispatcher->connect('user.change_culture', array($this, 'listenToChangeCultureEvent'));

// ...

  /**
   * Listens to the user.change_culture event.
   *
   * @param sfEvent An sfEvent instance
   *
   */
  public function listenToChangeCultureEvent(sfEvent $event)
  {
    // change the message format object with the new culture
    $this->setCulture($event['culture']);
  }

Event types

Events can be triggered by 3 different methods:

  • notify
  • notifyUntil
  • filter

notify

  • notify notifies all listeners
  • Listeners cannot returns a value
  • All listeners are guaranteed to be executed
  public function notify(sfEvent $event)
  {
    foreach ($this->getListeners($event->getName()) as $listener)
    {
      call_user_func($listener, $event);
    }

    return $event;
  }

notifyUntil

  • notifyUntil notifies all listeners until one stops the chain by returning a true value
  public function notifyUntil(sfEvent $event)
  {
    foreach ($this->getListeners($event->getName()) as $listener)
    {
      if (call_user_func($listener, $event))
      {
        $event->setProcessed(true);
        break;
      }
    }

    return $event;
  }

filter

  • filter notifies all listeners that they can filter the given value
  • All listeners are passed the value and they must return the filtered value
  • All listeners are guaranteed to be executed
  public function filter(sfEvent $event, $value)
  {
    foreach ($this->getListeners($event->getName()) as $listener)
    {
      $value = call_user_func_array($listener, array($event, $value));
    }

    $event->setReturnValue($value);

    return $event;
  }