joomla/joomla-platform — pull request #1621 by florianv (2012-10-20 01:29:46+00:00)
Title: Implementing a new event system

This is not ready to be merged and probably won't never be.

I wanted to implement a new feature for the event package, but for some reasons I'm not 100% fan of the actual system.

For example, JEvent might be called JEventListener/Observer, and the difference between JDispatcher::attach and register is not totally clear.
Also, I'm not sure if the Dispatcher should instanciate the listener, I think the listener object must be passed to the dispatcher instead.

For these reasons, I ended up rewriting the package.
So I am asking you what do you think about a new event package that can look like this.

This implementation allow to specify a priority for a given listener, so if you have multiple listeners for a given event, you can manage the order in which they are notified during the registration.

An event is a particular object having a name and holding arguments.
Each listener must take a JEvent object as parameter and can manipulate its arguments.
Additionnaly, they can stop the event propagation, so the next listeners are not notified.

Moreover, this Dispatcher allows to use closures as listeners.

Like the actual one it is possible to use any object as event listener.

Some examples :

Using a closure as listener and a class, stopping the event propagation.

```php
stopPropagation();
}
}

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

$dispatcher = JEventDispatcher::getInstance();

$dispatcher->registerListener(array('onBeforeSomething'), new MyListener);
$dispatcher->registerListener(array('onBeforeSomething'), $listener);

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

Registering a listener with a given priority.

```php
registerListener(
array('onBeforeSomething'),
new Listener1,
array('onBeforeSomething' => 2)
);

// Registering the Listener2 with a priority = 1
// for the onBeforeSomething event
$dispatcher->registerListener(
array('onBeforeSomething'),
new Listener2,
array('onBeforeSomething' => 1)
);

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

Injecting you custom events.
There are two ways to override the default event object that is passed to all listeners.

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

// First method to inject an event object.
$dispatcher->registerEvent($onBeforeSomething);

// Second method to inject an event a object.
$dispatcher->triggerEvent($onBeforeSomething);
```