2017 © Pedro Peláez
 

contao-bundle event-dispatcher

Event dispatcher service for Contao Open Source CMS

image

contao-community-alliance/event-dispatcher

Event dispatcher service for Contao Open Source CMS

  • Sunday, February 11, 2018
  • by xtra
  • Repository
  • 6 Watchers
  • 2 Stars
  • 47,129 Installations
  • PHP
  • 54 Dependents
  • 0 Suggesters
  • 3 Forks
  • 1 Open issues
  • 11 Versions
  • 2 % Grown

The README.md

Version Build Status License Downloads, (*1)

NOTE: This is obsolete in Contao 4 - you should use the event dispatcher from the symfony kernel directly for Contao 4 only extensions. See below for maintaining compatibility with both Contao versions., (*2)

Event dispatcher for Contao Open Source CMS

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., (*3)

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

The real big reasons, why an event dispatcher exists for Contao are:, (*5)

  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., (*6)

First and mostly used is an event listener. It is designed to listen on a single event., (*7)

Second the event subscriber is designed to listen on multiple events., (*8)

Event listener per configuration

Since version 1.3 there are two ways to define your listeners per configuration., (*9)

/config/event_listeners.php

We recommend to use this method!, (*10)

The file /config/event_listeners.php must return an array of event names as keys and listeners as values., (*11)

<?php
return array(
    // With a closure
    'event-name' => array(
        function($event) {
            // event code
        }
    ),

    // With a static callable
    'event-name' => array(
        array('MyEventListener', 'myCallable')
    ),

    // With an object callable
    'event-name' => array(
        array(new MyEventListener(), 'myCallable')
    ),

    // With a service object
    'event-name' => array(
        array($GLOBALS['container']['my_event_listener'], 'myCallable')
    ),

    // You can wrap the listener into an array with a priority
    'event-name' => array(
        array($listener, $priority)
    ),
);

/config/config.php

In your /config/config.php use $GLOBALS['TL_EVENTS'] to register your event handlers., (*12)

With a closure:, (*13)

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

With a static callable:, (*14)

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

With an object callable:, (*15)

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

Handle with priority

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

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

Event listener per code

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

Event subscriber per configuration

Since version 1.3 there are two ways to define your listeners per configuration., (*17)

/config/event_subscribers.php

We recommend to use this method!, (*18)

The file /config/event_subscribers.php must return an array of subscribers., (*19)

<?php
return array(
    // With a factory
    function($eventDispatcher) {
        return new MyEventSubscriber();
    },

    // With an object class name
    'MyEventSubscriber',

    // With an object instance
    new MyEventSubscriber(),

    // With a service object
    $GLOBALS['container']['my_event_subscriber'],
);

/config/config.php

In your /config/config.php use $GLOBALS['TL_EVENT_SUBSCRIBERS'] to register your subscribers., (*20)

With a factory:, (*21)

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

With an object class name:, (*22)

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

With an object instance:, (*23)

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

Event subscriber per code

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

Compatibility with Contao 3 & 4.

You are encouraged to register your events directly in the symfony event dispatcher in Contao 4 using service configuration files (yaml, xml, ...)., (*24)

However, for maintaining compatibility, you still need to supply the event_listeners.php and event_subscribers.php files as otherwise the events will not get registered in Contao 3., (*25)

To register events only in Contao 3, we suggest to return an empty array if the version compare is equal to 4., (*26)

 <?php

+ // Contao 4 registers events via symfony configuration.
+ if (version_compare(VERSION, '4.0', '>=')) {
+     return [];
+ }

 return [
     'some.event' => ['Some\EventListener', 'someMethod']
 ];

The Versions

11/02 2018

dev-master

9999999-dev http://c-c-a.org/

Event dispatcher service for Contao Open Source CMS

  Sources   Download

LGPL-3.0+ LGPL-3.0-or-later

The Requires

 

The Development Requires

event contao

11/02 2018

dev-develop

dev-develop http://c-c-a.org/

Event dispatcher service for Contao Open Source CMS

  Sources   Download

LGPL-3.0+ LGPL-3.0-or-later

The Requires

 

The Development Requires

event contao

11/02 2018
06/02 2018

dev-support/1.3.x

dev-support/1.3.x http://c-c-a.org/

Event dispatcher service for Contao Open Source CMS

  Sources   Download

LGPL-3.0-or-later

The Requires

 

The Development Requires

event contao

06/02 2018

dev-feature/contao4

dev-feature/contao4 http://c-c-a.org/

Event dispatcher service for Contao Open Source CMS

  Sources   Download

LGPL-3.0+ LGPL-3.0-or-later

The Requires

 

The Development Requires

event contao

06/02 2018
19/12 2013