A new Event API

By Adam Patterson in: Announcements Code


Here is an overview of the event API in Tentacle, One thing that was really important to use was the ability to chain methods giving us the ability to filter and process a chain of events.

At the moment there are a number of hooks and triggers throughout the system responsible for triggering actions in the site.

Registering an event:

function method_one (){
echo 'one ';
}
function method_two (){
echo 'two ';
}
event::on('event_trigger', 'method_one', 2);
event::on('event_trigger', 'method_two', 1);
event::trigger('event_trigger');

Turning events off:

event::off('event_trigger');
event::off('event_trigger', 'method_two');
event::off(null, 'method_one');

Test if an event exists:

var_dump(event::exists('event_trigger'));

boolean true

Trigger an event:

function method_name ( )
{
echo 'my method name';
}
event::on('event_name', 'method_name');
event::trigger('event_name');

my method name

Trigger an event and pass data to it:

function method_data ( $text = '' )
{
echo ' 1  my method data is '.$text;
}
function method_data_two ( $text = '' )
{
echo ' 2 my method data is '.$text;
}
event::on('event_data', 'method_data', 1);
event::on('event_data', 'method_data_two', 2);
event::trigger('event_data', 'this');

1 my method data is this 2 my method data is this

Triggering a class and passing data to the method

class my
{
static function method_name ( $text = '' )
{
echo 'my class method name is '.$text;
}
}
event::on('event_class', 'my::method_name');
event::trigger('event_class', 'Lary');

my class method name is Lary

Event chaining

function method_sad ( $text = '' )
{
return str_replace('blah', "sad", $text);
}
function method_happy ( $text = '' )
{
return str_replace('sad', "happy", $text);
}
event::on('event_mood', 'method_sad', 1);
event::on('event_mood', 'method_happy', 2);
echo event::filter('event_mood', 'I am blah!');

I am happy!