Facades
August 1, 2013 ยท View on GitHub
The package provides two facades that allow an easy static access to the Event and Queue logic.
Common Interface
The common interface consists of:
- forge: Factory method for the underlying class.
- instance: Create and retrieve a globally accessible instance.
- delete: Delete one or all globally defined instances contained in the facade.
Event Facade
Under the hood the Facade provides access to a Fuel\Event\Container and maps all the available method to that instance. So now you can do:
<?php
use Fuel\Event\Facades\Event;
// Register an event
Event::on('my_event', function(){}})
// Unregister an event
Event::off('my_event');
// Trigger an event
$result = Event::trigger('my_event');
Because the result from the forwarded function is returned you can also chain function:
Event::on('my_event', function(){})
->on('other_event', function());
Queue
<?php
use Fuel\Event\Facades\Queue;
// Add to the queue
Queue::queue('my_event', $payload);
// Add a listener
Queue::on('my_event', $handler);
// Remove all listener from event my_event
Queue::off('my_event');
// Flush the queue
$result = Queue::flush('my_event');
Just like with the Event facade you can chain the calls:
$result = Queue::queue('my_event', $payload)
->on('my_event', $handler)
->flush('my_event');
Named instances
Both Queues and Containers have a multiton implementation. This allows you "namespace" queues and events. This is done by using the ::instance method:
use Fuel\Event\Facades\Event;
use Fuel\Event\Facades\Queue;
// It works for queues
Queue::instance('my_name')->on('event', $handler);
// As well as for events
Event::instance('named')->on('this', $doThat);