yaroslawww /
laravel-dusk-reporter
| 1 | <?php |
||
| 2 | |||
| 3 | namespace LaravelDuskReporter\Generation; |
||
| 4 | |||
| 5 | use Illuminate\Support\Str; |
||
| 6 | |||
| 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) |
||
| 37 | { |
||
| 38 | if ( |
||
| 39 | Str::startsWith($name, 'addAction') |
||
| 40 | && ($hookAction = Str::camel(Str::after($name, 'addAction'))) |
||
| 41 | ) { |
||
| 42 | static::addHookAction($hookAction, ...$arguments); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 43 | |||
| 44 | return; |
||
| 45 | } |
||
| 46 | |||
| 47 | throw new \BadMethodCallException("Method [$name] not exists"); |
||
| 48 | } |
||
| 49 | } |
||
| 50 |