/** * Finds a function to call from a request path * * @param string $method Request method * @param string $request Request path * @return array Function to call and parameters to call it with */ private static function route($method, $request) { $params = array('splat' => array(), 'captures' => array()); if (isset(self::$routes[$method][$request])) { $function = self::$routes[$method][$request]; } elseif (($route = reverse_preg_match_array($request, array_keys(self::$routes[$method]), array('#\\*(/|$)#', '/:[A-Za-z0-9]+/'))) && $route !== false) { $route = end($route); // The only different things between the request url and the // route should be the regex's, so we get them. $changes = url_diff($request, $route); $function = self::$routes[$method][$route]; foreach ($changes as $index => $value) { if (preg_match('/^:/', $index)) { //Strip leading : $index = preg_replace('/^:/', '', $index); $params[$index] = $value; } elseif ($index == '*') { $params['splat'][] = $value; } else { $params['captures'][] = $value; } } } if (!isset($function)) { self::$status = 404; //We don't want to display headers for command line if (!defined('STDIN')) { header("HTTP/1.1 404 Not Found"); } if (isset(self::$errors['404'])) { $function = self::$errors['404']; } else { $function = create_function('', 'echo "We couldn\'t find that page.";'); } } return array($function, $params); }