From 1ad567f33765f7f9a012d21579d033c85ab2d1a1 Mon Sep 17 00:00:00 2001 From: tracer Date: Sat, 22 Oct 2022 12:43:57 +0200 Subject: [PATCH] refactored routing to handle any number of parameters --- src/Service/Router.php | 63 +++++++++++++++++++----------------------- 1 file changed, 28 insertions(+), 35 deletions(-) diff --git a/src/Service/Router.php b/src/Service/Router.php index c9636d1..d6627e9 100644 --- a/src/Service/Router.php +++ b/src/Service/Router.php @@ -3,51 +3,44 @@ namespace App\Service; +use App\Entity\Route; +use Closure; + class Router { - private array $uri; private array $routes; - public function __construct() + /* + * This method takes a route like /admin/users/{user} and create a regex to match on call + */ + function addRoute(string $name, string $route, Closure $callback): void { - $uri = parse_url(url: $_SERVER['REQUEST_URI'], component: PHP_URL_PATH); - $this->uri = explode(separator: '/', string: $uri); - } + // check for parameters + preg_match_all(pattern: "/(?<={).+?(?=})/", subject: $route, matches: $matches); + $parameters = $matches[0]; - public function registerRoute(string $route, \Closure $callback): void - { - $this->routes[$route] = $callback; - } + // create regex for route: + $regex = preg_replace(pattern: '/(?<={).+?(?=})/', replacement: '(.*?)', subject: $route); + $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); - - private function matchRoute($url = '/users/tracer/posts/tracer', $method = 'GET') - { - $reqUrl = $url; - - $reqUrl = rtrim(string: $reqUrl, characters: "/"); - - foreach ($this->routes as $route => $closure) { - // convert urls like '/users/:uid/posts/:pid' to regular expression - // $pattern = "@^" . preg_replace('/\\\:[a-zA-Z0-9\_\-]+/', '([a-zA-Z0-9\-\_]+)', preg_quote($route['url'])) . "$@D"; - $pattern = "@^" . preg_replace('\\/users/:[a-zA-Z0-9\_\-]+/', '([a-zA-Z0-9\-\_]+)', $route) . "$@D"; - // echo $pattern."\n"; - $params = []; - // check if the current request params the expression - $match = preg_match($pattern, $reqUrl, $params); - if ($match) { - // remove the first match - array_shift($params); - // call the callback with the matched positions as params - // return call_user_func_array($route['callback'], $params); - return [$route, $params]; - } - } - return []; + $this->routes[] = $route; } public function handleRouting(): void { - $foo = $this->matchRoute(); - var_dump($foo); + $requestUri = $_SERVER['REQUEST_URI']; + + foreach ($this->routes as $route) { + if (preg_match(pattern: $route->getRegex(), subject: $requestUri, matches: $matches)) { + $parameters = []; + foreach ($route->getParameters() as $id => $parameter) { + $parameters[$parameter] = $matches[$id +1]; + } + call_user_func(callback: $route->getCallback(), args: $parameters); + } + } } } \ No newline at end of file