1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* event-symfony-event-dispatcher (https://github.com/phpgears/event-symfony-event-dispatcher). |
5
|
|
|
* Event bus with Symfony Event Dispatcher. |
6
|
|
|
* |
7
|
|
|
* @license MIT |
8
|
|
|
* @link https://github.com/phpgears/event-symfony-event-dispatcher |
9
|
|
|
* @author Julián Gutiérrez <[email protected]> |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Gears\Event\Symfony\Dispatcher; |
15
|
|
|
|
16
|
|
|
use Gears\Event\EventHandler; |
17
|
|
|
use Symfony\Component\EventDispatcher\Event as SymfonyEvent; |
18
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcher as SymfonyEventDispatcher; |
19
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
20
|
|
|
|
21
|
|
|
class Dispatcher extends SymfonyEventDispatcher implements EventDispatcher |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* ContainerAwareEventDispatcher constructor. |
25
|
|
|
* |
26
|
|
|
* @param array<string, mixed> $listenersMap |
27
|
|
|
*/ |
28
|
|
|
public function __construct(array $listenersMap = []) |
29
|
|
|
{ |
30
|
|
|
foreach ($listenersMap as $eventName => $listeners) { |
31
|
|
|
if (!\is_array($listeners)) { |
32
|
|
|
$listeners = [$listeners]; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
foreach ($listeners as $listener) { |
36
|
|
|
$this->addListener($eventName, $listener); |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* {@inheritdoc} |
43
|
|
|
*/ |
44
|
|
|
public function addSubscriber(EventSubscriberInterface $subscriber): void |
45
|
|
|
{ |
46
|
|
|
foreach ($subscriber::getSubscribedEvents() as $eventName => $params) { |
47
|
|
|
if (!\is_array($params)) { |
48
|
|
|
$params = [$params]; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
foreach ($params as $listener) { |
52
|
|
|
if (!\is_array($listener)) { |
53
|
|
|
$this->addListener($eventName, $listener); |
54
|
|
|
} else { |
55
|
|
|
$this->addListener($eventName, $listener[0], $listener[1] ?? 0); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Adds an event listener that listens on the specified events. |
63
|
|
|
* |
64
|
|
|
* @param string $eventName |
65
|
|
|
* @param mixed $listener |
66
|
|
|
* @param mixed $priority |
|
|
|
|
67
|
|
|
*/ |
68
|
|
|
public function addListener($eventName, $listener, $priority = 0): void |
69
|
|
|
{ |
70
|
|
|
if (!$listener instanceof EventHandler) { |
71
|
|
|
throw new \InvalidArgumentException(\sprintf( |
72
|
|
|
'Event handler must be an instance of %s, %s given', |
73
|
|
|
EventHandler::class, |
74
|
|
|
\is_object($listener) ? \get_class($listener) : \gettype($listener) |
75
|
|
|
)); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
parent::addListener($eventName, $listener, (int) $priority); |
|
|
|
|
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* {@inheritdoc} |
83
|
|
|
*/ |
84
|
|
|
public function dispatch($eventName, SymfonyEvent $event = null): SymfonyEvent |
85
|
|
|
{ |
86
|
|
|
if ($event === null) { |
87
|
|
|
throw new \InvalidArgumentException('Dispatched event cannot be empty'); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
if (!$event instanceof EventEnvelope) { |
91
|
|
|
throw new \InvalidArgumentException(\sprintf( |
92
|
|
|
'Dispatched event must implement %s, %s given', |
93
|
|
|
EventEnvelope::class, |
94
|
|
|
\get_class($event) |
95
|
|
|
)); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$this->dispatchEvent($this->getListeners($eventName), $event); |
99
|
|
|
|
100
|
|
|
return $event; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Dispatch event to registered listeners. |
105
|
|
|
* |
106
|
|
|
* @param EventHandler[] $listeners |
107
|
|
|
* @param EventEnvelope $event |
108
|
|
|
*/ |
109
|
|
|
private function dispatchEvent(array $listeners, EventEnvelope $event): void |
110
|
|
|
{ |
111
|
|
|
$dispatchEvent = $event->getWrappedEvent(); |
112
|
|
|
|
113
|
|
|
foreach ($listeners as $handler) { |
114
|
|
|
$handler->handle($dispatchEvent); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|