Download the PHP package solventt/event-dispatcher without Composer
On this page you can find all versions of the php package solventt/event-dispatcher. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download solventt/event-dispatcher
More information about solventt/event-dispatcher
Files in solventt/event-dispatcher
Package event-dispatcher
Short Description PSR-14 compatible event dispatcher
License BSD-3-Clause
Informations about the package event-dispatcher
Table of Contents
- Installing
- Container Listener Provider
- Classic Listener Provider
- Reflection Listener Provider
- Deferred events
- Listeners constraints according to PSR-14
- Stopped Propagation Events
- Iterators instead of arrays
- Creating listener providers
It's the PSR-14 compatible Event Dispatcher which provides several ways of determining "Event-Listeners" definitions.
NOTE: All the examples below, which include the DI container, are written using the php-di library. But you can utilize any DI container in your code.
Installing
Note: if you are going to use Container Listener Provider you must install any PSR-11 compatible DI Container.
Container Listener Provider
This provider supports only string definitions of listeners. And listeners must implement the method. It allows "lazy" creation of callable listeners while dispatching an event.
Remember:
- the executing order of listeners is as you specified in the DI container definition;
- this provider doesn't support dynamic binding/unbinding of listeners to/from events using the or methods
The "Event-Listeners" definition for the DI container,
The first format:
The second format without specifying events (listeners events will be automatically resolved):
The third mixed format (arrays and strings):
The Event Dispatcher definition for the DI container:
Somewhere in the code:
- the definition name which by default is recognized by the Container Provider. If you want to use a different name you have to pass it to the constructor as a second argument:
Classic Listener Provider
This provider supports only callable definitions of listeners. You can assign listeners to events using:
- a provider constructor
- and/or the method (the method unbinds a listener)
Using:
If you use the DI Container your definition might look like:
Also, you can specify the listener execution order (an integer parameter). The higher the value, the earlier it will be called:
The "Event-Listeners" definition using the method somewhere in the code:
And with the priority:
To unbind a listener from an event use the method:
Reflection Listener Provider
This provider admits only callable definitions of listeners but without specifying events (it will be automatically resolved).
You can assign listeners to events using:
- the provider constructor
- and/or the method (the method unbinds a listener)
Using:
With the listener execution order:
The "Event-Listeners" definition using the method somewhere in the code:
And with the priority:
To unbind a listener from an event use the method:
Deferred events
In some cases you may need to use deferred events.
Listeners constraints according to PSR-14
Quotes from the PSR-14: 1) "A Listener MUST have one and only one parameter, which is the Event to which it responds"; 2) "A Listener SHOULD have a void return, and SHOULD type hint that return explicitly".
You can say that SHOULD not equal MUST. But I can't imagine what use-cases might require returning values from listeners and then use them. Anyway this dispatcher does not implement it.
So in this package you can't specify:
- an empty listener signature;
- more than one parameter in a listener signature;
- any return type besides . Omitted return type is also forbidden
You must provide a type hint of the listener argument - it can be an existent class or the type. Other type-hints are not accepted.
In these cases an exception will be thrown:
But you can switch off these listener constraints. It is necessary to pass as the argument in the constructor of some listener provider:
However, the listeners provided without corresponding events ignores this setting because it requires a type-hinted argument for resolving an event:
Also, does not support switching off the listener constraints.
Stopped Propagation Events
Your events can implement (from PSR-14) to have more control over the listeners execution. For example, we have the following event class:
And there are three listeners:
The listeners definition:
The third listener will not be executed because the method of the returns after the second listener is being executed.
Iterators instead of arrays
Providers also receive iterators in their constructor. But an iterator must implement the interface.
Example 1:
Example 2:
Somewhere in the code:
Creating listener providers
You need to implement the PSR-14 . And if you want your provider to support the methods you also have to implement .