add('GET', '/posts/:id', function($id) { echo "Viewing post $id"; });` */ public static function add(array &$routes, string $method, string $route, callable $handler): void { // Recursively build the node tree self::addNode( $routes, array_map( // Expand the route into segments and make dynamic segments into a common placeholder fn($segment) => str_starts_with($segment, ':') ? ':x' : $segment, explode('/', trim($route, '/')) ), $method, $handler ); } private static function addNode(array &$node, array $segments, string $method, callable $handler): void { // Base case: if no more segments, add the handler if (empty($segments)) { $node[$method] = $handler; return; } // Get the current segment $segment = array_shift($segments); // Skip empty segments if ($segment === '') { self::addNode($node, $segments, $method, $handler); return; } // Ensure the segment exists in the node if (!isset($node[$segment])) { $node[$segment] = []; } // Recur for the next segment self::addNode($node[$segment], $segments, $method, $handler); } /** * Perform a lookup in the route tree for a given method and URI. Returns an array with a result code, * a handler if found, and any dynamic parameters. Codes are 200 for success, 404 for not found, and * 405 for method not allowed. * * @return array ['code', 'handler', 'params'] */ public static function lookup(array &$routes, string $method, string $uri): array { // node is a reference to our current location in the node tree $node = $routes; if ($uri === '/' && isset($node[$method]) && array_key_exists($method, $node)) { return ['code' => 200, 'handler' => $node[$method], 'params' => null]; } // params will hold any dynamic segments we find $params = []; foreach (explode('/', trim($uri, '/')) as $segment) { if (isset($node[$segment])) { $node = $node[$segment]; } elseif (isset($node[':x'])) { $params[] = $segment; $node = $node[':x']; } else { return ['code' => 404, 'handler' => null, 'params' => []]; } } // if we found a handler for the method, return it and any params. if not, return a 405 if (array_key_exists($method, $node)) { return ['code' => 200, 'handler' => $node[$method], 'params' => $params]; } return ['code' => 405, 'handler' => null, 'params' => []]; } /** * Clear all routes from the router. */ public static function clear(array &$routes): void { $routes = []; } /** * Dump the route tree as an array. */ /*public static function dump(array &$routes): array { return $routes; }*/ /** * Register a GET route. */ public static function get(array &$routes, string $route, callable $handler): void { self::add($routes, 'GET', $route, $handler); } /** * Register a POST route. */ public static function post(array &$routes, string $route, callable $handler): void { self::add($routes, 'POST', $route, $handler); } /** * Register a PUT route. */ public static function put(array &$routes, string $route, callable $handler): void { self::add($routes, 'PUT', $route, $handler); } /** * Register a PATCH route. */ public static function patch(array &$routes, string $route, callable $handler): void { self::add($routes, 'PATCH', $route, $handler); } /** * Register a DELETE route. */ public static function delete(array &$routes, string $route, callable $handler): void { self::add($routes, 'DELETE', $route, $handler); } /** * Register a HEAD route. */ public static function head(array &$routes, string $route, callable $handler): void { self::add($routes, 'HEAD', $route, $handler); } }