3 * TParameterModule class
5 * @author Qiang Xue <qiang.xue@gmail.com>
6 * @link http://www.pradosoft.com/
7 * @copyright Copyright © 2005-2013 PradoSoft
8 * @license http://www.pradosoft.com/license/
9 * @version $Id: TParameterModule.php 3245 2013-01-07 20:23:32Z ctrlaltca $
10 * @package System.Util
14 * TParameterModule class
16 * TParameterModule enables loading application parameters from external
17 * storage other than the application configuration.
18 * To load parameters from an XML file, configure the module by setting
19 * its {@link setParameterFile ParameterFile} property.
20 * Note, the property only accepts a file path in namespace format with
21 * file extension being '.xml'. The file format is as follows, which is
22 * similar to the parameter portion in an application configuration,
25 * <parameter id="param1" value="paramValue1" />
26 * <parameter id="param2" Property1="Value1" Property2="Value2" ... />
30 * In addition, any content enclosed within the module tag is also treated
31 * as parameters, e.g.,
33 * <module class="System.Util.TParameterModule">
34 * <parameter id="param1" value="paramValue1" />
35 * <parameter id="param2" Property1="Value1" Property2="Value2" ... />
39 * If a parameter is defined both in the external file and within the module
40 * tag, the former takes precedence.
42 * @author Qiang Xue <qiang.xue@gmail.com>
43 * @author Carl G. Mathisen <carlgmathisen@gmail.com>
44 * @version $Id: TParameterModule.php 3245 2013-01-07 20:23:32Z ctrlaltca $
45 * @package System.Util
48 class TParameterModule extends TModule
51 * @deprecated since 3.2
53 const PARAM_FILE_EXT='.xml';
54 private $_initialized=false;
55 private $_paramFile=null;
58 * Initializes the module by loading parameters.
59 * @param mixed content enclosed within the module tag
61 public function init($config)
63 $this->loadParameters($config);
64 if($this->_paramFile!==null)
67 if($this->getApplication()->getConfigurationType()==TApplication::CONFIG_TYPE_XML && ($cache=$this->getApplication()->getCache())!==null)
69 $cacheKey='TParameterModule:'.$this->_paramFile;
70 if(($configFile=$cache->get($cacheKey))===false)
72 $configFile=new TXmlDocument;
73 $configFile->loadFromFile($this->_paramFile);
74 $cache->set($cacheKey,$configFile,0,new TFileCacheDependency($this->_paramFile));
79 if($this->getApplication()->getConfigurationType()==TApplication::CONFIG_TYPE_PHP)
81 $configFile = include $this->_paramFile;
85 $configFile=new TXmlDocument;
86 $configFile->loadFromFile($this->_paramFile);
89 $this->loadParameters($configFile);
91 $this->_initialized=true;
95 * Loads parameters into application.
96 * @param mixed XML of PHP representation of the parameters
97 * @throws TConfigurationException if the parameter file format is invalid
99 protected function loadParameters($config)
102 if(is_array($config))
104 foreach($config as $id => $parameter)
106 if(is_array($parameter) && isset($parameter['class']))
108 $properties = isset($parameter['properties'])?$parameter['properties']:array();
109 $parameters[$id]=array($parameter['class'],$properties);
113 $parameters[$id] = $parameter;
117 else if($config instanceof TXmlElement)
119 foreach($config->getElementsByTagName('parameter') as $node)
121 $properties=$node->getAttributes();
122 if(($id=$properties->remove('id'))===null)
123 throw new TConfigurationException('parametermodule_parameterid_required');
124 if(($type=$properties->remove('class'))===null)
126 if(($value=$properties->remove('value'))===null)
127 $parameters[$id]=$node;
129 $parameters[$id]=$value;
132 $parameters[$id]=array($type,$properties->toArray());
136 $appParams=$this->getApplication()->getParameters();
137 foreach($parameters as $id=>$parameter)
139 if(is_array($parameter))
141 $component=Prado::createComponent($parameter[0]);
142 foreach($parameter[1] as $name=>$value)
143 $component->setSubProperty($name,$value);
144 $appParams->add($id,$component);
147 $appParams->add($id,$parameter);
152 * @return string the parameter file path
154 public function getParameterFile()
156 return $this->_paramFile;
160 * @param string the parameter file path. It must be in namespace format
161 * and the file extension is '.xml'.
162 * @throws TInvalidOperationException if the module is initialized
163 * @throws TConfigurationException if the file is invalid
165 public function setParameterFile($value)
167 if($this->_initialized)
168 throw new TInvalidOperationException('parametermodule_parameterfile_unchangeable');
169 else if(($this->_paramFile=Prado::getPathOfNamespace($value,$this->getApplication()->getConfigurationFileExt()))===null || !is_file($this->_paramFile))
170 throw new TConfigurationException('parametermodule_parameterfile_invalid',$value);