From 3a65efc7a28e5a1513301fcf2948f873db41039b Mon Sep 17 00:00:00 2001 From: "haertl.mike" <> Date: Mon, 24 Nov 2008 17:16:44 +0000 Subject: Base url wildcard implementation (Issue #72) --- framework/Web/TUrlMapping.php | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/framework/Web/TUrlMapping.php b/framework/Web/TUrlMapping.php index 6eba80f4..338c00f8 100644 --- a/framework/Web/TUrlMapping.php +++ b/framework/Web/TUrlMapping.php @@ -264,7 +264,8 @@ class TUrlMapping extends TUrlManager if(is_string($key)) $params[$key]=$value; } - $params[$pattern->getServiceID()]=$pattern->getServiceParameter(); + if (!$pattern->getIsWildCardPattern()) + $params[$pattern->getServiceID()]=$pattern->getServiceParameter(); return $params; } } @@ -299,6 +300,8 @@ class TUrlMapping extends TUrlManager if(!(is_array($getItems) || ($getItems instanceof Traversable))) $getItems=array(); $key=$serviceID.':'.$serviceParam; + $wildCardKey = ($pos=strrpos($serviceParam,'.'))!==false ? + $serviceID.':'.substr($serviceParam,0,$pos).'.*' : $serviceID.':*'; if(isset($this->_constructRules[$key])) { foreach($this->_constructRules[$key] as $rule) @@ -306,6 +309,17 @@ class TUrlMapping extends TUrlManager if($rule->supportCustomUrl($getItems)) return $rule->constructUrl($getItems,$encodeAmpersand,$encodeGetItems); } + } + elseif(isset($this->_constructRules[$wildCardKey])) + { + foreach($this->_constructRules[$wildCardKey] as $rule) + { + if($rule->supportCustomUrl($getItems)) + { + $getItems['*']= $pos ? substr($serviceParam,$pos+1) : $serviceParam; + return $rule->constructUrl($getItems,$encodeAmpersand,$encodeGetItems); + } + } } } return parent::constructUrl($serviceID,$serviceParam,$getItems,$encodeAmpersand,$encodeGetItems); @@ -399,6 +413,8 @@ class TUrlMappingPattern extends TComponent private $_manager; private $_caseSensitive=true; + + private $_isWildCardPattern=false; /** * Constructor. @@ -428,6 +444,8 @@ class TUrlMappingPattern extends TComponent { if($this->_serviceParameter===null) throw new TConfigurationException('urlmappingpattern_serviceparameter_required', $this->getPattern()); + if(strpos($this->_serviceParameter,'*')!==false) + $this->_isWildCardPattern=true; } /** @@ -444,6 +462,11 @@ class TUrlMappingPattern extends TComponent $params[]='{'.$key.'}'; $values[]='(?P<'.$key.'>'.$value.')'; } + if ($this->getIsWildCardPattern()) { + $params[]='{*}'; + // service parameter must not contain '=' and '/' + $values[]='(?P<'.$this->getServiceID().'>[^=/]+)'; + } $params[]='/'; $values[]='\\/'; $regexp=str_replace($params,$values,trim($this->getPattern(),'/').'/'); @@ -584,6 +607,14 @@ class TUrlMappingPattern extends TComponent $this->_customUrl=TPropertyValue::ensureBoolean($value); } + /** + * @return boolean whether this pattern is a wildcard pattern + * @since 3.1.4 + */ + public function getIsWildCardPattern() { + return $this->_isWildCardPattern; + } + /** * @param array list of GET items to be put in the constructed URL * @return boolean whether this pattern IS the one for constructing the URL with the specified GET items. @@ -618,7 +649,7 @@ class TUrlMappingPattern extends TComponent { if($encodeGetItems) $value=rawurlencode($value); - if($this->_parameters->contains($key)) + if($this->_parameters->contains($key) || $key==='*' && $this->getIsWildCardPattern()) $replace['{'.$key.'}']=$value; else $extra[$key]=$value; -- cgit v1.2.3