You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
flight-core/flight/net/Router.php

149 lines
3.7 KiB

14 years ago
<?php
/**
* Flight: An extensible micro-framework.
14 years ago
*
* @copyright Copyright (c) 2011, Mike Cao <mike@mikecao.com>
* @license http://www.opensource.org/licenses/mit-license.php
*/
namespace flight\net;
/**
* The Router class is responsible for routing an HTTP request to
* an assigned callback function. The Router tries to match the
* requested URL against a series of URL patterns.
*/
14 years ago
class Router {
14 years ago
/**
* Mapped routes.
*
* @var array
*/
14 years ago
protected $routes = array();
/**
* Matched route.
*
* @var string
*/
public $matched = null;
/**
* Matched URL parameters.
*
* @var array
*/
public $params = array();
/**
* Gets mapped routes.
*
* @return array Array of routes
*/
public function getRoutes() {
return $this->routes;
}
/**
* Resets the router.
*/
public function clear() {
$this->routes = array();
}
14 years ago
/**
* Maps a URL pattern to a callback function.
*
* @param string $pattern URL pattern to match
* @param callback $callback Callback function
*/
public function map($pattern, $callback) {
13 years ago
if (strpos($pattern, ' ') !== false) {
list($method, $url) = explode(' ', trim($pattern), 2);
14 years ago
foreach (explode('|', $method) as $value) {
$this->routes[$value][$url] = $callback;
}
}
else {
$this->routes['*'][$pattern] = $callback;
}
}
/**
* Tries to match a request to a route. Also parses named parameters in the url.
14 years ago
*
* @param string $pattern URL pattern
* @param string $url Requested URL
12 years ago
* @return boolean Match status
14 years ago
*/
public function match($pattern, $url) {
14 years ago
$ids = array();
// Build the regex for matching
$regex = preg_replace_callback(
'#@([\w]+)(:([^/\(\)]*))?#',
function($matches) use (&$ids) {
$ids[$matches[1]] = null;
if (isset($matches[3])) {
return '(?P<'.$matches[1].'>'.$matches[3].')';
14 years ago
}
return '(?P<'.$matches[1].'>[^/\?]+)';
14 years ago
},
$pattern
);
// Allow trailing slash
if (substr($pattern, -1) === '/') {
$regex .= '?';
}
else {
$regex .= '/?';
}
// Replace wildcard
if (substr($pattern, -1) === '*') {
$regex = str_replace('*', '.+?', $pattern);
}
// Convert optional parameters
$regex = str_replace(')', ')?', $regex);
14 years ago
// Attempt to match route and named parameters
if (preg_match('#^'.$regex.'(?:\?.*)?$#i', $url, $matches)) {
foreach ($ids as $k => $v) {
$this->params[$k] = (array_key_exists($k, $matches)) ? urldecode($matches[$k]) : null;
14 years ago
}
$this->matched = $pattern;
14 years ago
return true;
}
return false;
}
/**
* Routes the current request.
*
12 years ago
* @param Request $request Request object
* @return callable|boolean Matched callback function or false if not found
14 years ago
*/
public function route(Request $request) {
$this->matched = null;
$this->params = array();
13 years ago
$routes = isset($this->routes[$request->method]) ? $this->routes[$request->method] : array();
if (isset($this->routes['*'])) $routes += $this->routes['*'];
14 years ago
foreach ($routes as $pattern => $callback) {
if ($pattern === '*' || $request->url === $pattern || self::match($pattern, $request->url)) {
return $callback;
14 years ago
}
}
return false;
}
}
?>