Event listeners are simple callbacks that run when a specific event is dispatched.
Use the addListener(string $eventName, callable $listener, int $priority = 0) method to add a listener to an event. A simple example appears below:
$api->addListener(StartRequest::NAME,function(StartRequest$event){ // Get properties from event$uri=$event->getUri();$context=$event->getContext(); // Do something at start of data request});
How to add an event subscriber
An event subscriber is a class that can listen to multiple events. Since it's in a class it's easier to re-use code.
You can add an event subscriber which can listen to multiple events via the addSubscriber(EventSubscriberInterface $subscriber) method.
Available event subscribers
LoggerSubscriber
Add logging for the data request process.
StopwatchSubscriber
Add timing profiling for the Symfony Stopwatch profiler (only recommended in development).
use Strata\Data\Http\Rest;
use Strata\Data\Event\Subscriber\LoggerSubscriber;
use Monolog\Logger;
$api = new Rest();
$api->addSubscriber(new LoggerSubscriber(new Logger('/path/to/log')));
use Strata\Data\Http\Rest;
use Strata\Data\Event\Subscriber\StopwatchSubscriber;
use Symfony\Component\Stopwatch\Stopwatch;
$api = new Rest();
$api->addSubscriber(new StopwatchSubscriber(new Stopwatch());