1 | <?php |
||
2 | |||
3 | /** |
||
4 | * This file is part of dimtrovich/db-dumper". |
||
5 | * |
||
6 | * (c) 2024 Dimitri Sitchet Tomkeu <[email protected]> |
||
7 | * |
||
8 | * For the full copyright and license information, please view |
||
9 | * the LICENSE file that was distributed with this source code. |
||
10 | */ |
||
11 | |||
12 | namespace Dimtrovich\DbDumper; |
||
13 | |||
14 | class Event |
||
15 | { |
||
16 | /** |
||
17 | * @var array<string, list<callable>> |
||
0 ignored issues
–
show
Documentation
Bug
introduced
by
![]() |
|||
18 | */ |
||
19 | private array $listeners = []; |
||
20 | |||
21 | /** |
||
22 | * Register a listener for event |
||
23 | */ |
||
24 | public function on(string $event, callable $callable): void |
||
25 | { |
||
26 | if (! array_key_exists($event, $this->listeners)) { |
||
27 | $this->listeners[$event] = []; |
||
28 | } |
||
29 | |||
30 | $this->listeners[$event][] = $callable; |
||
31 | } |
||
32 | |||
33 | /** |
||
34 | * Trigger event |
||
35 | */ |
||
36 | public function emit(string $event, ...$args) |
||
37 | { |
||
38 | $listeners = $this->listeners[$event] ?? []; |
||
39 | |||
40 | foreach ($listeners as $callable) { |
||
41 | $callable(...$args); |
||
42 | } |
||
43 | } |
||
44 | } |
||
45 |