Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: 1.0.x/develop

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 classes
Octocat-spinner-32 README.md
README.md

Kohana Event Dispatcher

A simple events-system for Kohana 3.x.

  • Author: Jeremy Lindblom (jeremeamia)
  • Version: 0.1
  • Compatible Kohana Version(s): 3.1.x

Disclaimer

Currently untested. Not for production use.

Sample Usage

In this example, the event dispatcher will be used to implement part of a user rewards system. In this case, a Rewarder object listens for an event signifying the someone has accepted a Referral to the application. The rewarder then rewards the referrer a specific amount of points.

Instantiation

$dispatcher = Event_Dispatcher::factory()
    ->load_subscribers();

Triggering an Event

class Referral {
    protected $_dispatcher;
    /* ... */

    public function __construct(/* ... */, Interface_Dispatcher $dispatcher)
    {
        /* ... */
        $this->_dispatcher = $dispatcher;
    }

    /* ... */

    public function accept()
    {
        /* ... */
        $this->_dispatcher->trigger('referral.accepted', new Event($this));
        /* ... */
    }
}

Handling the Event

class Subscriber_Rewarder implements Interface_Subscriber {
    public function get_listeners()
    {
        return array(
            'referral.accepted' => array($this, 'referral'),
        );
    }

    public function referral(Interface_Event $event)
    {
        $referral = $event->get_subject();
        if ($referral instanceof Referral)
        {
            $points = Kohana::config('rewards.points.referral');
            $referral->referrer->reward_points($points);
        }
    }
}
Something went wrong with that request. Please try again.