From 29130c17def4e63475b3fd775f48832e8d07bda0 Mon Sep 17 00:00:00 2001 From: xue <> Date: Sun, 2 Apr 2006 15:39:57 +0000 Subject: Added TParameterModule. --- framework/Util/TParameterModule.php | 131 ++++++++++++++++++++++++++++++++++++ 1 file changed, 131 insertions(+) create mode 100644 framework/Util/TParameterModule.php (limited to 'framework/Util') diff --git a/framework/Util/TParameterModule.php b/framework/Util/TParameterModule.php new file mode 100644 index 00000000..a72e8fe8 --- /dev/null +++ b/framework/Util/TParameterModule.php @@ -0,0 +1,131 @@ + + * @link http://www.pradosoft.com/ + * @copyright Copyright © 2005 PradoSoft + * @license http://www.pradosoft.com/license/ + * @version $Revision: $ $Date: $ + * @package System.Util + */ + +/** + * TParameterModule class + * + * TParameterModule enables loading application parameters from external + * storage other than the application configuration. + * To load parameters from an XML file, configure the module by setting + * its {@link setParameterFile ParameterFile} property. + * Note, the property only accepts a file path in namespace format with + * file extension being '.xml'. The file format is as follows, which is + * similar to the parameter portion in an application configuration, + * + * + * + * + * + * + * + * In addition, any content enclosed within the module tag is also treated + * as parameters, e.g., + * + * + * + * + * + * + * + * If a parameter is defined both in the external file and within the module + * tag, the former takes precedence. + * + * @author Qiang Xue + * @version $Revision: $ $Date: $ + * @package System.Util + * @since 3.0 + */ +class TParameterModule extends TModule +{ + const PARAM_FILE_EXT='.xml'; + private $_initialized=false; + private $_paramFile=null; + + /** + * Initializes the module by loading parameters. + * @param TXmlElement content enclosed within the module tag + */ + public function init($config) + { + $this->loadParameters($config); + if($this->_paramFile!==null) + { + $dom=new TXmlDocument; + $dom->loadFromFile($this->_paramFile); + $this->loadParameters($dom); + } + $this->_initialized=true; + } + + /** + * Loads parameters into application. + * @param TXmlElement XML representation of the parameters + * @throws TConfigurationException if the parameter file format is invalid + */ + protected function loadParameters($xmlNode) + { + $parameters=array(); + foreach($xmlNode->getElementsByTagName('parameter') as $node) + { + $properties=$node->getAttributes(); + if(($id=$properties->remove('id'))===null) + throw new TConfigurationException('parametermodule_parameterid_required'); + if(($type=$properties->remove('class'))===null) + { + if(($value=$properties->remove('value'))===null) + $parameters[$id]=$node; + else + $parameters[$id]=$value; + } + else + $parameters[$id]=array($type,$properties->toArray()); + } + + $appParams=$this->getApplication()->getParameters(); + foreach($parameters as $id=>$parameter) + { + if(is_array($parameter)) + { + $component=Prado::createComponent($parameter[0]); + foreach($parameter[1] as $name=>$value) + $component->setSubProperty($name,$value); + $appParams->add($id,$component); + } + else + $appParams->add($id,$parameter); + } + } + + /** + * @return string the parameter file path + */ + public function getParameterFile() + { + return $this->_paramFile; + } + + /** + * @param string the parameter file path. It must be in namespace format + * and the file extension is '.xml'. + * @throws TInvalidOperationException if the module is initialized + * @throws TConfigurationException if the file is invalid + */ + public function setParameterFile($value) + { + if($this->_initialized) + throw new TInvalidOperationException('parametermodule_parameterfile_unchangeable'); + else if(($this->_paramFile=Prado::getPathOfNamespace($value,self::PARAM_FILE_EXT))===null || !is_file($this->_paramFile)) + throw new TConfigurationException('parametermodule_parameterfile_invalid',$value); + } +} + +?> \ No newline at end of file -- cgit v1.2.3