public function route($uri = null)
{
if (empty($uri)) {
// CAUTION: parse_url does not work reliably with relative URIs, it is intended for fully qualified URLs.
// Using parse_url with URI can cause bugs like this: https://github.com/zaphpa/zaphpa/issues/13
// We have URI and we could really use parse_url however, so let's pretend we have a full URL by prepending
// our URI with a meaningless scheme/domain.
$tokens = parse_url('http://foo.com' . $_SERVER['REQUEST_URI']);
$uri = rawurldecode($tokens['path']);
}
/* Call preprocessors on each middleware impl */
foreach (self::$middleware as $m) {
$m->preprocess($this);
}
$routes = $this->getRoutes();
foreach ($routes as $route) {
$params = $route['template']->match($uri);
if (!is_null($params)) {
Zaphpa_Middleware::$context['pattern'] = $route['template']->getTemplate();
Zaphpa_Middleware::$context['http_method'] = self::getRequestMethod();
Zaphpa_Middleware::$context['callback'] = $route['callback'];
$callback = Zaphpa_Callback_Util::getCallback($route['callback'], $route['file']);
return $this->invoke_callback($callback, $params);
}
}
if (strcasecmp(Zaphpa_Router::getRequestMethod(), "options") == 0) {
return $this->invoke_options();
}
throw new Zaphpa_InvalidPathException('Invalid path');
}