Skip to content

Commit 6e0087c

Browse files
committed
feat: add support for resource route options + chained middleware
1 parent 1dcc391 commit 6e0087c

File tree

1 file changed

+61
-28
lines changed

1 file changed

+61
-28
lines changed

src/Router.php

Lines changed: 61 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -186,10 +186,21 @@ public static function match(string $allowedMethods, string $pattern, $handler)
186186
];
187187

188188
if ($routeOptions['middleware'] || !empty(static::$routeGroupMiddleware)) {
189-
static::$middleware[$method][] = [
190-
'pattern' => $pattern,
191-
'handler' => $routeOptions['middleware'] ?? static::$routeGroupMiddleware,
192-
];
189+
$routeMiddleware = $routeOptions['middleware'] ?? static::$routeGroupMiddleware;
190+
191+
if (is_array($routeMiddleware)) {
192+
foreach ($routeMiddleware as $middleware) {
193+
static::$middleware[$method][] = [
194+
'pattern' => $pattern,
195+
'handler' => $middleware,
196+
];
197+
}
198+
} else {
199+
static::$middleware[$method][] = [
200+
'pattern' => $pattern,
201+
'handler' => $routeMiddleware,
202+
];
203+
}
193204
}
194205
}
195206

@@ -328,10 +339,20 @@ public static function redirect(
328339
* - `/posts/{id}/delete` - POST | DELETE - Controller@destroy
329340
*
330341
* @param string $pattern The base route to use eg: /post
331-
* @param string $controller to handle route eg: PostController
342+
* @param array|string $controller to handle route eg: PostController
332343
*/
333-
public static function resource(string $pattern, string $controller)
344+
public static function resource(string $pattern, $controller)
334345
{
346+
if (is_array($controller)) {
347+
$controllerToCall = $controller[0];
348+
349+
$controller[0] = function () use ($pattern, $controllerToCall) {
350+
static::resource('/', $controllerToCall);
351+
};
352+
353+
return static::group($pattern, $controller);
354+
}
355+
335356
static::match('GET|HEAD', $pattern, "$controller@index");
336357
static::post($pattern, "$controller@store");
337358
static::match('GET|HEAD', "$pattern/create", "$controller@create");
@@ -489,6 +510,18 @@ protected static function mapHandler(
489510
$parsedOptions['middleware'] = static::$namedMiddleware[$handler['middleware']] ?? null;
490511
}
491512

513+
if (is_array($handler['middleware'] ?? null)) {
514+
$parsedOptions['middleware'] = [];
515+
516+
foreach ($handler['middleware'] as $middleware) {
517+
if (is_string($middleware)) {
518+
$parsedOptions['middleware'][] = static::$namedMiddleware[$middleware] ?? null;
519+
} else {
520+
$parsedOptions['middleware'][] = $middleware;
521+
}
522+
}
523+
}
524+
492525
if (isset($handler['handler'])) {
493526
$parsedHandler = $handler['handler'];
494527
unset($handler['handler']);
@@ -584,19 +617,6 @@ public function registerMiddleware(string $name, callable $middleware)
584617
static::$namedMiddleware[$name] = $middleware;
585618
}
586619

587-
/**
588-
* Run middleware
589-
*/
590-
protected static function runMiddleware()
591-
{
592-
$currentMiddleware = array_shift(static::$middleware);
593-
$currentMiddleware();
594-
595-
if (!empty(static::$middleware)) {
596-
static::runMiddleware();
597-
}
598-
}
599-
600620
/**
601621
* Return server base Path, and define it if isn't defined.
602622
*
@@ -697,6 +717,15 @@ public static function findRoute(
697717
return isset($match[0][0]) ? trim($match[0][0], '/') : null;
698718
}, $matches, array_keys($matches));
699719

720+
$paramsWithSlash = array_filter($params, function ($param) {
721+
return strpos($param, '/') !== false;
722+
});
723+
724+
// if any of the params contain /, we should skip this route
725+
if (!empty($paramsWithSlash)) {
726+
continue;
727+
}
728+
700729
$routeData = [
701730
'params' => $params,
702731
'handler' => $route['handler'],
@@ -786,16 +815,20 @@ public static function run(?callable $callback = null)
786815
*/
787816
private static function handle(?array $routes = null, bool $quitAfterRun = false, ?string $uri = null): int
788817
{
818+
$uri = $uri ?? static::getCurrentUri();
789819
$routeToHandle = static::findRoute($routes, $uri, $quitAfterRun);
790820

791-
if (!empty($routeToHandle)) {
792-
if (count($routeToHandle) > 1) {
793-
foreach ($routeToHandle as $route) {
794-
static::invoke($route['handler'], $route['params']);
795-
}
796-
} else {
797-
static::invoke($routeToHandle[0]['handler'], $routeToHandle[0]['params']);
798-
}
821+
// hacky solution to handle middleware catching all middleware with pattern (.*?)
822+
$routesToRun = array_filter($routeToHandle, function ($route) use ($uri) {
823+
return $route['route']['pattern'] === $uri || $route['route']['pattern'] === '/.*';
824+
});
825+
826+
if (empty($routesToRun)) {
827+
$routesToRun = $routeToHandle;
828+
}
829+
830+
foreach ($routesToRun as $currentRoute) {
831+
static::invoke($currentRoute['handler'], $currentRoute['params']);
799832
}
800833

801834
return count($routeToHandle);
@@ -824,7 +857,7 @@ private static function invoke($handler, $params = [])
824857
if (call_user_func_array([new $controller(), $method], $params) === false) {
825858
// Try to call the method as a non-static method. (the if does nothing, only avoids the notice)
826859
if (forward_static_call_array([$controller, $method], $params) === false)
827-
;
860+
;
828861
}
829862
}
830863
}

0 commit comments

Comments
 (0)