diff options
author | xue <> | 2006-04-02 15:39:57 +0000 |
---|---|---|
committer | xue <> | 2006-04-02 15:39:57 +0000 |
commit | 29130c17def4e63475b3fd775f48832e8d07bda0 (patch) | |
tree | 941a62e23743b2dfc5ed8a109cab6bdf12a0edf0 | |
parent | 64e629c5336084f39d36405bda2e78f15367573c (diff) |
Added TParameterModule.
-rw-r--r-- | .gitattributes | 1 | ||||
-rw-r--r-- | HISTORY | 1 | ||||
-rw-r--r-- | demos/quickstart/protected/pages/Configurations/AppConfig.page | 2 | ||||
-rw-r--r-- | framework/Exceptions/messages.txt | 6 | ||||
-rw-r--r-- | framework/Security/TUserManager.php | 22 | ||||
-rw-r--r-- | framework/Util/TParameterModule.php | 131 | ||||
-rw-r--r-- | framework/Web/Services/TPageService.php | 6 |
7 files changed, 154 insertions, 15 deletions
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 @@ -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 <tt>application. <com:TTextHighlighter Language="xml" CssClass="source">
<parameter id="ParameterID" value="ParameterValue" />
</com:TTextHighlighter>
-Note, if the <tt>value</tt> attribute is not specified, the whole parameter XML node (of type <tt>TXmlElement</tt>) will be returned as the parameter value.
+Note, if the <tt>value</tt> attribute is not specified, the whole parameter XML node (of type <tt>TXmlElement</tt>) will be returned as the parameter value. In addition, the <tt>System.Util.TParameterModule</tt> module provides a way to load parameters from an external XML file. See more details in its API documentation.
</li>
</ul>
<p>
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,
+ * <code>
* <module id="users" class="System.Security.TUserManager" PasswordMode="Clear">
* <user name="Joe" password="demo" />
* <user name="John" password="demo" />
* <role name="Administrator" users="John" />
* <role name="Writer" users="Joe,John" />
* </module>
+ * </code>
+ *
+ * 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 <b>Clear</b>, <b>SH1</b> or <b>MD5</b>.
@@ -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 @@ +<?php
+/**
+ * TParameterModule class
+ *
+ * @author Qiang Xue <qiang.xue@gmail.com>
+ * @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,
+ * <code>
+ * <parameters>
+ * <parameter id="param1" value="paramValue1" />
+ * <parameter id="param2" Property1="Value1" Property2="Value2" ... />
+ * </parameters>
+ * </code>
+ *
+ * In addition, any content enclosed within the module tag is also treated
+ * as parameters, e.g.,
+ * <code>
+ * <module class="System.Util.TParameterModule">
+ * <parameter id="param1" value="paramValue1" />
+ * <parameter id="param2" Property1="Value1" Property2="Value2" ... />
+ * </module>
+ * </code>
+ *
+ * If a parameter is defined both in the external file and within the module
+ * tag, the former takes precedence.
+ *
+ * @author Qiang Xue <qiang.xue@gmail.com>
+ * @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
|