Logaholic.de

Avatar

queer as code!

How we use Event Driven Development in an Agavi project

We used the observer pattern to write our own EventObserver, which enables us to use Event Driven Development in an Agavi project.

Since we are working module-based, there are two simple steps to add an Event listener for your module to the system.

1. Add %module_dir%/config/event_listeners.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <listeners>
        <event>
            <name>modules.forum.post.new</name>
            <callback>Forum_DefaultListener</callback>
        </event>
    </listeners>
</config>

The <name> element holds the event name or “channel”, the <callback> element holds the name of the listener-class.

2. Add %module_dir%/listeners/Forum_DefaultListener.class.php

(Note: callback name goes here as you can see, for autoloading)

class Forum_DefaultListener implements EventListenerInterface
{
    public static function listenToEvent(Event $event) {
        $name = $event->getName();
        switch($name) {
            case 'modules.forum.post.new':
                throw new HelloWorldException();
        }
    }
}

Listening to ‘modules.forum.post.new’ events is working from this point ;)

Firing events is as easy as this, anywhere in your module-code:

EventObserver::getInstance()
    ->notify(new Event('modules.forum.post.new', $message));

This call will find its way to the configured listener, Forum_DefaultListener, and Forum_DefaultListener::listenToEvent (like any other listener) will be called with an Event, including the passed $message object.

Bookmark and Share

Related posts:

  1. Agavi vs Zend Framework Part 1 – Forms

No Comments, Comment or Ping

Reply to “How we use Event Driven Development in an Agavi project”