public function match()
{
$routes = $this->routes();
// try a simple match
if (array_key_exists($this->uri, $routes)) {
return new Route($routes[$this->uri]);
}
// search for patterns
$searches = array_keys(static::$patterns);
$replaces = array_values(static::$patterns);
foreach ($routes as $pattern => $action) {
// replace wildcards
if (strpos($pattern, ':') !== false) {
$pattern = str_replace($searches, $replaces, $pattern);
}
// slice array of matches. $matches[0] will contain the text that
// matched the full pattern, $matches[1] will have the text that
// matched the first captured parenthesized subpattern, and so on.
if (preg_match('#^' . $pattern . '$#', $this->uri, $matched)) {
return new Route($action, array_slice($matched, 1));
}
}
// call 404 handler
if (is_array(static::$not_found)) {
return new Route(static::$not_found);
}
throw new Router\Exception('No routes matched');
}