|
| 1 | +# Listener Providers |
| 2 | + |
| 3 | +Listener providers aggregate listeners, and allow a [dispatcher](dispatcher.md) |
| 4 | +to retrieve all listeners capable of listening to the event it is currently |
| 5 | +dispatching. |
| 6 | + |
| 7 | +This library provides several additional provider interfaces, as well as |
| 8 | +implementations, for common listener registration patterns. |
| 9 | + |
| 10 | +## Basic listener attachment |
| 11 | + |
| 12 | +`Phly\EventDispatcher\ListenerProvider\AttachableListenerProviderInterface` |
| 13 | +extends `Psr\EventDispatcher\ListenerProviderInterface` and defines a very basic |
| 14 | +attachment pattern: |
| 15 | + |
| 16 | +```php |
| 17 | +public function listen(string $eventName, callable $listener) : void |
| 18 | +``` |
| 19 | + |
| 20 | +The library provides an implementation via |
| 21 | +`Phly\EventDispatcher\ListenerProvider\AttachableListenerProvider`. |
| 22 | + |
| 23 | +## Positionable attachment |
| 24 | + |
| 25 | +`Phly\EventDispatcher\ListenerProvider\PositionalListenerProviderInterface` |
| 26 | +extends `AttachableListenerProviderInterface`, and adds the following two |
| 27 | +methods: |
| 28 | + |
| 29 | +```php |
| 30 | +public function listenAfter(string $listenerTypeToAppend, string $eventType, callable $newListener) : void; |
| 31 | +public function listenBefore(string $listenerTypeToPrepend, string $eventType, callable $newListener) : void; |
| 32 | +``` |
| 33 | + |
| 34 | +In both cases, the implication is that implementations will append or prepend |
| 35 | +the new listener to the last or first listener that matches the type of the |
| 36 | +first argument. |
| 37 | + |
| 38 | +As an example, |
| 39 | + |
| 40 | +```php |
| 41 | +$provider->listenAfter( |
| 42 | + SendMailListener::class, |
| 43 | + ContactEvent::class, |
| 44 | + lazyListener($container, LoggingListener::class) |
| 45 | +) |
| 46 | +``` |
| 47 | + |
| 48 | +would add a new `LoggingListener` to the provider, to execute after any |
| 49 | +previously registered `SendMailListener` instances. |
| 50 | + |
| 51 | +> See the chapter on [lazy listeners](lazy-listeners.md) for information on the |
| 52 | +> `lazyListener()` function. |
| 53 | +
|
| 54 | +This library does not currently provide any implementations of this interface. |
| 55 | + |
| 56 | +## Prioritized attachment |
| 57 | + |
| 58 | +`Phly\EventDispatcher\ListenerProvider\PrioritizedListenerProviderInterface` |
| 59 | +extends the PSR-14 `ListenerProviderInterface`, and defines a single method for |
| 60 | +attaching listeners: |
| 61 | + |
| 62 | +```php |
| 63 | +public function listen(string $eventType, callable $listener, int $priority = 1) : void; |
| 64 | +``` |
| 65 | + |
| 66 | +Priority values are expected to follow the same behavior as `SplPriorityQueue`: |
| 67 | +larger values should execute first, while negative values should execute last. |
| 68 | +Listeners registered at the same priority should execute in the order in which |
| 69 | +they are attached to the provider. |
| 70 | + |
| 71 | +As an example: |
| 72 | + |
| 73 | +```php |
| 74 | +$provider = new PrioritizedListenerProvider(); |
| 75 | + |
| 76 | +class SomeEvent |
| 77 | +{ |
| 78 | + public $counter = []; |
| 79 | +} |
| 80 | + |
| 81 | +$factory = function (int $index) : callable { |
| 82 | + return function (object $event) use ($index) : void { |
| 83 | + $event->counter[] = $index; |
| 84 | + }; |
| 85 | +}; |
| 86 | + |
| 87 | +$provider->listen(SomeEvent::class, $factory(1)); |
| 88 | +$provider->listen(SomeEvent::class, $factory(2), -100); |
| 89 | +$provider->listen(SomeEvent::class, $factory(3), 100); |
| 90 | + |
| 91 | +$dispatcher = new EventDispatcher($provider); |
| 92 | + |
| 93 | +var_export($dispatcher->dispatch(new SomeEvent())); |
| 94 | +/* |
| 95 | +array ( |
| 96 | + 0 => 3, |
| 97 | + 1 => 1, |
| 98 | + 2 => 2, |
| 99 | +) |
| 100 | +*/ |
| 101 | +``` |
| 102 | + |
| 103 | +This library provides an implementation via the class |
| 104 | +`Phly\EventDispatcher\ListenerProvider\PrioritizedListenerProvider`. |
| 105 | + |
| 106 | +## Randomized attachment |
| 107 | + |
| 108 | +If you do not care what order listeners are called in, and, in fact, want to |
| 109 | +enforce that the order is random, you can use |
| 110 | +`Phly\EventDispatcher\ListenerProvider\RandomizedListenerProvider`. This class |
| 111 | +defines the same `listen()` method as the `AttachableListenerProvider` detailed |
| 112 | +in an earlier section, but has a `getListenersForEvent()` method that randomizes |
| 113 | +the order in which listeners are returned during iteration. |
| 114 | + |
| 115 | +## Reflection-based attachment |
| 116 | + |
| 117 | +Since events are objects, one way to identify if a listener can listen to a |
| 118 | +given event is to _reflect_ on its argument, to see what type it accepts. |
| 119 | +This package defines an interface for providers that can do this, via |
| 120 | +`Phly\EventDispatcher\ListenerProvider\ReflectableListenerProviderInterface`: |
| 121 | + |
| 122 | +```php |
| 123 | +public function listen(callable $listener, string $eventType = null) : void; |
| 124 | +``` |
| 125 | + |
| 126 | +When called with a single argument, implementations are expected to use |
| 127 | +reflection to determine which event type(s) the listener can accept. As an |
| 128 | +example: |
| 129 | + |
| 130 | +```php |
| 131 | +$provider = new ReflectionBasedListenerProvider(); |
| 132 | + |
| 133 | +class SomeEvent |
| 134 | +{ |
| 135 | +} |
| 136 | + |
| 137 | +$listener = function (SomeEvent $event) : void { |
| 138 | + // do something |
| 139 | +}; |
| 140 | + |
| 141 | +$provider->listen($listener); |
| 142 | + |
| 143 | +// This will dispatch $listener: |
| 144 | +$dispatcher->dispatch(new SomeEvent()); |
| 145 | +``` |
| 146 | + |
| 147 | +The package provides an implementation via `Phly\EventDispatcher\ListenerProvider\ReflectionBasedListenerProvider`. |
| 148 | + |
| 149 | +## Provider aggregation |
| 150 | + |
| 151 | +You may want to allow multiple providers in your application, but still have a |
| 152 | +single dispatcher. `Phly\EventDispatcher\ListenerProvider\ListenerProviderAggregate` |
| 153 | +allows aggregating multiple providers into a single one, which will then loop |
| 154 | +through each to yield listeners. |
| 155 | + |
| 156 | +```php |
| 157 | +$nonPrioritized = new AttachableListenerProvider(); |
| 158 | +$prioritized = new PrioritizedListenerProvider(); |
| 159 | +$reflected = new ReflectionBasedListenerProvider(); |
| 160 | + |
| 161 | +$provider = new ListenerProviderAggregate(); |
| 162 | +$provider->attach($nonPrioritized); |
| 163 | +$provider->attach($prioritized); |
| 164 | +$provider->attach($reflected); |
| 165 | + |
| 166 | +$dispatcher = new EventDispatcher($provider); |
| 167 | +``` |
0 commit comments