Getting started
Routing and controllers
Command line
Databases (SQL)
Databases (NoSQL)
Security
Packages
Learn more
Events
Mako includes two ways of handling events, an event listener and a trait that makes your class observable.
Event listener
The phpregister method lets you register an event handler that will get executed when the event is triggered. You can register multiple handlers for the same event. They will be executed in the order that they were registered.
$this->event->register('foobar', function()
{
return 'foobar event handler';
});
You can also handle your events using a class instead of a closure.
$this->event->register('foobar', 'app\events\FoobarHandler');
Class handlers will be instantiated by the dependency injection container so you can easily inject your dependencies through the constructor. Both closure and class handlers will be executed by the phpContainer::call() and all dependencies will automatically be injected there as well.
<?php
namespace app\events;
use mako\event\EventHandlerInterface;
class FoobarHandler implements EventHandlerInterface
{
public function handle()
{
return 'foobar event handler';
}
}
The phphas method will return TRUE if an event handler has been registered and FALSE if not.
$registered = $this->event->has('foobar');
The phpclear method will clear all event handlers registered for an event.
$this->event->clear('foobar');
The phpoverride method will clear all previously registered handlers for an event before registering a new handler.
$this->event->override('foobar', function()
{
return 'foobar event 1';
});
The phptrigger method run all handlers for the registered event and return an array containing all the return values.
$values = $this->event->trigger('foobar');
You can also pass arguments your handlers using the optional second parameter.
$values = $this->event->trigger('foobar', [1, 2, 3]);
The third optional parameter lets you stop event handling if one of the handlers return phpFALSE.
$values = $this->event->trigger('foobar', [1, 2, 3], true);
Observable trait
Your class must use the phpObservableTrait in order to be obserable.
class Observable
{
use mako\event\ObsevableTrait;
}
$observable = new Observable;
The phpattachObserver method allows you to attach an observer to your class. You can attach multiple observers for the same event.
$observable->attachObserver('foobar', function()
{
return 'foobar event';
});
The phphasObserver method returns TRUE if an event has an observer and FALSE if not.
$observed = $observable->hasObserver('foobar');
The phpclearObservers will clear all observers. You can also specify which event you want to clear.
$observable->clearObservers();
$observable->clearObservers('foobar');
The phpoverrideObservers method clears all observers for an event and registers a new one.
$observable->overrideObservers('foobar', function($string)
{
return mb_strtoupper($string);
});
The phpnotifyObservers method will notify all observers of an event and return an array containing all of their return values.
$returnValues = $this->notifyObservers('foobar', 'hello, world!');
The
phpnotifyObserversmethod isphpprotectedand can only be called from within the observable class.