From 736e92efbc75908a2bf26fe333a03990e3bb8100 Mon Sep 17 00:00:00 2001 From: carlgmathisen <> Date: Sun, 7 Dec 2008 13:05:05 +0000 Subject: work on php style configuration --- framework/Web/TUrlMapping.php | 132 +++++++++++++++++++++++++----------------- 1 file changed, 80 insertions(+), 52 deletions(-) (limited to 'framework/Web/TUrlMapping.php') diff --git a/framework/Web/TUrlMapping.php b/framework/Web/TUrlMapping.php index 83dd99b6..30e62496 100644 --- a/framework/Web/TUrlMapping.php +++ b/framework/Web/TUrlMapping.php @@ -4,7 +4,7 @@ * * @author Wei Zhuo * @link http://www.pradosoft.com/ - * @copyright Copyright © 2005-2008 PradoSoft + * @copyright Copyright © 2005-2008 PradoSoft * @license http://www.pradosoft.com/license/ * @version $Id$ * @package System.Web @@ -69,10 +69,6 @@ Prado::using('System.Collections.TAttributeCollection'); */ class TUrlMapping extends TUrlManager { - /** - * File extension of external configuration file - */ - const CONFIG_FILE_EXT='.xml'; /** * @var TUrlMappingPattern[] list of patterns. */ @@ -101,17 +97,17 @@ class TUrlMapping extends TUrlManager /** * Initializes this module. * This method is required by the IModule interface. - * @param TXmlElement configuration for this module, can be null + * @param mixed configuration for this module, can be null * @throws TConfigurationException if module is configured in the global scope. */ - public function init($xml) + public function init($config) { - parent::init($xml); + parent::init($config); if($this->getRequest()->getRequestResolved()) throw new TConfigurationException('urlmapping_global_required'); if($this->_configFile!==null) $this->loadConfigFile(); - $this->loadUrlMappings($xml); + $this->loadUrlMappings($config); if($this->_urlPrefix==='') $this->_urlPrefix=$this->getRequest()->getApplicationUrl(); $this->_urlPrefix=rtrim($this->_urlPrefix,'/'); @@ -125,9 +121,17 @@ class TUrlMapping extends TUrlManager { if(is_file($this->_configFile)) { - $dom=new TXmlDocument; - $dom->loadFromFile($this->_configFile); - $this->loadUrlMappings($dom); + if($this->getApplication()->getConfigurationType()==TApplication::CONFIG_TYPE_PHP) + { + $config = include $this->_configFile; + $this->loadUrlMappings($dom); + } + else + { + $dom=new TXmlDocument; + $dom->loadFromFile($this->_configFile); + $this->loadUrlMappings($dom); + } } else throw new TConfigurationException('urlmapping_configfile_inexistent',$this->_configFile); @@ -191,7 +195,7 @@ class TUrlMapping extends TUrlManager */ public function setConfigFile($value) { - if(($this->_configFile=Prado::getPathOfNamespace($value,self::CONFIG_FILE_EXT))===null) + if(($this->_configFile=Prado::getPathOfNamespace($value,$this->getApplication()->getConfigurationFileExt()))===null) throw new TConfigurationException('urlmapping_configfile_invalid',$value); } @@ -218,28 +222,52 @@ class TUrlMapping extends TUrlManager /** * Load and configure each url mapping pattern. - * @param TXmlElement configuration node + * @param mixed configuration node * @throws TConfigurationException if specific pattern class is invalid */ - protected function loadUrlMappings($xml) + protected function loadUrlMappings($config) { - foreach($xml->getElementsByTagName('url') as $url) + if(is_array($config)) { - $properties=$url->getAttributes(); - if(($class=$properties->remove('class'))===null) - $class=$this->getDefaultMappingClass(); - $pattern=Prado::createComponent($class,$this); - if(!($pattern instanceof TUrlMappingPattern)) - throw new TConfigurationException('urlmapping_urlmappingpattern_required'); - foreach($properties as $name=>$value) - $pattern->setSubproperty($name,$value); - $this->_patterns[]=$pattern; - $pattern->init($url); - - $key=$pattern->getServiceID().':'.$pattern->getServiceParameter(); - $this->_constructRules[$key][]=$pattern; + if(isset($config['urls']) && is_array($config['urls'])) + { + foreach($config['urls'] as $url) + { + $class=null; + if(!isset($url['class'])) + $class=$this->getDefaultMappingClass(); + $pattern=Prado::createComponent($class,$this); + $properties = isset($url['properties'])?$url['properties']:array(); + $this->buildUrlMapping($class,$pattern,$properties,$url); + } + } + } + else + { + foreach($config->getElementsByTagName('url') as $url) + { + $properties=$url->getAttributes(); + if(($class=$properties->remove('class'))===null) + $class=$this->getDefaultMappingClass(); + $pattern=Prado::createComponent($class,$this); + $this->buildUrlMapping($class,$pattern,$properties,$url); + } } } + + private function buildUrlMapping($class, $pattern, $properties, $url) + { + $pattern=Prado::createComponent($class,$this); + if(!($pattern instanceof TUrlMappingPattern)) + throw new TConfigurationException('urlmapping_urlmappingpattern_required'); + foreach($properties as $name=>$value) + $pattern->setSubproperty($name,$value); + $this->_patterns[]=$pattern; + $pattern->init($url); + + $key=$pattern->getServiceID().':'.$pattern->getServiceParameter(); + $this->_constructRules[$key][]=$pattern; + } /** * Parses the request URL and returns an array of input parameters. @@ -264,7 +292,7 @@ class TUrlMapping extends TUrlManager if(is_string($key)) $params[$key]=$value; } - if (!$pattern->getIsWildCardPattern()) + if (!$pattern->getIsWildCardPattern()) $params[$pattern->getServiceID()]=$pattern->getServiceParameter(); return $params; } @@ -300,8 +328,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.':*'; + $wildCardKey = ($pos=strrpos($serviceParam,'.'))!==false ? + $serviceID.':'.substr($serviceParam,0,$pos).'.*' : $serviceID.':*'; if(isset($this->_constructRules[$key])) { foreach($this->_constructRules[$key] as $rule) @@ -309,16 +337,16 @@ class TUrlMapping extends TUrlManager if($rule->supportCustomUrl($getItems)) return $rule->constructUrl($getItems,$encodeAmpersand,$encodeGetItems); } - } - elseif(isset($this->_constructRules[$wildCardKey])) - { + } + elseif(isset($this->_constructRules[$wildCardKey])) + { foreach($this->_constructRules[$wildCardKey] as $rule) { if($rule->supportCustomUrl($getItems)) - { - $getItems['*']= $pos ? substr($serviceParam,$pos+1) : $serviceParam; + { + $getItems['*']= $pos ? substr($serviceParam,$pos+1) : $serviceParam; return $rule->constructUrl($getItems,$encodeAmpersand,$encodeGetItems); - } + } } } } @@ -413,8 +441,8 @@ class TUrlMappingPattern extends TComponent private $_manager; private $_caseSensitive=true; - - private $_isWildCardPattern=false; + + private $_isWildCardPattern=false; /** * Constructor. @@ -444,8 +472,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; + if(strpos($this->_serviceParameter,'*')!==false) + $this->_isWildCardPattern=true; } /** @@ -462,11 +490,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().'>[^=/]+)'; - } + if ($this->getIsWildCardPattern()) { + $params[]='{*}'; + // service parameter must not contain '=' and '/' + $values[]='(?P<'.$this->getServiceID().'>[^=/]+)'; + } $params[]='/'; $values[]='\\/'; $regexp=str_replace($params,$values,trim($this->getPattern(),'/').'/'); @@ -608,13 +636,13 @@ class TUrlMappingPattern extends TComponent } /** - * @return boolean whether this pattern is a wildcard pattern + * @return boolean whether this pattern is a wildcard pattern * @since 3.1.4 */ - public function getIsWildCardPattern() { - return $this->_isWildCardPattern; - } - + 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. -- cgit v1.2.3