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.
302 lines
8.8 KiB
302 lines
8.8 KiB
12 years ago
|
<?php
|
||
|
/**
|
||
|
* @link http://www.yiiframework.com/
|
||
12 years ago
|
* @copyright Copyright (c) 2008 Yii Software LLC
|
||
12 years ago
|
* @license http://www.yiiframework.com/license/
|
||
|
*/
|
||
|
|
||
|
namespace yii\web;
|
||
|
|
||
|
use yii\base\Object;
|
||
12 years ago
|
use yii\base\InvalidConfigException;
|
||
12 years ago
|
|
||
|
/**
|
||
12 years ago
|
* UrlRule represents a rule used for parsing and generating URLs.
|
||
12 years ago
|
*
|
||
|
* @author Qiang Xue <qiang.xue@gmail.com>
|
||
|
* @since 2.0
|
||
|
*/
|
||
|
class UrlRule extends Object
|
||
|
{
|
||
|
/**
|
||
12 years ago
|
* Set [[mode]] with this value to mark that this rule is for URL parsing only
|
||
|
*/
|
||
|
const PARSING_ONLY = 1;
|
||
|
/**
|
||
|
* Set [[mode]] with this value to mark that this rule is for URL creation only
|
||
|
*/
|
||
|
const CREATION_ONLY = 2;
|
||
|
|
||
|
/**
|
||
12 years ago
|
* @var string the pattern used to parse and create the path info part of a URL.
|
||
|
* @see host
|
||
12 years ago
|
*/
|
||
12 years ago
|
public $pattern;
|
||
12 years ago
|
/**
|
||
12 years ago
|
* @var string the pattern used to parse and create the host info part of a URL.
|
||
|
* @see pattern
|
||
|
*/
|
||
|
public $host;
|
||
|
/**
|
||
12 years ago
|
* @var string the route to the controller action
|
||
|
*/
|
||
|
public $route;
|
||
|
/**
|
||
12 years ago
|
* @var array the default GET parameters (name => value) that this rule provides.
|
||
12 years ago
|
* When this rule is used to parse the incoming request, the values declared in this property
|
||
|
* will be injected into $_GET.
|
||
|
*/
|
||
12 years ago
|
public $defaults = array();
|
||
12 years ago
|
/**
|
||
|
* @var string the URL suffix used for this rule.
|
||
|
* For example, ".html" can be used so that the URL looks like pointing to a static HTML page.
|
||
|
* If not, the value of [[UrlManager::suffix]] will be used.
|
||
|
*/
|
||
|
public $suffix;
|
||
|
/**
|
||
|
* @var string|array the HTTP verb (e.g. GET, POST, DELETE) that this rule should match.
|
||
|
* Use array to represent multiple verbs that this rule may match.
|
||
|
* If this property is not set, the rule can match any verb.
|
||
|
* Note that this property is only used when parsing a request. It is ignored for URL creation.
|
||
|
*/
|
||
|
public $verb;
|
||
|
/**
|
||
12 years ago
|
* @var integer a value indicating if this rule should be used for both request parsing and URL creation,
|
||
12 years ago
|
* parsing only, or creation only.
|
||
12 years ago
|
* If not set or 0, it means the rule is both request parsing and URL creation.
|
||
|
* If it is [[PARSING_ONLY]], the rule is for request parsing only.
|
||
12 years ago
|
* If it is [[CREATION_ONLY]], the rule is for URL creation only.
|
||
|
*/
|
||
|
public $mode;
|
||
12 years ago
|
|
||
12 years ago
|
/**
|
||
|
* @var string the template for generating a new URL. This is derived from [[pattern]] and is used in generating URL.
|
||
|
*/
|
||
12 years ago
|
private $_template;
|
||
12 years ago
|
/**
|
||
|
* @var string the regex for matching the route part. This is used in generating URL.
|
||
|
*/
|
||
12 years ago
|
private $_routeRule;
|
||
12 years ago
|
/**
|
||
|
* @var array list of regex for matching parameters. This is used in generating URL.
|
||
|
*/
|
||
12 years ago
|
private $_paramRules = array();
|
||
12 years ago
|
/**
|
||
|
* @var array list of parameters used in the route.
|
||
|
*/
|
||
12 years ago
|
private $_routeParams = array();
|
||
12 years ago
|
|
||
12 years ago
|
/**
|
||
|
* Initializes this rule.
|
||
|
*/
|
||
12 years ago
|
public function init()
|
||
|
{
|
||
12 years ago
|
if ($this->pattern === null) {
|
||
|
throw new InvalidConfigException('UrlRule::pattern must be set.');
|
||
|
}
|
||
|
if ($this->route === null) {
|
||
|
throw new InvalidConfigException('UrlRule::route must be set.');
|
||
|
}
|
||
12 years ago
|
if ($this->verb !== null) {
|
||
|
if (is_array($this->verb)) {
|
||
|
foreach ($this->verb as $i => $verb) {
|
||
|
$this->verb[$i] = strtoupper($verb);
|
||
|
}
|
||
|
} else {
|
||
|
$this->verb = array(strtoupper($this->verb));
|
||
|
}
|
||
|
}
|
||
12 years ago
|
|
||
12 years ago
|
$this->pattern = trim($this->pattern, '/');
|
||
12 years ago
|
|
||
12 years ago
|
if ($this->host !== null) {
|
||
|
$this->pattern = rtrim($this->host, '/') . rtrim('/' . $this->pattern, '/') . '/';
|
||
|
} elseif ($this->pattern === '') {
|
||
12 years ago
|
$this->_template = '';
|
||
12 years ago
|
$this->pattern = '#^$#u';
|
||
|
return;
|
||
|
} else {
|
||
|
$this->pattern = '/' . $this->pattern . '/';
|
||
|
}
|
||
|
|
||
|
$this->route = trim($this->route, '/');
|
||
|
if (strpos($this->route, '<') !== false && preg_match_all('/<(\w+)>/', $this->route, $matches)) {
|
||
|
foreach ($matches[1] as $name) {
|
||
12 years ago
|
$this->_routeParams[$name] = "<$name>";
|
||
12 years ago
|
}
|
||
|
}
|
||
|
|
||
|
$tr = $tr2 = array();
|
||
|
if (preg_match_all('/<(\w+):?([^>]+)?>/', $this->pattern, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER)) {
|
||
|
foreach ($matches as $match) {
|
||
|
$name = $match[1][0];
|
||
|
$pattern = isset($match[2][0]) ? $match[2][0] : '[^\/]+';
|
||
|
if (isset($this->defaults[$name])) {
|
||
|
$length = strlen($match[0][0]);
|
||
|
$offset = $match[0][1];
|
||
12 years ago
|
if ($offset > 1 && $this->pattern[$offset - 1] === '/' && $this->pattern[$offset + $length] === '/') {
|
||
12 years ago
|
$tr["/<$name>"] = "(/(?P<$name>$pattern))?";
|
||
12 years ago
|
} else {
|
||
12 years ago
|
$tr["<$name>"] = "(?P<$name>$pattern)?";
|
||
12 years ago
|
}
|
||
|
} else {
|
||
|
$tr["<$name>"] = "(?P<$name>$pattern)";
|
||
|
}
|
||
12 years ago
|
if (isset($this->_routeParams[$name])) {
|
||
12 years ago
|
$tr2["<$name>"] = "(?P<$name>$pattern)";
|
||
|
} else {
|
||
12 years ago
|
$this->_paramRules[$name] = $pattern === '[^\/]+' ? '' : "#^$pattern$#";
|
||
12 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
12 years ago
|
$this->_template = preg_replace('/<(\w+):?([^>]+)?>/', '<$1>', $this->pattern);
|
||
|
$this->pattern = '#^' . trim(strtr($this->_template, $tr), '/') . '$#u';
|
||
12 years ago
|
|
||
12 years ago
|
if (!empty($this->_routeParams)) {
|
||
12 years ago
|
$this->_routeRule = '#^' . strtr($this->route, $tr2) . '$#u';
|
||
12 years ago
|
}
|
||
|
}
|
||
|
|
||
12 years ago
|
/**
|
||
12 years ago
|
* Parses the given request and returns the corresponding route and parameters.
|
||
12 years ago
|
* @param UrlManager $manager the URL manager
|
||
12 years ago
|
* @param Request $request the request component
|
||
12 years ago
|
* @return array|boolean the parsing result. The route and the parameters are returned as an array.
|
||
|
* If false, it means this rule cannot be used to parse this path info.
|
||
|
*/
|
||
12 years ago
|
public function parseRequest($manager, $request)
|
||
12 years ago
|
{
|
||
12 years ago
|
if ($this->mode === self::CREATION_ONLY) {
|
||
|
return false;
|
||
|
}
|
||
|
|
||
12 years ago
|
if ($this->verb !== null && !in_array($request->getRequestMethod(), $this->verb, true)) {
|
||
12 years ago
|
return false;
|
||
|
}
|
||
|
|
||
12 years ago
|
$pathInfo = $request->getPathInfo();
|
||
12 years ago
|
$suffix = (string)($this->suffix === null ? $manager->suffix : $this->suffix);
|
||
|
if ($suffix !== '' && $pathInfo !== '') {
|
||
|
$n = strlen($suffix);
|
||
|
if (substr($pathInfo, -$n) === $suffix) {
|
||
|
$pathInfo = substr($pathInfo, 0, -$n);
|
||
|
if ($pathInfo === '') {
|
||
|
// suffix alone is not allowed
|
||
|
return false;
|
||
|
}
|
||
|
} elseif ($suffix !== '/') {
|
||
|
// we allow the ending '/' to be optional if it is a suffix
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
|
||
12 years ago
|
if ($this->host !== null) {
|
||
12 years ago
|
$pathInfo = strtolower($request->getHostInfo()) . '/' . $pathInfo;
|
||
|
}
|
||
|
|
||
12 years ago
|
if (!preg_match($this->pattern, $pathInfo, $matches)) {
|
||
|
return false;
|
||
|
}
|
||
12 years ago
|
foreach ($this->defaults as $name => $value) {
|
||
|
if (!isset($matches[$name]) || $matches[$name] === '') {
|
||
|
$matches[$name] = $value;
|
||
|
}
|
||
|
}
|
||
12 years ago
|
$params = $this->defaults;
|
||
|
$tr = array();
|
||
|
foreach ($matches as $name => $value) {
|
||
12 years ago
|
if (isset($this->_routeParams[$name])) {
|
||
|
$tr[$this->_routeParams[$name]] = $value;
|
||
12 years ago
|
unset($params[$name]);
|
||
12 years ago
|
} elseif (isset($this->_paramRules[$name])) {
|
||
12 years ago
|
$params[$name] = $value;
|
||
12 years ago
|
}
|
||
|
}
|
||
12 years ago
|
if ($this->_routeRule !== null) {
|
||
12 years ago
|
$route = strtr($this->route, $tr);
|
||
|
} else {
|
||
|
$route = $this->route;
|
||
|
}
|
||
|
return array($route, $params);
|
||
12 years ago
|
}
|
||
|
|
||
12 years ago
|
/**
|
||
|
* Creates a URL according to the given route and parameters.
|
||
|
* @param UrlManager $manager the URL manager
|
||
|
* @param string $route the route. It should not have slashes at the beginning or the end.
|
||
|
* @param array $params the parameters
|
||
|
* @return string|boolean the created URL, or false if this rule cannot be used for creating this URL.
|
||
|
*/
|
||
|
public function createUrl($manager, $route, $params)
|
||
12 years ago
|
{
|
||
12 years ago
|
if ($this->mode === self::PARSING_ONLY) {
|
||
12 years ago
|
return false;
|
||
|
}
|
||
|
|
||
12 years ago
|
$tr = array();
|
||
|
|
||
|
// match the route part first
|
||
|
if ($route !== $this->route) {
|
||
12 years ago
|
if ($this->_routeRule !== null && preg_match($this->_routeRule, $route, $matches)) {
|
||
|
foreach ($this->_routeParams as $name => $token) {
|
||
12 years ago
|
if (isset($this->defaults[$name]) && strcmp($this->defaults[$name], $matches[$name]) === 0) {
|
||
|
$tr[$token] = '';
|
||
|
} else {
|
||
|
$tr[$token] = $matches[$name];
|
||
|
}
|
||
12 years ago
|
}
|
||
|
} else {
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// match default params
|
||
|
// if a default param is not in the route pattern, its value must also be matched
|
||
|
foreach ($this->defaults as $name => $value) {
|
||
12 years ago
|
if (isset($this->_routeParams[$name])) {
|
||
12 years ago
|
continue;
|
||
|
}
|
||
12 years ago
|
if (!isset($params[$name])) {
|
||
|
return false;
|
||
|
} elseif (strcmp($params[$name], $value) === 0) { // strcmp will do string conversion automatically
|
||
|
unset($params[$name]);
|
||
12 years ago
|
if (isset($this->_paramRules[$name])) {
|
||
12 years ago
|
$tr["<$name>"] = '';
|
||
|
}
|
||
12 years ago
|
} elseif (!isset($this->_paramRules[$name])) {
|
||
12 years ago
|
return false;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// match params in the pattern
|
||
12 years ago
|
foreach ($this->_paramRules as $name => $rule) {
|
||
12 years ago
|
if (isset($params[$name]) && ($rule === '' || preg_match($rule, $params[$name]))) {
|
||
|
$tr["<$name>"] = urlencode($params[$name]);
|
||
|
unset($params[$name]);
|
||
|
} elseif (!isset($this->defaults[$name]) || isset($params[$name])) {
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
|
||
12 years ago
|
$url = trim(strtr($this->_template, $tr), '/');
|
||
12 years ago
|
if ($this->host !== null) {
|
||
12 years ago
|
$pos = strpos($url, '/', 8);
|
||
|
if ($pos !== false) {
|
||
|
$url = substr($url, 0, $pos) . preg_replace('#/+#', '/', substr($url, $pos));
|
||
|
}
|
||
|
} elseif (strpos($url, '//') !== false) {
|
||
12 years ago
|
$url = preg_replace('#/+#', '/', $url);
|
||
|
}
|
||
12 years ago
|
|
||
|
if ($url !== '') {
|
||
|
$url .= ($this->suffix === null ? $manager->suffix : $this->suffix);
|
||
|
}
|
||
|
|
||
12 years ago
|
if (!empty($params)) {
|
||
12 years ago
|
$url .= '?' . http_build_query($params);
|
||
|
}
|
||
|
return $url;
|
||
|
}
|
||
12 years ago
|
}
|