summaryrefslogtreecommitdiff
path: root/framework/Web/TUrlMapping.php
diff options
context:
space:
mode:
Diffstat (limited to 'framework/Web/TUrlMapping.php')
-rw-r--r--framework/Web/TUrlMapping.php194
1 files changed, 111 insertions, 83 deletions
diff --git a/framework/Web/TUrlMapping.php b/framework/Web/TUrlMapping.php
index 47232401..09e45d99 100644
--- a/framework/Web/TUrlMapping.php
+++ b/framework/Web/TUrlMapping.php
@@ -70,10 +70,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.
*/
protected $_patterns=array();
@@ -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))
+ {
+ 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
{
- $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;
+ 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;
}
@@ -450,11 +478,11 @@ class TUrlMappingPattern extends TComponent
private $_caseSensitive=true;
- private $_isWildCardPattern=false;
-
- private $_urlFormat=THttpRequestUrlFormat::Get;
-
- private $_separator='/';
+ private $_isWildCardPattern=false;
+
+ private $_urlFormat=THttpRequestUrlFormat::Get;
+
+ private $_separator='/';
/**
* Constructor.
@@ -676,57 +704,57 @@ 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;
- }
-
- /**
- * @return THttpRequestUrlFormat the format of URLs. Defaults to THttpRequestUrlFormat::Get.
- */
- public function getUrlFormat()
- {
- return $this->_urlFormat;
- }
-
- /**
- * Sets the format of URLs constructed and interpreted by this pattern.
- * A Get URL format is like index.php?name1=value1&name2=value2
- * while a Path URL format is like index.php/name1/value1/name2/value.
- * The separating character between name and value can be configured with
- * {@link setUrlParamSeparator} and defaults to '/'.
- * Changing the UrlFormat will affect {@link constructUrl} and how GET variables
- * are parsed.
- * @param THttpRequestUrlFormat the format of URLs.
- * @param since 3.1.4
- */
- public function setUrlFormat($value)
- {
- $this->_urlFormat=TPropertyValue::ensureEnum($value,'THttpRequestUrlFormat');
- }
-
- /**
- * @return string separator used to separate GET variable name and value when URL format is Path. Defaults to slash '/'.
- */
- public function getUrlParamSeparator()
- {
- return $this->_separator;
- }
-
- /**
- * @param string separator used to separate GET variable name and value when URL format is Path.
- * @throws TInvalidDataValueException if the separator is not a single character
- */
- public function setUrlParamSeparator($value)
- {
- if(strlen($value)===1)
- $this->_separator=$value;
- else
- throw new TInvalidDataValueException('httprequest_separator_invalid');
- }
-
+ public function getIsWildCardPattern() {
+ return $this->_isWildCardPattern;
+ }
+
+ /**
+ * @return THttpRequestUrlFormat the format of URLs. Defaults to THttpRequestUrlFormat::Get.
+ */
+ public function getUrlFormat()
+ {
+ return $this->_urlFormat;
+ }
+
+ /**
+ * Sets the format of URLs constructed and interpreted by this pattern.
+ * A Get URL format is like index.php?name1=value1&name2=value2
+ * while a Path URL format is like index.php/name1/value1/name2/value.
+ * The separating character between name and value can be configured with
+ * {@link setUrlParamSeparator} and defaults to '/'.
+ * Changing the UrlFormat will affect {@link constructUrl} and how GET variables
+ * are parsed.
+ * @param THttpRequestUrlFormat the format of URLs.
+ * @param since 3.1.4
+ */
+ public function setUrlFormat($value)
+ {
+ $this->_urlFormat=TPropertyValue::ensureEnum($value,'THttpRequestUrlFormat');
+ }
+
+ /**
+ * @return string separator used to separate GET variable name and value when URL format is Path. Defaults to slash '/'.
+ */
+ public function getUrlParamSeparator()
+ {
+ return $this->_separator;
+ }
+
+ /**
+ * @param string separator used to separate GET variable name and value when URL format is Path.
+ * @throws TInvalidDataValueException if the separator is not a single character
+ */
+ public function setUrlParamSeparator($value)
+ {
+ if(strlen($value)===1)
+ $this->_separator=$value;
+ else
+ throw new TInvalidDataValueException('httprequest_separator_invalid');
+ }
+
/**
* @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.