public function dispatch($path)
{
if (!$this->hasCache) {
foreach ($this->plugins as $p) {
$p->beforeCompile($this);
}
$this->routes->compile();
foreach ($this->plugins as $p) {
$p->afterCompile($this);
}
if ($this->cache) {
// make cache
$this->makeCache();
}
// we are already in runtime, doesn't need to reload cache or
// re-compile pattern.
$this->hasCache = true;
}
$routeClass = $this->matchedRouteClass;
if ($this->extensionSupport) {
// function_exists('roller_dispatch')
if ($route = roller_dispatch($this->routes->routes, $path)) {
return new $routeClass($this, $route, $path);
} else {
return false;
}
} else {
$server_req_method = isset($_SERVER['REQUEST_METHOD']) ? strtolower($_SERVER['REQUEST_METHOD']) : null;
foreach ($this->routes as $route) {
if (preg_match($route['compiled'], $path, $regs)) {
// if method is defined, we should check server request method
if ($server_req_method && isset($route['method']) && ($m = $route['method'])) {
/*
* Which request method was used to access the page;
* i.e. 'GET', 'HEAD', 'POST', 'PUT'.
*/
if ($server_req_method !== $m) {
continue;
}
}
// apply variables
foreach ($route['variables'] as $k) {
if (isset($regs[$k])) {
$route['vars'][$k] = $regs[$k];
} elseif (isset($route['default'][$k])) {
$route['vars'][$k] = $route['default'][$k];
}
}
// matched!
return new $routeClass($this, $route, $path);
}
}
}
return false;
}