PHP code example of contao-community-alliance / event-dispatcher

1. Go to this page and download the library: Download contao-community-alliance/event-dispatcher library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

contao-community-alliance / event-dispatcher example snippets



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)
    ),
);

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

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

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

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

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


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'],
);

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

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

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

$container['event-dispatcher']->addSubscriber(new MyEventSubscriber());
diff
 
 
+ // Contao 4 registers events via symfony configuration.
+ if (version_compare(VERSION, '4.0', '>=')) {
+     return [];
+ }
 
 return [
     'some.event' => ['Some\EventListener', 'someMethod']
 ];