-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathEventDispatcher.php
More file actions
120 lines (102 loc) · 3.5 KB
/
EventDispatcher.php
File metadata and controls
120 lines (102 loc) · 3.5 KB
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
<?php
declare(strict_types=1);
namespace PhpSchool\PhpWorkshop\Event;
use PhpSchool\PhpWorkshop\Listener\LazyContainerListener;
use PhpSchool\PhpWorkshop\Result\ResultInterface;
use PhpSchool\PhpWorkshop\ResultAggregator;
/**
* An event dispatcher implementation.
*/
class EventDispatcher
{
/**
* @var array<string, array<callable>>
*/
private array $listeners = [];
/**
* @var ResultAggregator
*/
private ResultAggregator $resultAggregator;
public function __construct(ResultAggregator $resultAggregator)
{
$this->resultAggregator = $resultAggregator;
}
/**
* Dispatch an event. Can be any event object which implements `PhpSchool\PhpWorkshop\Event\EventInterface`.
*/
public function dispatch(EventInterface $event): EventInterface
{
if (array_key_exists($event->getName(), $this->listeners)) {
foreach ($this->listeners[$event->getName()] as $listener) {
$listener($event);
}
}
return $event;
}
/**
* Attach a callback to an event name. `$eventNames` can be an array of event names in order to attach the same
* callback to multiple events or it can just be one event name as a string.
*
* @param string|array<string> $eventNames
* @param callable $callback
*/
public function listen($eventNames, callable $callback): void
{
if (!is_array($eventNames)) {
$eventNames = [$eventNames];
}
foreach ($eventNames as $eventName) {
$this->attachListener($eventName, $callback);
}
}
/**
* @param string $eventName
* @param callable $callback
*/
private function attachListener(string $eventName, callable $callback): void
{
if (!array_key_exists($eventName, $this->listeners)) {
$this->listeners[$eventName] = [$callback];
} else {
$this->listeners[$eventName][] = $callback;
}
}
public function removeListener(string $eventName, callable $callback): void
{
foreach ($this->listeners[$eventName] ?? [] as $key => $listener) {
if ($listener instanceof LazyContainerListener) {
$listener = $listener->getWrapped();
}
if ($listener === $callback) {
unset($this->listeners[$eventName][$key]);
$this->listeners[$eventName] = array_values($this->listeners[$eventName]);
if (empty($this->listeners[$eventName])) {
unset($this->listeners[$eventName]);
}
break;
}
}
}
/**
* Insert a verifier callback which will execute at the given event name much like normal listeners.
* A verifier should return an object which implements `PhpSchool\PhpWorkshop\Result\FailureInterface`
* or `PhpSchool\PhpWorkshop\Result\SuccessInterface`. This result object will be added to the result aggregator.
*/
public function insertVerifier(string $eventName, callable $verifier): void
{
$this->attachListener($eventName, function (EventInterface $event) use ($verifier) {
$result = $verifier($event);
//return type hints pls
if ($result instanceof ResultInterface) {
$this->resultAggregator->add($result);
}
});
}
/**
* @return array<string, array<callable>>
*/
public function getListeners(): array
{
return $this->listeners;
}
}