Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
12 / 12 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
Events | |
100.00% |
12 / 12 |
|
100.00% |
4 / 4 |
6 | |
100.00% |
1 / 1 |
listen | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
trigger | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
3 | |||
remove | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
isListening | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
1 | <?php declare(strict_types=1); |
2 | /* |
3 | * This file is part of Aplus Framework Events Library. |
4 | * |
5 | * (c) Natan Felles <natanfelles@gmail.com> |
6 | * |
7 | * For the full copyright and license information, please view the LICENSE |
8 | * file that was distributed with this source code. |
9 | */ |
10 | namespace Framework\Events; |
11 | |
12 | use JetBrains\PhpStorm\Pure; |
13 | use Throwable; |
14 | |
15 | /** |
16 | * Class Events. |
17 | * |
18 | * @package events |
19 | */ |
20 | class Events |
21 | { |
22 | /** |
23 | * @var array<string,callable> |
24 | */ |
25 | protected static array $listeners = []; |
26 | |
27 | public static function listen(string $name, callable $callback) : void |
28 | { |
29 | static::$listeners[$name] = $callback; |
30 | } |
31 | |
32 | public static function trigger(string $name, mixed ...$arguments) : void |
33 | { |
34 | if ( ! static::isListening($name)) { |
35 | return; |
36 | } |
37 | try { |
38 | static::$listeners[$name](...$arguments); |
39 | } catch (Throwable $throwable) { |
40 | throw new EventsException( |
41 | 'Event "' . $name . '" failed', |
42 | 0, |
43 | $throwable |
44 | ); |
45 | } |
46 | } |
47 | |
48 | public static function remove(string $name) : void |
49 | { |
50 | unset(static::$listeners[$name]); |
51 | } |
52 | |
53 | #[Pure] |
54 | public static function isListening(string $name) : bool |
55 | { |
56 | return isset(static::$listeners[$name]); |
57 | } |
58 | } |