dev-master
9999999-devPSR-14 event emitter
BSD-3-Clause
The Requires
- php ^7.1
- psr/event-dispatcher dev-feature/mwop-eliminate-cruft as 0.2.0
- zendframework/zend-stdlib ^3.2
The Development Requires
event psr-14
PSR-14 event emitter
Experimental!
This library is experimental, tracking different iterations and experiments being proposed for PSR-14. It is highly unstable in terms of API; use at your own risk., (*2)
This library provides an implementation of the following proposed PSR-14 interfaces:, (*3)
ListenerProvider
implements ListenerProviderInterface
, and allows you to
attach listeners to any message type. It then acts as a generator, looping
through each listener and testing if it handles the message type., (*4)
PrioritizedListenerProvider
also implements ListenerProviderInterface
,
and allows you to attach listeners to any message type, with an integer
priority. When listeners are retrieved, it loops through all attached
listeners, and injects those capable of listening to the emitted message to a
priority queue, which it then returns., (*5)
MessageNotifier
implements MessageNotifierInterface
, and accepts a
ListenerProviderInterface
to its constructor. It then loops through
and notifies listeners returned for the message. If any listeners throw
exceptions, it catches them, and, when all listeners have been notified,
throws a Phly\EventEmitter\Exception\ExceptionAggregate
that aggregates all
of them; call the getListenerExceptions()
method of that class to iterate
through them., (*6)
TaskProcessor
implements TaskProcessorInterface
, and accepts a
ListenerProviderInterface
to its constructor. It then loops through
and processes listeners returned for the task, halting early if the
task is stoppable and indicates propagation has been stopped. Exceptions
thrown by listeners are not caught., (*7)
It DOES NOT provide implementations for the following interfaces:, (*8)
EventInterface
(consumers will create these)MessageInterface
(consumers will create these)TaskInterface
(consumers will create these)StoppableTaskInterface
(consumers will create these)You will first need to add a repository entry to your composer.json
:, (*9)
"repositories": [ { "type": "vcs", "url": "https://github.com/phly/phly-event-emitter.git" } ],
Then, run the following to install this library:, (*10)
$ composer require phly/phly-event-emitter
The following demonstrates using the ListenerProvider
to attach a listener.
The provider is then used to seed either a MessageNotifier
or TaskProcessor
., (*11)
use Phly\EventEmitter\MessageNotifier; use Phly\EventEmitter\ListenerProvider; $listeners = new ListenerProvider(); $listeners->on(BootstrapEvent::class, function ($e) { // do something with the bootstrap event }); $notifier = new MessageNotifier($listeners); $notifier->notify(new BootstrapEvent($params));
The following example uses a PrioritizedListenerProvider
to provide three
different listeners, each with a different priority. Priorities are integers;
higher priorities execute first, while lower priorities (including negative
priorities) execute last., (*12)
use Phly\EventEmitter\TaskProcessor; use Phly\EventEmitter\PrioritizedListenerProvider; $listeners = new PrioritizedListenerProvider(); $listeners->on(BootstrapTask::class, function ($e) { echo 1, PHP_EOL; }, -100); $listeners->on(BootstrapTask::class, function ($e) { echo 2, PHP_EOL; }, 100); $listeners->on(BootstrapTask::class, function ($e) { echo 3, PHP_EOL; }, 1); $processor = new TaskProcessor($listeners); $processor->process(new BootstrapTask($params));
In the above, the output will become:, (*13)
2 3 1
PSR-14 event emitter
BSD-3-Clause
event psr-14