-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDispatcher.php
172 lines (155 loc) · 4.98 KB
/
Dispatcher.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<?php
/**
* Qubus\EventDispatcher
*
* @link https://github.com/QubusPHP/event-dispatcher
* @copyright 2020 Joshua Parker <joshua@joshuaparker.dev>
* @copyright 2018 Filip Štamcar (original author Tor Morten Jensen)
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
declare(strict_types=1);
namespace Qubus\EventDispatcher;
use Qubus\Exception\Data\TypeException;
use function array_merge;
use function call_user_func;
use function is_array;
use function is_callable;
use function is_string;
class Dispatcher implements EventDispatcher
{
/**
* Array of listeners.
*
* @var ListenerPriorityQueue[] $listeners
*/
protected array $listeners = [];
/**
* {@inheritdoc}
*/
public function dispatch($eventName, ?Event $event = null): void
{
if ($eventName instanceof Event) {
$event = $eventName;
} elseif (null === $event) {
$event = new GenericEvent($eventName, null);
}
if (isset($this->listeners[$event->getName()])) {
foreach ($this->listeners[$event->getName()] as $listener) {
if ($event->isPropagationStopped()) {
break;
}
call_user_func([$listener, 'handle'], $event);
}
}
}
/**
* {@inheritdoc}
* @throws TypeException
*/
public function addListener(string $eventName, $listener, int $priority = self::PRIORITY_DEFAULT): void
{
if (! isset($this->listeners[$eventName])) {
$this->listeners[$eventName] = new ListenerPriorityQueue();
}
if (is_callable($listener)) {
$listener = CallableListener::createFromCallable($listener);
}
if (! $listener instanceof EventListener) {
throw new TypeException(
'The listener should be an implementation of EventListener or a callable.'
);
}
$this->listeners[$eventName]->insert($listener, $priority);
}
/**
* {@inheritdoc}
* @throws TypeException
*/
public function addSubscriber(EventSubscriber $subscriber): void
{
foreach ($subscriber->getSubscribedEvents() as $eventName => $parameters) {
if (is_string($parameters)) {
$this->addListener($eventName, [$subscriber, $parameters]);
} elseif (is_string($parameters[0])) {
$this->addListener($eventName, [$subscriber, $parameters[0]], $parameters[1] ?? 0);
} else {
foreach ($parameters as $listener) {
$this->addListener($eventName, [$subscriber, $listener[0]], $listener[1] ?? 0);
}
}
}
}
/**
* {@inheritdoc}
* @throws TypeException
*/
public function removeListener(string $eventName, $listener): void
{
if (empty($this->listeners[$eventName])) {
return;
}
if (is_callable($listener) && false === ($listener = CallableListener::findByCallable($listener))) {
return;
}
$this->listeners[$eventName]->detach($listener);
}
/**
* {@inheritdoc}
* @throws TypeException
*/
public function removeSubscriber(EventSubscriber $subscriber): void
{
foreach ($subscriber->getSubscribedEvents() as $eventName => $parameters) {
if (is_array($parameters) && is_array($parameters[0])) {
foreach ($parameters as $listener) {
$this->removeListener($eventName, [$subscriber, $listener[0]]);
}
} else {
$this->removeListener($eventName, [$subscriber, is_string($parameters) ? $parameters : $parameters[0]]);
}
}
}
/**
* {@inheritdoc}
*/
public function removeAllListeners(?string $eventName = null): void
{
if (null !== $eventName && isset($this->listeners[$eventName])) {
$this->listeners[$eventName]->clear();
} else {
foreach ($this->listeners as $queue) {
$queue->clear();
}
}
}
/**
* {@inheritdoc}
* @throws TypeException
*/
public function hasListener(string $eventName, $listener): bool
{
if (! isset($this->listeners[$eventName])) {
return false;
}
if (is_callable($listener)) {
$listener = CallableListener::findByCallable($listener);
}
return $this->listeners[$eventName]->contains($listener);
}
/**
* {@inheritdoc}
*/
public function getListeners(?string $eventName = null): array
{
if (null !== $eventName) {
return isset($this->listeners[$eventName]) ?
$this->listeners[$eventName]->all() : [];
} else {
$listeners = [];
foreach ($this->listeners as $queue) {
$listeners = array_merge($listeners, $queue->all());
}
return $listeners;
}
}
}