Event Management

From Cloudrexx Development Wiki
Jump to: navigation, search

In Contrexx versions 3.1 an newer, there's a global event handler available:

Get the handler

In order to receive the event handler instance you need the instance of the Cx class:

$eventHandlerInstance = $cx->getEvents();

Create an event

In order to be able to trigger and register to an event, it needs to be created:

$eventHandlerInstance->addEvent('SysLog:Add');

The name of the event should be named after the following convention: 'component/event'

Please note that event need to be created on each page load in order to work properly. A good place to do so is the registerEvents() method of the ComponentController.

Register a listener to an event

In order to listen to an event, you need to create an event listener (a class that implements the interface \Cx\Core\Event\Model\Entity\EventListener):

$eventHandlerInstance->addEventListener('SysLog:Add', $myEventListener);

Please note that event listeners need to be registered on each page load in order to work properly. A good place to do so is the registerEventListeners() method of the ComponentController.

Trigger an event

To trigger an event, simply:

// $myEventArguments must be an array
$myEventArguments = array();
$eventHandlerInstance->triggerEvent('SysLog:Add', $myEventArguments);

Model events (Doctrine events)

Since Contrexx 3.1 doctrine events are wrapped through the Contrexx event handler. In order to listen to a doctrine event, add an event listener using the addModelListener method:

$eventHandlerInstance->addModelListener({doctrine_event_type}, {escaped_entity_class}, $myEventListener);

while:

  • {doctrine_event_type} is one of doctrines event types (for example: \Doctrine\ORM\Events::prePersist)
  • {escaped_entity_class} is a doctrine entity class name with escaped backslashes (for example: 'Cx\\Core\\ContentManager\\Model\\Entity\\Page')