One feature that ships with version 1.1 of Leafpub are plugins. Plugins need entry points to extend or edit Leafpub's default behavior.

This is where Leafpub events come into play.

Where are the events located?

Leafpub uses Symfony's Event Dispatcher to implement the event system.
All events are located at source/events.

The LeafpubEvent

  • A Leafpub event extends the abstract class LeafpubEvent. 
  • The event has a static property NAME which identifies the event (post.beforeRender for example).
  • The event has two public functions getEventData and setEventData
    • getEventData returns the event data, if present. It is possible to edit those data and
    • set your edited data with setEventData and all others receive your edited data

To cancel the propagation of this event to other listeners, call stopPropagation()

Subscribe to events

"But how do I subscribe/listen to those events?" you might ask.

To subscribe to a Leafpub event just call the static function Leafpub::on($eventName, $callback, [optional $priority]).

This method needs two paramters:

  • the event, you want to listen to
  • your callable callback.
  • the optional third parameter sets the priority of your callback. Priority is set to 0 per default.

 Unsubscribe

Unsubscribe from events is as simple as subscribing. Just call Leafpub::off($eventname, $callback) and you're done.

Example

private static function _registerCoreListener(){
// Add Application Listener
$appListener = new Listeners\Application();
self::on(Events\Application\Startup::NAME, array($appListener, 'onApplicationStartup'));

self::on(Events\Application\MailSend::NAME, array('sendMail'), 999);
// Add Post Listener
$postListener = new Listeners\Post();
self::on(Events\Post\Add::NAME, array($postListener, 'onPostAdd'));
self::on(Events\Post\Added::NAME, array($postListener, 'onPostAdded'));
self::on(Events\Post\BeforeRender::NAME, array($postListener, 'onBeforeRender'));
}