Total Complexity | 14 |
Total Lines | 41 |
Duplicated Lines | 0 % |
Coverage | 0% |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
7 | trait HasHooks |
||
8 | { |
||
9 | protected static array $hookActions = []; |
||
10 | |||
11 | public static function addHookAction(string $name, \Closure $callback, int $order = 10) |
||
12 | { |
||
13 | if (!isset(static::$hookActions[$name]) || !is_array(static::$hookActions[$name])) { |
||
14 | static::$hookActions[$name] = []; |
||
15 | } |
||
16 | if (!isset(static::$hookActions[$name][$order]) || !is_array(static::$hookActions[$name][$order])) { |
||
17 | static::$hookActions[$name][$order] = []; |
||
18 | } |
||
19 | static::$hookActions[$name][$order][] = $callback; |
||
20 | } |
||
21 | |||
22 | protected function runHookAction(string $name, ...$arguments) |
||
23 | { |
||
24 | if (isset(static::$hookActions[$name]) && is_array(static::$hookActions[$name])) { |
||
25 | sort(static::$hookActions[$name]); |
||
26 | foreach (static::$hookActions[$name] as $orderedActions) { |
||
27 | foreach ($orderedActions as $action) { |
||
28 | if (is_callable($action)) { |
||
29 | call_user_func($action, ...$arguments); |
||
30 | } |
||
31 | } |
||
32 | } |
||
33 | } |
||
34 | } |
||
35 | |||
36 | public static function __callStatic(string $name, array $arguments) |
||
48 | } |
||
49 | } |
||
50 |