Application Event System

Event Emitter Interface

Application can emit events and bind listeners to them.


Accessing event manager service.

/** @var \WebinoEventLib\EventManager $events */
$events = $app->getEvents();


Binding a listener to an event.

use WebinoAppLib\Event\AppEvent;

// creating a closure listener
$handler = function (AppEvent $event) {
    // do something...

// attaching a closure listener to a custom event
$app->bind('myEvent', $handler);

// attaching a closure listener to a dispatch event
$app->bind(AppEvent::DISPATCH, $handler);

// attaching an invokable class
$app->bind(AppEvent::DISPATCH, MyListener::class);

// attaching an invokable object
$app->bind(AppEvent::DISPATCH, new MyListener);

// specifying a priority
$app->bind(AppEvent::DISPATCH, $handler, AppEvent::BEGIN);

// attaching a listener aggregate class

// attaching a listener aggregate object
$app->bind(new MyListenerAggregate);


Unbinding a listener from an event.

// for all events and priorities

// for a specific event
$app->unbind('someEvent', $handler);

// for a specific priority too
$app->unbind('someEvent', $handler, $priority);


Emitting an event.

// just emit an event

// event with custom arguments
$app->emit('myEvent', [$argOne, $argTwo]);

// creating an event callback
$callback = function ($result)
    // do something...

// event with callback
$app->emit('myEvent', $callback);

// event with custom arguments and callback
$app->emit('myEvent', [$argOne, $argTwo], $callback);

// custom event object
$app->emit(new MyEvent);

Events Config


Configuring listeners.

use WebinoAppLib\Feature\Listener;

    // invokable
    new Listener(MyExampleListener::class),
    // factory
    new Listener(MyExampleListener::class, MyExampleListenerFactory::class),


Configuring core listeners.

use WebinoAppLib\Feature\CoreListener;

    // invokable
    new CoreListener(MyExampleCoreListener::class),
    // factory
    new CoreListener(MyExampleCoreListener::class, MyExampleCoreListenerFactory::class),

Application Core Events

Following events are emitted during an application core lifecycle.


Configuring an application, merging modules configurations.

This event is emitted in the middle of the two pass bootstrap event, allowing you to merge an application configuration.


Only core listeners can bind to this event.

use WebinoAppLib\Event\AppEvent;

$app->bind(AppEvent::CONFIGURE, function (AppEvent $event) {});


Initializing an application, all the services will be ready.

After this two pass event all the services should be ready. The core listeners binds to the first pass and a remaining can listen to the second pass bootstrap event.

use WebinoAppLib\Event\AppEvent;

$app->bind(AppEvent::BOOTSTRAP, function (AppEvent $event) {});


Handling the client request and sending a response.

This event is triggered to handle the server client request.

use WebinoAppLib\Event\AppEvent;
use WebinoAppLib\Event\DispatchEvent;

$app->bind(AppEvent::DISPATCH, function (DispatchEvent $event) {});

Dispatch Event

Following methods are provided by the dispatch event object.


Obtaining application instance.

/** @var \WebinoAppLib\Event\DispatchEvent $event */
/** @var \WebinoAppLib\Application\AbstractApplication $app */
$app = $event->getApp();


Obtaining request instance.

/** @var \WebinoAppLib\Event\DispatchEvent $event */
/** @var \Zend\Stdlib\RequestInterface $request */
$request = $event->getRequest();


Obtaining response instance.

/** @var \WebinoAppLib\Event\DispatchEvent $event */
/** @var \Zend\Stdlib\ResponseInterface $response */
$response = $event->getResponse();


Setting a response instance or appending text response to existing one.

/** @var \WebinoAppLib\Event\DispatchEvent $event */

// setting a response instance
/** @var \Zend\Stdlib\ResponseInterface $response */
// string response will be appended to existing output
$event->setResponse('Example response content.');
// array response will be joined to string and appended to existing output
$event->setResponse(['Example ', 'response ', 'content.']);

Setting a response stream.

use WebinoAppLib\Response\StreamResponse;

/** @var \WebinoAppLib\Event\DispatchEvent $event */
// preview
$event->setResponse(new StreamResponse('path/to/file.txt'));
// download
$event->setResponse((new StreamResponse('path/to/file.txt'))->setForceDownload());


Resetting response text, clearing the existing one.

/** @var \WebinoAppLib\Event\DispatchEvent $event */
// set new string response to the output
$event->resetResponse('Example response content.');
// array response will be joined to string
$event->resetResponse(['Example ', 'response ', 'content.']);

Event Listener Priority

When attaching a listener to an event the priority integer could be specified. Positive number is a higher priority than a negative one. If you do not provide any priority to a listener, it will be invoked as soon after the main action triggers.

Event provides some constants for priorities:


Handled at the beginning of an event.

use WebinoEventLib\Event;

$app->bind('myEvent', function (Event $event) {}, Event::BEGIN);


Handled before main event.

use WebinoEventLib\Event;

$app->bind('myEvent', function (Event $event) {}, Event::BEFORE);


Handled after main event.

use WebinoEventLib\Event;

$app->bind('myEvent', function (Event $event) {}, Event::AFTER);


Handled at the end of an event.

use WebinoEventLib\Event;

$app->bind('myEvent', function (Event $event) {}, Event::FINISH);

Fine Tuning The Priority

You can always fine-tune your listener priority by adding (earlier) or substracting (later) an integer.

use WebinoEventLib\Event;

// earlier
$app->bind('myEvent', function (Event $event) {}, Event::BEGIN + 100);

// later
$app->bind('myEvent', function (Event $event) {}, Event::BEGIN - 100);