DariusIII /
tmdb-laravel
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * @package php-tmdb\laravel |
||
| 4 | * @author Mark Redeman <[email protected]> |
||
| 5 | * @copyright (c) 2014, Mark Redeman |
||
| 6 | */ |
||
| 7 | namespace Tmdb\Laravel\Adapters; |
||
| 8 | |||
| 9 | use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
||
| 10 | use Symfony\Component\EventDispatcher\EventDispatcherInterface as SymfonyDispatcher; |
||
| 11 | |||
| 12 | /** |
||
| 13 | * This adapter provides a Laravel integration for applications |
||
| 14 | * using the Symfony EventDispatcherInterface |
||
| 15 | * It passes any request on to a Symfony Dispatcher and only |
||
| 16 | * uses the Laravel Dispatcher when dispatching events |
||
| 17 | */ |
||
| 18 | abstract class EventDispatcherAdapter implements SymfonyDispatcher |
||
| 19 | {
|
||
| 20 | |||
| 21 | /** |
||
| 22 | * The Laravel Events Dispatcher |
||
| 23 | * @var \Illuminate\Contracts\Events\Dispatcher | \Illuminate\Events\Dispatcher |
||
| 24 | */ |
||
| 25 | protected $laravelDispatcher; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * The Symfony Event Dispatcher |
||
| 29 | * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface |
||
| 30 | */ |
||
| 31 | protected $symfonyDispatcher; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Dispatches an event to all registered listeners. |
||
| 35 | * |
||
| 36 | * @param object|null $event The event to pass to the event handlers/listeners. |
||
| 37 | * If not supplied, an empty Event instance is created. |
||
| 38 | * |
||
| 39 | * |
||
| 40 | * @param string|null $eventName The name of the event to dispatch. The name of |
||
| 41 | * the event is the name of the method that is |
||
| 42 | * invoked on listeners. |
||
| 43 | * @return object |
||
| 44 | * @api |
||
| 45 | */ |
||
| 46 | public function dispatch(object $event = null, string $eventName = null) : object |
||
| 47 | {
|
||
| 48 | $this->laravelDispatcher->dispatch($event, $eventName); |
||
| 49 | $this->symfonyDispatcher->dispatch($event, $eventName); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 50 | |||
| 51 | return $event; |
||
|
0 ignored issues
–
show
|
|||
| 52 | } |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Adds an event listener that listens on the specified events. |
||
| 56 | * |
||
| 57 | * @param string $eventName The event to listen on |
||
| 58 | * @param callable $listener The listener |
||
| 59 | * @param int $priority The higher this value, the earlier an event |
||
| 60 | * listener will be triggered in the chain (defaults to 0) |
||
| 61 | * |
||
| 62 | * @api |
||
| 63 | */ |
||
| 64 | public function addListener($eventName, $listener, $priority = 0) |
||
| 65 | {
|
||
| 66 | $this->symfonyDispatcher->addListener($eventName, $listener, $priority); |
||
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Adds an event subscriber. |
||
| 71 | * |
||
| 72 | * The subscriber is asked for all the events he is |
||
| 73 | * interested in and added as a listener for these events. |
||
| 74 | * |
||
| 75 | * @param EventSubscriberInterface $subscriber The subscriber. |
||
| 76 | * |
||
| 77 | * @api |
||
| 78 | */ |
||
| 79 | public function addSubscriber(EventSubscriberInterface $subscriber) |
||
| 80 | {
|
||
| 81 | $this->symfonyDispatcher->addSubscriber($subscriber); |
||
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Removes an event listener from the specified events. |
||
| 86 | * |
||
| 87 | * @param string $eventName The event to remove a listener from |
||
| 88 | * @param callable $listenerToBeRemoved The listener to remove |
||
| 89 | */ |
||
| 90 | public function removeListener($eventName, $listenerToBeRemoved) |
||
| 91 | {
|
||
| 92 | $this->symfonyDispatcher->removeListener($eventName, $listenerToBeRemoved); |
||
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Removes an event subscriber. |
||
| 97 | * |
||
| 98 | * @param EventSubscriberInterface $subscriber The subscriber |
||
| 99 | */ |
||
| 100 | public function removeSubscriber(EventSubscriberInterface $subscriber) |
||
| 101 | {
|
||
| 102 | $this->symfonyDispatcher->removeSubscriber($subscriber); |
||
| 103 | } |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Gets the listeners of a specific event or all listeners. |
||
| 107 | * |
||
| 108 | * @param string $eventName The name of the event |
||
| 109 | * |
||
| 110 | * @return array The event listeners for the specified event, or all event listeners by event name |
||
| 111 | */ |
||
| 112 | public function getListeners(string $eventName = null): array |
||
| 113 | {
|
||
| 114 | return $this->symfonyDispatcher->getListeners($eventName); |
||
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Checks whether an event has any registered listeners. |
||
| 119 | * |
||
| 120 | * @param string $eventName The name of the event |
||
| 121 | * |
||
| 122 | * @return bool true if the specified event has any listeners, false otherwise |
||
| 123 | */ |
||
| 124 | public function hasListeners(string $eventName = null): bool |
||
| 125 | {
|
||
| 126 | return ($this->symfonyDispatcher->hasListeners($eventName) || |
||
| 127 | $this->laravelDispatcher->hasListeners($eventName)); |
||
| 128 | } |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Gets the listener priority for a specific event. |
||
| 132 | * |
||
| 133 | * Returns null if the event or the listener does not exist. |
||
| 134 | * |
||
| 135 | * @param string $eventName The name of the event |
||
| 136 | * @param callable $listener The listener |
||
| 137 | * |
||
| 138 | * @return int|null The event listener priority |
||
| 139 | */ |
||
| 140 | public function getListenerPriority($eventName, $listener): ?int |
||
| 141 | {
|
||
| 142 | return $this->symfonyDispatcher->getListenerPriority($eventName, $listener); |
||
| 143 | } |
||
| 144 | } |
||
| 145 |