bit3/contao-event-dispatcher

This package is abandoned and no longer maintained. The author suggests using the contao-community-alliance/event-dispatcher package instead.

Event dispatcher service for Contao Open Source CMS

dev-master / 1.0.x-dev 2013-10-30 15:00 UTC

This package is not auto-updated.

Last update: 2019-02-20 17:30:20 UTC


README

Why an event dispatcher for Contao Open Source CMS, are the hooks not enough? First you need to understand, there is no real difference between hooks and events. The are both notifications from within the system.

But events are more elastic than hooks. They can be hand round, consumed, stopped or bubble upon a hierarchy.

The real big reasons, why an event dispatcher exists for Contao are:

  1. Events are standard paradigm in software design.
  2. Hooking is a paradigm to alter the behavior of a software, is it not designed for notifications.
  3. Hooks are only a special form of events.
  4. The symfony event dispatcher this extension based on is widely used.
  5. The event dispatcher can handle every form of callbacks, like closures or static methods.

Listen on events

The event dispatcher provide two ways to listen on events.

First and mostly used is an event listener. It is designed to listen on a single event.

Second the event subscriber is designed to listen on multiple events.

Event listener per configuration

Use $GLOBALS['TL_EVENTS'] to register your event handlers.

With a closure:

$GLOBALS['TL_EVENTS']['event-name'][] = function($event) {
	// event code
};

With a static callable:

$GLOBALS['TL_EVENTS']['event-name'][] = array('MyClass', 'myCallable');

With an object callable:

$GLOBALS['TL_EVENTS']['event-name'][] = array(new MyClass(), 'myCallable');

Handle with priority

To define the priority, you can use an array with the listener as first and the priority as second element.

$GLOBALS['TL_EVENTS']['event-name'][] = array($listener, $priority);

Event listener per code

$container['event-dispatcher']->addListener('event-name', $listener);

Event subscriber per configuration

Use $GLOBALS['TL_EVENT_SUBSCRIBERS'] to register your subscribers.

With a factory:

$GLOBALS['TL_EVENT_SUBSCRIBERS'][] = function($eventDispatcher) {
	return new MyEventSubscriber();
};

With an object class name:

$GLOBALS['TL_EVENT_SUBSCRIBERS'][] = 'MyEventSubscriber';

With an object instance:

$GLOBALS['TL_EVENT_SUBSCRIBERS'][] = new MyEventSubscriber();

Event subscriber per code

$container['event-dispatcher']->addSubscriber(new MyEventSubscriber());