From e25ad4bc88810e97f55585c9204a72eb7ea19470 Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Mon, 11 Feb 2013 13:29:53 -0500 Subject: [PATCH] URL wip. --- framework/web/UrlRule.php | 58 ++++++++++++++++++++------------ tests/unit/framework/web/UrlRuleTest.php | 44 ++++++++++++++++++++++++ 2 files changed, 81 insertions(+), 21 deletions(-) diff --git a/framework/web/UrlRule.php b/framework/web/UrlRule.php index 247633e..612a793 100644 --- a/framework/web/UrlRule.php +++ b/framework/web/UrlRule.php @@ -1,6 +1,6 @@ 'post/', - * 'route' => 'post/view', - * 'defaults' => array('page' => 1), - * ) - * - * array( - * 'pattern' => 'about', - * 'route' => 'site/page', - * 'defaults' => array('view' => 'about'), - * ) + * UrlRule represents a rule used for parsing and generating URLs. * * @author Qiang Xue * @since 2.0 @@ -45,13 +34,36 @@ class UrlRule extends Object */ public $defaults = array(); - protected $paramRules = array(); - protected $routeRule; + /** + * @var string the template for generating a new URL. This is derived from [[pattern]] and is used in generating URL. + */ protected $template; + /** + * @var string the regex for matching the route part. This is used in generating URL. + */ + protected $routeRule; + /** + * @var array list of regex for matching parameters. This is used in generating URL. + */ + protected $paramRules = array(); + /** + * @var array list of parameters used in the route. + */ protected $routeParams = array(); + /** + * Initializes this rule. + */ public function init() { + $this->compileRule(); + } + + /** + * Compiles the rule using the current configuration. + */ + protected function compileRule() + { $this->pattern = trim($this->pattern, '/'); if ($this->pattern === '') { $this->template = ''; @@ -105,15 +117,19 @@ class UrlRule extends Object if (!preg_match($this->pattern, $pathInfo, $matches)) { return false; } + foreach ($this->defaults as $name => $value) { + if (!isset($matches[$name]) || $matches[$name] === '') { + $matches[$name] = $value; + } + } $params = $this->defaults; $tr = array(); foreach ($matches as $name => $value) { - if ($value !== '') { - if (isset($this->routeParams[$name])) { - $tr[$this->routeParams[$name]] = $value; - } elseif (isset($this->paramRules[$name])) { - $params[$name] = $value; - } + if (isset($this->routeParams[$name])) { + $tr[$this->routeParams[$name]] = $value; + unset($params[$name]); + } elseif (isset($this->paramRules[$name])) { + $params[$name] = $value; } } if ($this->routeRule !== null) { diff --git a/tests/unit/framework/web/UrlRuleTest.php b/tests/unit/framework/web/UrlRuleTest.php index 1467454..e9148c7 100644 --- a/tests/unit/framework/web/UrlRuleTest.php +++ b/tests/unit/framework/web/UrlRuleTest.php @@ -435,6 +435,50 @@ class UrlRuleTest extends \yiiunit\TestCase array('post', false), ), ), + array( + 'route has parameters', + array( + 'pattern' => '/', + 'route' => '/', + 'defaults' => array(), + ), + array( + array('post/index', 'post/index'), + array('module/post/index', false), + ), + ), + array( + 'route has parameters with regex', + array( + 'pattern' => '/', + 'route' => '/', + 'defaults' => array(), + ), + array( + array('post/index', 'post/index'), + array('comment/index', 'comment/index'), + array('test/index', false), + array('post', false), + array('module/post/index', false), + ), + ), + array( + 'route has default parameter', + array( + 'pattern' => '/', + 'route' => '/', + 'defaults' => array('action' => 'index'), + ), + array( + array('post/view', 'post/view'), + array('comment/view', 'comment/view'), + array('test/view', false), + array('post', 'post/index'), + array('posts', false), + array('test', false), + array('index', false), + ), + ), ); } }