From f571d7548a6dd2f1315dd89367892c53a31343fc Mon Sep 17 00:00:00 2001 From: tracer Date: Sat, 22 Oct 2022 19:51:20 +0200 Subject: [PATCH] Removed named arguments from call_user_func --- src/Service/Router.php | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/Service/Router.php b/src/Service/Router.php index d6627e9..054d8a4 100644 --- a/src/Service/Router.php +++ b/src/Service/Router.php @@ -11,7 +11,7 @@ class Router private array $routes; /* - * This method takes a route like /admin/users/{user} and create a regex to match on call + * This method takes a route like /admin/users/{user} and creates a regex to match on call */ function addRoute(string $name, string $route, Closure $callback): void { @@ -21,8 +21,10 @@ class Router // create regex for route: $regex = preg_replace(pattern: '/(?<={).+?(?=})/', replacement: '(.*?)', subject: $route); + // code below is ugly, better match including the braces $regex = str_replace(search: '{', replace: '', subject: $regex); $regex = str_replace(search: '}', replace: '', subject: $regex); + $regex = '/^' . str_replace(search: "/", replace: '\\/', subject: $regex) . '$/i'; $route = new Route(name: $name, route: $route, regEx: $regex, parameters: $parameters, callback: $callback); @@ -39,8 +41,13 @@ class Router foreach ($route->getParameters() as $id => $parameter) { $parameters[$parameter] = $matches[$id +1]; } - call_user_func(callback: $route->getCallback(), args: $parameters); + // PHP is mad about named parameters in call_user_func + // Uncaught Error: Unknown named parameter $args in … + // But PHPStorm seems happy without them. So what? + call_user_func($route->getCallback(), $parameters); + return; } } + die("Invalid route: $requestUri"); } } \ No newline at end of file