mirror of https://github.com/flightphp/core
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.
121 lines
3.1 KiB
121 lines
3.1 KiB
14 years ago
|
<?php
|
||
|
/**
|
||
14 years ago
|
* 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
|
||
|
*/
|
||
13 years ago
|
|
||
|
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();
|
||
|
|
||
13 years ago
|
/**
|
||
|
* 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) {
|
||
|
list($method, $url) = explode(' ', trim($pattern), 2);
|
||
|
|
||
|
if (!is_null($url)) {
|
||
|
foreach (explode('|', $method) as $value) {
|
||
|
$this->routes[$value][$url] = $callback;
|
||
|
}
|
||
|
}
|
||
|
else {
|
||
|
$this->routes['*'][$pattern] = $callback;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Tries to match a requst to a route. Also parses named parameters in the url.
|
||
|
*
|
||
|
* @param string $pattern URL pattern
|
||
13 years ago
|
* @param object $request Request object
|
||
14 years ago
|
*/
|
||
13 years ago
|
public function match($pattern, $request) {
|
||
14 years ago
|
$ids = array();
|
||
|
|
||
|
// Build the regex for matching
|
||
|
$regex = '/^'.implode('\/', array_map(
|
||
|
function($str) use (&$ids){
|
||
|
if ($str == '*') {
|
||
|
$str = '(.*)';
|
||
|
}
|
||
14 years ago
|
else if ($str{0} == '@') {
|
||
14 years ago
|
if (preg_match('/@(\w+)(\:([^\/]*))?/', $str, $matches)) {
|
||
|
$ids[$matches[1]] = true;
|
||
14 years ago
|
return '(?P<'.$matches[1].'>'.(isset($matches[3]) ? $matches[3] : '[^(\/|\?)]+').')';
|
||
14 years ago
|
}
|
||
|
}
|
||
|
return $str;
|
||
|
},
|
||
|
explode('/', $pattern)
|
||
|
)).'\/?(?:\?.*)?$/i';
|
||
|
|
||
|
// Attempt to match route and named parameters
|
||
13 years ago
|
if (preg_match($regex, $request->url, $matches)) {
|
||
14 years ago
|
if (!empty($ids)) {
|
||
13 years ago
|
$request->params = array_intersect_key($matches, $ids);
|
||
14 years ago
|
}
|
||
13 years ago
|
|
||
|
$request->matched = $pattern;
|
||
|
|
||
14 years ago
|
return true;
|
||
|
}
|
||
|
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Routes the current request.
|
||
|
*
|
||
|
* @param object $request Request object
|
||
13 years ago
|
* @return callable Matched callback function
|
||
14 years ago
|
*/
|
||
13 years ago
|
public function route(Request $request) {
|
||
|
$request->matched = null;
|
||
|
$request->params = array();
|
||
|
|
||
14 years ago
|
$routes = ($this->routes[$request->method] ?: array()) + ($this->routes['*'] ?: array());
|
||
14 years ago
|
|
||
|
foreach ($routes as $pattern => $callback) {
|
||
13 years ago
|
if ($pattern === '*' || $request->url === $pattern || self::match($pattern, $request)) {
|
||
|
return $callback;
|
||
14 years ago
|
}
|
||
|
}
|
||
|
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
?>
|