phmlabs/annovent

Simple lightweight event dispatcher using annotations for registering listeners

1.0.1 2016-01-14 15:09 UTC

This package is auto-updated.

Last update: 2024-12-06 05:53:54 UTC


README

#Annovent

Annovent is a simple to use event dispatcher inspired by the symfony component event dispatching implementation. It tries to provide all the features symfony does with some useful extensions.

##Simple Usage

The Annovent dispatcher can be used in a simple and standard way as you are used from symfony:

$dispatcher = new Dispatcher();
$dispatcher->connect('SomeComponent.Render', array($listener, 'Method1'));
$dispatcher->notify(new Event('SomeComponent.Render', array('foo' => 'bar'));

##Namespace

An extra feature of this event dispatcher is the so called namespacing. It is possible to register a lister to a complete set events belonging to a special namespace.

$dispatcher->connect('SomeComponent.*', array($listener, 'Method1'));
$dispatcher->connect('*', array($listener, 'Method2'));

The first listener will be notified whenever a event is fired that starts with SomeComponent. The second one will always be notified.

##Annotation

Connection a listener to a special event is not limited to the connect method. It is also possible to register a listener using annotation (see doctrine common).

class Listener
{
  /**
   * @Event("SomeComponent.Render")
   */
  public function method1(Event $event)
  {
  }
}

$dispatcher->connectListener( new Listener );

Using the connectListener method it is possible to connect a bunch of callbacks at once.

##Named Parameters

If desired the event dispatcher can be used with named parameters.

class Listener2
{
  /**
   * @Event("SomeComponent.Render")
   */
  public function method1($argument1, $foo)
  {
  }
}
$dispatcher->notify(new Event('SomeComponent.Render', array('foo' => 'bar', 'argument1' => 'arg1' ));