joomla/joomla-platform — pull request #1666 by florianv (2012-11-04 15:29:07+00:00)
Title: Implementing a new event system

This is https://github.com/joomla/joomla-platform/pull/1621.

I added unit tests and changed some things.

Resume :
-----------

All event listeners must take a `JEvent` object as sole parameter.

```php
registerListener(new Listener);

// The listener can be registered only
// to some specific events.
$dispatcher->registerListener(
new Listener,
array('onBeforeSomething')
);

/**
* The listener can be registered to
* some events with a given priority.
* The default priority is 0.
* If some listeners have the same priority,
* they will be called in the order they were
* registered.
**/
$dispatcher->registerListener(
new Listener,
array()
array('onBeforeSomething' => 4)
);
```

Closures

```php
registerListener(
$listener,
array('onBeforeSomething')
);

// Specifying a priority.
$dispatcher->registerListener(
$listener,
array('onBeforeSomething')
array('onBeforeSomething' => 4)
);
```

Closures and 'normal' objects can be used together in the dispatcher.
Additionnaly, the event propagation can be stopped by a listener.
```php
stopPropagation();
}
}

$listenerTwo = function (JEvent $event)
{
echo 'Please pass me this event!';
};

$dispatcher = JEventDispatcher::getInstance();

$dispatcher->registerListener(new ListenerOne);
$dispatcher->registerListener(
$listenerTwo,
array('onBeforeSomething')
);

$dispatcher->triggerEvent('onBeforeSomething');
```

By default a JEvent object is created and passed to the listeners
when triggering the event.
You can inject your custom event object using two methods :

```php
setArgument('foo', 'bar');

$dispatcher = JEventDispatcher::getInstance();

// First method : registering a custom event.
$dispatcher->registerEvent($onBeforeSomething);

// Second method : when triggering the event.
$dispatcher->triggerEvent($onBeforeSomething);