diff --git a/Router.php b/Router.php index 584b6d4..973bc13 100644 --- a/Router.php +++ b/Router.php @@ -2,5 +2,5 @@ interface Router { public function add(string $method, string $route, callable $handler): Router; - public function lookup(string $method, string $uri): int|array; + public function lookup(string $method, string $uri): array; } diff --git a/SegmentRouter.php b/SegmentRouter.php index 2ff0863..e3f2616 100644 --- a/SegmentRouter.php +++ b/SegmentRouter.php @@ -4,10 +4,6 @@ class SegmentRouter implements Router { public array $routes = []; - /** - * Add a route to the router. The route can contain dynamic segments, which are denoted by a colon. They are - * inserted into the node tree per segment; if a segment is dynamic, it is stored under the key ':x'. - */ public function add(string $method, string $route, callable $handler): Router { // Expand the route into segments and make dynamic segments into a common placeholder @@ -29,14 +25,8 @@ class SegmentRouter implements Router return $this; } - /** - * Lookup a route in the router. This method will return the handler for the route if it is found, or a 404 or 405 - * status code if the route is not found or the method is not allowed. If the route contains dynamic segments, the - * method will return an array with the handler and the dynamic segments. - * - * @return [callable, array] | int - */ - public function lookup(string $method, string $uri): int|array + // @return array{code: int, handler: callable, params: array|null } + public function lookup(string $method, string $uri): array { // node is a reference to our current location in the node tree $node = $this->routes; @@ -60,17 +50,15 @@ class SegmentRouter implements Router } // if we can't find a node for this segment, return 404 - return 404; + return ['code' => 404]; } - // if we fail to find a handler for the method, return 405 - if (!isset($node[$method])) return 405; - - // return the handler and any dynamic segments we found - return [$node[$method], $params]; + // if we found a handler for the method, return it and any params. if not, return a 405 + return isset($node[$method]) + ? ['code' => 200, 'handler' => $node[$method], 'params' => $params ?? null] + : ['code' => 405]; } - public function clear(): Router { $this->routes = []; diff --git a/StaticRouter.php b/StaticRouter.php index 30610f5..bd67561 100644 --- a/StaticRouter.php +++ b/StaticRouter.php @@ -2,24 +2,7 @@ class StaticRouter { - public static $routes = []; - - public static function add(string $method, string $route, callable $handler): void - { - $segments = array_map(function($segment) { - return str_starts_with($segment, ':') ? ':x' : $segment; - }, explode('/', trim($route, '/'))); - - $node = &self::$routes; - foreach ($segments as $segment) { - if ($segment === '') continue; - $node = &$node[$segment]; - } - - $node[$method] = $handler; - } - - public static function lookup(array $node /* = &$this->routes */, string $method, string $uri): int|array + public static function lookup(array $node, string $method, string $uri): int|array { $uriSegments = explode('/', trim($uri, '/')); $params = []; diff --git a/TrieRouter.php b/TrieRouter.php index 3981f38..72037c5 100644 --- a/TrieRouter.php +++ b/TrieRouter.php @@ -26,7 +26,7 @@ class TrieRouter implements Router } // Find and handle the route - public function lookup(string $method, string $uri): int|array + public function lookup(string $method, string $uri): array { $node = &$this->root[$method]; $segments = explode('/', trim($uri, '/')); @@ -42,15 +42,15 @@ class TrieRouter implements Router $params[] = $segment; $node = &$node[$dynamicSegment]['_children']; } else { - return 404; + return ['code' => 404]; } } } // Check if a handler exists for the current node - if (isset($node['_handler'])) return [$node['_handler'], $params]; + if (isset($node['_handler'])) return ['code' => 200, 'handler' => $node['_handler'], 'params' => $params]; - return 404; + return ['code' => 404]; } // Match dynamic route segments like ':id'