From 1c32172efb18e8d08ea483e2460813670ebfe1a5 Mon Sep 17 00:00:00 2001 From: xue <> Date: Sat, 30 Sep 2006 18:40:40 +0000 Subject: merge from 3.0 branch till 1451. --- framework/Web/TUrlMapping.php | 81 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 69 insertions(+), 12 deletions(-) (limited to 'framework/Web/TUrlMapping.php') diff --git a/framework/Web/TUrlMapping.php b/framework/Web/TUrlMapping.php index a5d6abf8..a4c662a9 100644 --- a/framework/Web/TUrlMapping.php +++ b/framework/Web/TUrlMapping.php @@ -43,7 +43,7 @@ * @package System.Web * @since 3.0.5 */ -class TUrlMapping extends THttpRequest +class TUrlMapping extends TModule { /** * @var string default pattern class. @@ -201,9 +201,9 @@ class TUrlMapping extends THttpRequest * * Describes an URL mapping pattern, if a given URL matches the pattern, the * TUrlMapping class will alter the THttpRequest parameters. The - * url matching is done using regular expressions. + * url matching is done using patterns and regular expressions. * - * The {@link setPattern Pattern} property takes a regular expression with + * The {@link setPattern Pattern} property takes an string expression with * parameter names enclosed between a left brace '{' and a right brace '}'. * The pattens for each parameter can be set using {@link getParameters Parameters} * attribute collection. For example @@ -215,6 +215,17 @@ class TUrlMapping extends THttpRequest * In the above example, the pattern contains 3 parameters named "year", * "month" and "day". The pattern for these parameters are, respectively, * "\d{4}" (4 digits), "\d{2}" (2 digits) and "\d+" (1 or more digits). + * Essentially, the Parameters attribute name and values are used + * as substrings in replacing the placeholders in the Pattern string + * to form a complete regular expression string. A full regular expression + * may be expressed using the RegularExpression attribute or + * as the body content of the <module> tag. The above pattern is equivalent + * to the following regular expression pattern. + * + * /articles\/(?P\d{4})\/(?P\d{2})\/(?P\d+)/u + * + * The above regular expression used the "named group" feature available in PHP. + * Notice that you need to escape the slash in regular expressions. * * In the TUrlMappingPattern class, the pattern is matched against the * path property of the url only. @@ -262,6 +273,10 @@ class TUrlMappingPattern extends TComponent * @var string regular expression pattern. */ private $_regexp; + /** + * @var boolean case sensitive matching, default is true + */ + private $_caseSensitive=true; public function __construct() { @@ -277,20 +292,20 @@ class TUrlMappingPattern extends TComponent { $body = trim($config->getValue()); if(strlen($body)>0) - $this->setPattern($body); + $this->setRegularExpression($body); if(is_null($this->_serviceParameter)) { throw new TConfigurationException( 'dispatcher_url_service_parameter_missing', $this->getPattern()); } - $this->initializePattern(); } /** * Subsitutue the parameter key value pairs as named groupings * in the regular expression matching pattern. + * @return string regular expression pattern with parameter subsitution */ - protected function initializePattern() + protected function getParameterizedPattern() { $params= array(); $values = array(); @@ -299,17 +314,29 @@ class TUrlMappingPattern extends TComponent $params[] = '{'.$key.'}'; $values[] = '(?P<'.$key.'>'.$value.')'; } - $this->_regexp = str_replace($params,$values,$this->getPattern()); + $params[] = '/'; + $values[] = '\\/'; + $regexp = str_replace($params,$values,$this->getPattern()); + $modifiers = $this->getModifiers(); + return '/'.$regexp.'/'.$modifiers; } /** - * @return string mapping pattern + * @return string full regular expression mapping pattern */ - protected function getRegExpPattern() + public function getRegularExpression() { return $this->_regexp; } + /** + * @param string full regular expression mapping patern. + */ + public function setRegularExpression($value) + { + $this->_regexp; + } + /** * @param string service parameter, such as page class name. */ @@ -358,6 +385,22 @@ class TUrlMappingPattern extends TComponent $this->_pattern = $value; } + /** + * @param boolean case sensitive pattern matching, default is true. + */ + public function setCaseSensitive($value) + { + $this->_caseSensitive=TPropertyValue::ensureBoolean($value); + } + + /** + * @return boolean case sensitive pattern matching, default is true. + */ + public function getCaseSensitive() + { + return $this->_caseSensitive; + } + /** * @return TAttributeCollection parameter key value pairs. */ @@ -375,7 +418,8 @@ class TUrlMappingPattern extends TComponent } /** - * Use regular expression to match the given url path. + * Uses URL pattern (or full regular expression if available) to + * match the given url path. * @param TUri url to match against * @return array matched parameters, empty if no matches. */ @@ -383,10 +427,23 @@ class TUrlMappingPattern extends TComponent { $path = $url->getPath(); $matches=array(); - $pattern = str_replace('/', '\\/', $this->getRegExpPattern()); - preg_match('/'.$pattern.'/', $path, $matches); + $pattern = $this->getRegularExpression(); + if($pattern === null) + $pattern = $this->getParameterizedPattern(); + preg_match($pattern, $path, $matches); return $matches; } + + /** + * @return string regular expression matching modifiers. + */ + protected function getModifiers() + { + $modifiers = 'u'; + if(!$this->getCaseSensitive()) + $modifiers .= 'i'; + return $modifiers; + } } ?> \ No newline at end of file -- cgit v1.2.3