Listening for Events

You can write your a custom event handler by creating a PHP class that extends from Application_Event_Observer_Abstract.

Observer Name and Location

The name of the event you're observing determine the path, filename and class name of the class. The name of the event is also its path (with .php appended) on the filesystem. The class name is the name of the event with slashes replaced by underscores.

For example, a handler for /some/sample/event would have the filename ./basepath/some/sample/event.php. Its PHP class name would be classprefix_some_sample_event.

The base path and base class name depend on whether your observer lives with a module or if it lives with a driver.

If the observer is for a module, it must live inside the Observers directory in the module include path. For example, ./lib/modules/ModuleName/include/Observers/some/sample/event.php. The class name would be ModuleName_Observers_some_sample_event.

Example 6.5. Creating a module observer (event.php)

<?php
    class ModuleName_Observers_some_sample_event extends Application_Event_Observer_Abstract
    {
        // observer code here
    }
?>

If the observer is for a driver, it must live inside the observers directory in the the driver path. For example, ./lib/drivers/path/to/driver/observers/some/sample/event.php. The class name would be Driver_path_to_driver_observers_some_sample_event.

Example 6.6. Creating a driver observer (event.php)

<?php
    class Driver_path_to_driver_observers_some_sample_event extends Application_Event_Observer_Abstract
    {
        // observer code here
    }
?>

Implementing the Observer

The only method you must implement when creating an observer is the notify method.

  • public function notify(Application_Event $event)

This method accepts an instance of Application_Event as its only argument. This is the same class we used when triggering an event.

To retrieve the data stored with the event, call the get() method. This method accepts the name of the data the retrieve.

Example 6.7. Observing an event

<?php
    class Driver_path_to_driver_observers_some_sample_event extends Application_Event_Observer_Abstract
    {
        public function notify(Application_Event $event)
        {
            $foo = $event->get('foo');
            
            // do something with the foo value
        }
    }
?>

Sending a Response

An observer can send a response, which the event triggerer can decide to use or not. You can check if a response is requested by calling the getResponds() method on the passed event.

To respond to an event, return an instance of Application_Event_Response. You can set any data to be passed along with this object, which will then made available to the requestor.

Example 6.8. Observing and responding to an event

<?php
    class Driver_path_to_driver_observers_some_sample_event extends Application_Event_Observer_Abstract
    {
        public function notify(Application_Event $event)
        {
            if (!$event->getResponds()) {
                return;
            }
            
            $response = new Application_Event_Response();
            $response->set('success', true);
            
            return $response;
        }
    }
?>

Registering An Observer

In order to speed up response time, Recite CMS caches a list of the available observers for any given event. Once you've created a new observer, you can either clear the system-wide caching using the Recite CMS Administration Site, or you can clear the observer cache using the clear-cache.php script in the ./application/tools/events directory.

To ensure Recite CMS can see your observer, you can run the get-observers.php script in the same directory. This script accepts the name of the event as its only argument.

For instance run ./get-observers /some/sample/event to see if your observer is being detected.