…); // Action * Hooks::do('post.published', $post); * * Hooks::filter('navigation.items', fn (array $items) => [...]); // Filter * $items = Hooks::apply('navigation.items', $items); * * Niedrigere Priorität läuft zuerst (Standard 10). */ class Hooks { /** @var array>> */ private array $actions = []; /** @var array>> */ private array $filters = []; public static function on(string $name, callable $callback, int $priority = 10): void { app(self::class)->actions[$name][$priority][] = $callback; } public static function do(string $name, mixed ...$arguments): void { foreach (app(self::class)->sorted(app(self::class)->actions[$name] ?? []) as $callback) { $callback(...$arguments); } } public static function filter(string $name, callable $callback, int $priority = 10): void { app(self::class)->filters[$name][$priority][] = $callback; } public static function apply(string $name, mixed $value, mixed ...$arguments): mixed { foreach (app(self::class)->sorted(app(self::class)->filters[$name] ?? []) as $callback) { $value = $callback($value, ...$arguments); } return $value; } public static function has(string $name): bool { $hooks = app(self::class); return ! empty($hooks->actions[$name]) || ! empty($hooks->filters[$name]); } /** * @param array> $byPriority * @return list */ private function sorted(array $byPriority): array { ksort($byPriority); return array_merge(...array_values($byPriority) ?: [[]]); } }