-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathEvent.php
More file actions
219 lines (189 loc) · 5.28 KB
/
Copy pathEvent.php
File metadata and controls
219 lines (189 loc) · 5.28 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
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
<?php
declare(strict_types=1);
namespace Bow\Event;
use Bow\Event\Contracts\AppEvent;
use ErrorException;
use RuntimeException;
/**
* Class Event
*
* @package Bow\Event
* @method static void on(string $event, callable|string $fn, int $priority = 0)
* @method static void once(string $event, callable|array|string $fn, int $priority = 0)
* @method static ?bool emit(string|AppEvent $event)
* @method static void off(string $event)
* @method static ?bool dispatch(string|AppEvent $event)
*/
class Event
{
/**
* The event collector
*
* @var array
*/
private static array $events = [];
/**
* The Event instance
*
* @var ?Event
*/
private static ?Event $instance = null;
/**
* Event constructor.
*
* @throws \Exception
*/
public function __construct()
{
if (static::$instance != null) {
throw new \Exception("The Event class is a singleton and already instantiated. Please use Event::getInstance() to get the instance.");
}
}
/**
* Event constructor.
*
* @return Event
*/
public static function getInstance(): Event
{
if (static::$instance == null) {
static::$instance = new Event();
}
return static::$instance;
}
/**
* addEventListener
*
* @param string $event
* @param callable|string $fn
* @param int $priority
*/
public function on(string $event, callable|string $fn, int $priority = 0): void
{
if (!static::bound($event)) {
static::$events[$event] = [];
}
static::$events[$event][] = new Listener($fn, $priority);
uasort(
static::$events[$event],
function (Listener $first_listener, Listener $second_listener) {
return $second_listener->getPriority() <=> $first_listener->getPriority();
}
);
}
/**
* Alias to on method
*
* @param string $event
* @param callable|string $fn
* @param int $priority
*/
public function listener(string $event, callable|string $fn, int $priority = 0): void
{
$this->on($event, $fn, $priority);
}
/**
* Check whether an event is already recorded at least once.
*
* @param string|AppEvent $event
* @return bool
*/
public function bound(string|AppEvent $event): bool
{
$onces = static::$events['__bow.once.event'] ?? [];
return array_key_exists($event, static::$events) ||
array_key_exists($event, $onces);
}
/**
* Associate a single listener to an event
*
* @param string $event
* @param callable|array|string $fn
* @param int $priority
*/
public function once(string $event, callable|array|string $fn, int $priority = 0): void
{
static::$events['__bow.once.event'][$event] = new Listener($fn, $priority);
}
/**
* Get the one-time listener for an event
*
* @param string $event
* @return Array<Listener>
*/
public function getEventListeners(string $event_name): array
{
$once_event = static::$events['__bow.once.event'][$event_name] ?? null;
if ($once_event) {
return [$once_event];
}
$regular_events = static::$events[$event_name] ?? [];
return (array) $regular_events;
}
/**
* Dispatch event
*
* @param string|AppEvent $event
* @return bool|null
* @throws EventException
*/
public function emit(string|AppEvent $event): ?bool
{
$event_name = $event;
if ($event instanceof AppEvent) {
$event_name = get_class($event);
$data = [$event];
} else {
$data = array_slice(func_get_args(), 1);
}
if (!$this->bound($event_name)) {
return null;
}
$events = $this->getEventListeners($event_name);
// Execute each listener
collect($events)->each(fn(Listener $listener) => $listener->call($data));
return true;
}
/**
* Dispatch event
*
* @param string|AppEvent $event
* @return bool|null
* @throws EventException
*/
public function dispatch(string|AppEvent $event): ?bool
{
return $this->emit($event);
}
/**
* off removes an event saves
*
* @param string|AppEvent $event
*/
public function off(string|AppEvent $event): void
{
if ($this->bound($event)) {
unset(static::$events[$event], static::$events['__bow.once.event'][$event]);
}
}
/**
* __callStatic
*
* @param string $name
* @param array $arguments
* @return mixed
* @throws ErrorException
*/
public static function __callStatic(string $name, array $arguments)
{
if (is_null(static::$instance)) {
throw new ErrorException(
"Unable to get event instance before configuration"
);
}
if (method_exists(static::$instance, $name)) {
return call_user_func_array([static::$instance, $name], $arguments);
}
throw new RuntimeException('The method ' . $name . ' There is no');
}
}