$controller, 'action' => $action, 'original' => $route, ]; } /** * Reveal the treasure trove of routes. * * @return array */ public static function retrieve(): array { return self::$routes; } /** * Convert a route pattern into a regex for matching requests. * * @param string $route * @return string */ protected static function convertToRegex(string $route): string { $regex = preg_replace('/\{([a-zA-Z0-9_]+)\}/', '(?P<\1>[a-zA-Z0-9_-]+)', $route); return '#^' . $regex . '$#'; } /** * Retrieve a specific route by method and URL. * * @param string $method * @param string $url * @return array|null */ public static function find(string $method, string $url): ?array { foreach (self::$routes[$method] ?? [] as $routeRegex => $route) { if (preg_match($routeRegex, $url, $matches)) { $route['params'] = array_filter( $matches, fn($key) => !is_numeric($key), ARRAY_FILTER_USE_KEY ); return $route; } } return null; } /** * Purge all routes from the collection. * * @return void */ public static function clear(): void { self::$routes = []; } }