Skip to content

Commit 67cf923

Browse files
committed
feat: add support for multiple calls on hook
1 parent 258c48a commit 67cf923

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

src/Router.php

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ class Router
2929
* 'Middleware' to run at specific times
3030
*/
3131
protected static $hooks = [
32-
'router.before' => false,
33-
'router.before.route' => false,
34-
'router.after.route' => false,
35-
'router.after' => false,
32+
'router.before' => [],
33+
'router.before.route' => [],
34+
'router.after.route' => [],
35+
'router.after' => [],
3636
];
3737

3838
/**
@@ -617,7 +617,7 @@ public static function hook(string $name, callable $handler)
617617
trigger_error("$name is not a valid hook! Refer to the docs for all supported hooks");
618618
}
619619

620-
static::$hooks[$name] = $handler;
620+
static::$hooks[$name][] = $handler;
621621
}
622622

623623
/**
@@ -627,12 +627,19 @@ public static function hook(string $name, callable $handler)
627627
*/
628628
private static function callHook(string $name)
629629
{
630-
if (is_callable(static::$hooks[$name])) {
631-
$context = static::$hooks[$name](['routes' => static::$routes]);
632-
static::$routes = $context['routes'] ?? static::$routes;
630+
if (\count(static::$hooks[$name]) === 0) {
631+
return;
633632
}
634633

635-
return $context ?? null;
634+
foreach (static::$hooks[$name] as $hook) {
635+
if (is_callable($hook)) {
636+
$context = $hook(['routes' => static::$routes]);
637+
638+
if ($context) {
639+
static::$routes = $context['routes'] ?? static::$routes;
640+
}
641+
}
642+
}
636643
}
637644

638645
/**

0 commit comments

Comments
 (0)