Development

Symfony11Events

You must first sign up to be able to contribute.

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.

The core framework built-in events are listed in the symfony book.

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;
  }