-
Notifications
You must be signed in to change notification settings - Fork 408
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
193 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
<?php | ||
/** | ||
* Flight: An extensible micro-framework. | ||
* | ||
* @copyright Copyright (c) 2011, Mike Cao <mike@mikecao.com> | ||
* @license http://www.opensource.org/licenses/mit-license.php | ||
*/ | ||
|
||
namespace flight\net; | ||
|
||
/** | ||
* The Route 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. | ||
*/ | ||
class Route { | ||
/** | ||
* @var string URL pattern | ||
*/ | ||
public $pattern; | ||
|
||
/** | ||
* @var mixed Callback function | ||
*/ | ||
public $callback; | ||
|
||
/** | ||
* @var array HTTP methods | ||
*/ | ||
public $methods = array(); | ||
|
||
/** | ||
* @var array Route parameters | ||
*/ | ||
public $params = array(); | ||
|
||
/** | ||
* @var string Matching regular expression | ||
*/ | ||
public $regex; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param string $pattern URL pattern | ||
* @param mixed $callback Callback function | ||
* @param array $methods HTTP methods | ||
*/ | ||
public function __construct($pattern, $callback, $methods) { | ||
$this->pattern = $pattern; | ||
$this->callback = $callback; | ||
$this->methods = $methods; | ||
} | ||
|
||
/** | ||
* Checks if a URL matches the route pattern. Also parses named parameters in the URL. | ||
* | ||
* @param string $url Requested URL | ||
* @return boolean Match status | ||
*/ | ||
public function matchUrl($url) { | ||
if ($this->pattern === '*' || $this->pattern === $url) { | ||
return true; | ||
} | ||
|
||
$ids = array(); | ||
$char = substr($this->pattern, -1); | ||
$this->pattern = str_replace(')', ')?', $this->pattern); | ||
|
||
// 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].')'; | ||
} | ||
return '(?P<'.$matches[1].'>[^/\?]+)'; | ||
}, | ||
$this->pattern | ||
); | ||
|
||
// Fix trailing slash | ||
if ($char === '/') { | ||
$regex .= '?'; | ||
} | ||
// Replace wildcard | ||
else if ($char === '*') { | ||
$regex = str_replace('*', '.+?', $this->pattern); | ||
} | ||
// Allow trailing slash | ||
else { | ||
$regex .= '/?'; | ||
} | ||
|
||
// 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; | ||
} | ||
|
||
$this->regex = $regex; | ||
|
||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* Checks if an HTTP method matches the route methods. | ||
* | ||
* @param string $method HTTP method | ||
* @return bool Match status | ||
*/ | ||
public function matchMethod($method) { | ||
return count(array_intersect(array($method, '*'), $this->methods)) > 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters