From 29130c17def4e63475b3fd775f48832e8d07bda0 Mon Sep 17 00:00:00 2001 From: xue <> Date: Sun, 2 Apr 2006 15:39:57 +0000 Subject: Added TParameterModule. --- .gitattributes | 1 + HISTORY | 1 + .../protected/pages/Configurations/AppConfig.page | 2 +- framework/Exceptions/messages.txt | 6 +- framework/Security/TUserManager.php | 22 ++-- framework/Util/TParameterModule.php | 131 +++++++++++++++++++++ framework/Web/Services/TPageService.php | 6 +- 7 files changed, 154 insertions(+), 15 deletions(-) create mode 100644 framework/Util/TParameterModule.php diff --git a/.gitattributes b/.gitattributes index 6081156b..d00acc93 100644 --- a/.gitattributes +++ b/.gitattributes @@ -670,6 +670,7 @@ framework/TService.php -text framework/Util/TDataFieldAccessor.php -text framework/Util/TLogRouter.php -text framework/Util/TLogger.php -text +framework/Util/TParameterModule.php -text framework/Util/TSimpleDateFormatter.php -text framework/Util/TVarDumper.php -text framework/Web/Javascripts/TJSON.php -text diff --git a/HISTORY b/HISTORY index 1c2d0534..b41eac85 100644 --- a/HISTORY +++ b/HISTORY @@ -25,6 +25,7 @@ NEW: TImageMap control (Qiang) NEW: TWizard control (Qiang) NEW: TVarDumper and PradoBase::varDump() (Qiang) NEW: TComponentReflection (Qiang) +NEW: TParameterModule (Qiang) Version 3.0b March 6, 2006 ========================== diff --git a/demos/quickstart/protected/pages/Configurations/AppConfig.page b/demos/quickstart/protected/pages/Configurations/AppConfig.page index d617a280..48243200 100644 --- a/demos/quickstart/protected/pages/Configurations/AppConfig.page +++ b/demos/quickstart/protected/pages/Configurations/AppConfig.page @@ -33,7 +33,7 @@ Configuration for an application is stored in an XML file named application. -Note, if the value attribute is not specified, the whole parameter XML node (of type TXmlElement) will be returned as the parameter value. +Note, if the value attribute is not specified, the whole parameter XML node (of type TXmlElement) will be returned as the parameter value. In addition, the System.Util.TParameterModule module provides a way to load parameters from an external XML file. See more details in its API documentation.

diff --git a/framework/Exceptions/messages.txt b/framework/Exceptions/messages.txt index b4f81c8b..dd680098 100644 --- a/framework/Exceptions/messages.txt +++ b/framework/Exceptions/messages.txt @@ -273,4 +273,8 @@ texthighlighter_stylesheet_invalid = Unable to find the stylesheet file for TTe hotspotcollection_hotspot_required = THotSpotCollection can only accept instance of THotSpot or its derived classes. htmlarea_textmode_readonly = THtmlArea.TextMode is read-only. -htmlarea_tarfile_invalid = THtmlArea is unable to locate the TinyMCE tar file. \ No newline at end of file +htmlarea_tarfile_invalid = THtmlArea is unable to locate the TinyMCE tar file. + +parametermodule_parameterfile_unchangeable = TParameterModule.ParameterFile is not changeable because the module is already initialized. +parametermodule_parameterfile_invalid = TParameterModule.ParameterFile '{0}' is invalid. Make sure it is in namespace format and the file extension is '.xml'. +parametermodule_parameterid_required = Parameter element must have 'id' attribute. \ No newline at end of file diff --git a/framework/Security/TUserManager.php b/framework/Security/TUserManager.php index 5ad582e1..5bd18d0c 100644 --- a/framework/Security/TUserManager.php +++ b/framework/Security/TUserManager.php @@ -168,12 +168,19 @@ class TUser extends TComponent implements IUser * * TUserManager manages a static list of users {@link TUser}. * The user information is specified via module configuration using the following XML syntax, + * * * * * * * + * + * + * In addition, user information can also be loaded from an external file + * specified by {@link setUserFile UserFile} property. Note, the property + * only accepts a file path in namespace format. The user file format is + * similar to the above sample. * * The user passwords may be specified as clear text, SH1 or MD5 hashed by setting * {@link setPasswordMode PasswordMode} as Clear, SH1 or MD5. @@ -228,18 +235,13 @@ class TUserManager extends TModule */ public function init($config) { + $this->loadUserData($config); if($this->_userFile!==null) { - if(is_file($this->_userFile)) - { - $dom=new TXmlDocument; - $dom->loadFromFile($this->_userFile); - $this->loadUserData($dom); - } - else - throw new TConfigurationException('usermanager_userfile_invalid',$this->_userFile); + $dom=new TXmlDocument; + $dom->loadFromFile($this->_userFile); + $this->loadUserData($dom); } - $this->loadUserData($config); $this->_initialized=true; } @@ -287,7 +289,7 @@ class TUserManager extends TModule { if($this->_initialized) throw new TInvalidOperationException('usermanager_userfile_unchangeable'); - else if(($this->_userFile=Prado::getPathOfNamespace($value,self::USER_FILE_EXT))===null) + else if(($this->_userFile=Prado::getPathOfNamespace($value,self::USER_FILE_EXT))===null || !is_file($this->_userFile)) throw new TConfigurationException('usermanager_userfile_invalid',$value); } 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 diff --git a/framework/Web/Services/TPageService.php b/framework/Web/Services/TPageService.php index 022c1286..8590540f 100644 --- a/framework/Web/Services/TPageService.php +++ b/framework/Web/Services/TPageService.php @@ -170,15 +170,15 @@ class TPageService extends TService $parameters=$application->getParameters(); foreach($pageConfig->getParameters() as $id=>$parameter) { - if(is_string($parameter)) - $parameters->add($id,$parameter); - else + if(is_array($parameter)) { $component=Prado::createComponent($parameter[0]); foreach($parameter[1] as $name=>$value) $component->setSubProperty($name,$value); $parameters->add($id,$component); } + else + $parameters->add($id,$parameter); } // load modules specified in page directory config -- cgit v1.2.3