Download the PHP package koriit/eventdispatcher without Composer
On this page you can find all versions of the php package koriit/eventdispatcher. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download koriit/eventdispatcher
More information about koriit/eventdispatcher
Files in koriit/eventdispatcher
Package eventdispatcher
Short Description Simple event dispatcher based on PHP-DI
License MIT
Homepage https://github.com/Koriit/EventDispatcher
Informations about the package eventdispatcher
EventDispatcher
Simple event dispatcher based on PHP-DI.
This library does not aim to be general purpose library or to cover all possible use cases. What this library does aim to be is perfect choice for those who use PHP-DI and prefer to use PHP Definitions.
The goal is to create as decoupled code as possible. The code that uses the dispatcher may not know its listeners, and the other way around, the listeners may not even know that they are actually used as listeners!
Install
EventDispatcher is available via composer:
Versions
This library depends on PHP-DI and is thus affected by compatibility breaks coming from it.
v2
Tested with PHP-DI ^6.0.
v1
Tested with PHP-DI ^5.4.
Usage
You are encouraged to familiarize yourself with Koriit\EventDispatcher\EventDispatcherInterface
and
Koriit\EventDispatcher\EventContextInterface
as those two interfaces are everything you need to work
with this library.
Basic example:
Naturally since we are using PHP-DI then we would create a definition for
Koriit\EventDispatcher\EventDispatcherInterface
.
A listener may be anything that can be invoked by PHP-DI:
Even interfaces:
For above example to work you need to configure a definition for MyInterface, of course.
Warning:
Event dispatcher doesn't work well with listeners which implement fluent interface or allow for
method chaining. For more information, read about stopping dispatchemnt chain below.
Adding listeners
There are 2 ways to subscribe a listener. In both cases you have to specify name of the event and calling priority. The higher the priority value the later the listener will be called. Listeners with the same priority will be called in the order they have been subscribed. You can entirely omit priority parameter as it defaults to 0.
addListener
First, by using addListener
method on Koriit\EventDispatcher\EventDispatcher
object.
addListeners
Second, by using addListeners
method on Koriit\EventDispatcher\EventDispatcher
object.
Listeners array is a simple structure of 3 levels:
- At first level it is associative array where keys are names of registered events
- At second level it is indexed array where keys are priority values
- At third level it is simple list containing listeners subscribed to given event with given priority
Example:
Dispatchment
Dispatchment is a simple process of invoking all listeners subscribed to dispatched event.
Stopping dispatchment
If any listener in the dispatchment chain returns a value that can be evaluated as true,
the dispachment is stopped and all the remaining listeners are skipped. You can also achieve this by
calling stop
on event context.
While this design simplifies the process and does not require wiring listeners with event dispatcher,
it makes it problematic to work with listeners that return values which cannot be interpreted as
success or failure. This especially holds true for methods which allow for method chaining or implement
fluent interface. To work around this problem you can use stop
and ignoreReturnValue
methods on
event context, though, that requires wiring your listener with event context.
Everything is a trade-off, someone once said.
Context
Event context is simple data object holding information about the dispatchment process.
See Koriit\EventDispatcher\EventContextInterface
for more information.
Parameters
You can pass additional parameters to be used by invoker while injecting listener arguments by name.
eventName, eventContext, eventDispatcher
There are 3 parameters injected by event dispatcher itself:
- eventName - name of the event dispatched
- eventContext - reference to the context object of current dispatchment
- eventDispatcher - reference to the event dispatcher itself; this allows for nested dispatchments
You cannot override them as using those keys in parameters array causes exception.