3 * TParameterModule class
5 * @author Qiang Xue <qiang.xue@gmail.com>
6 * @link https://github.com/pradosoft/prado
7 * @copyright Copyright © 2005-2016 The PRADO Group
8 * @license https://github.com/pradosoft/prado/blob/master/COPYRIGHT
13 * TParameterModule class
15 * TParameterModule enables loading application parameters from external
16 * storage other than the application configuration.
17 * To load parameters from an XML file, configure the module by setting
18 * its {@link setParameterFile ParameterFile} property.
19 * Note, the property only accepts a file path in namespace format with
20 * file extension being '.xml'. The file format is as follows, which is
21 * similar to the parameter portion in an application configuration,
24 * <parameter id="param1" value="paramValue1" />
25 * <parameter id="param2" Property1="Value1" Property2="Value2" ... />
29 * In addition, any content enclosed within the module tag is also treated
30 * as parameters, e.g.,
32 * <module class="System.Util.TParameterModule">
33 * <parameter id="param1" value="paramValue1" />
34 * <parameter id="param2" Property1="Value1" Property2="Value2" ... />
38 * If a parameter is defined both in the external file and within the module
39 * tag, the former takes precedence.
41 * @author Qiang Xue <qiang.xue@gmail.com>
42 * @author Carl G. Mathisen <carlgmathisen@gmail.com>
43 * @package System.Util
46 class TParameterModule extends TModule
49 * @deprecated since 3.2
51 const PARAM_FILE_EXT='.xml';
52 private $_initialized=false;
53 private $_paramFile=null;
56 * Initializes the module by loading parameters.
57 * @param mixed content enclosed within the module tag
59 public function init($config)
61 $this->loadParameters($config);
62 if($this->_paramFile!==null)
65 if($this->getApplication()->getConfigurationType()==TApplication::CONFIG_TYPE_XML && ($cache=$this->getApplication()->getCache())!==null)
67 $cacheKey='TParameterModule:'.$this->_paramFile;
68 if(($configFile=$cache->get($cacheKey))===false)
70 $configFile=new TXmlDocument;
71 $configFile->loadFromFile($this->_paramFile);
72 $cache->set($cacheKey,$configFile,0,new TFileCacheDependency($this->_paramFile));
77 if($this->getApplication()->getConfigurationType()==TApplication::CONFIG_TYPE_PHP)
79 $configFile = include $this->_paramFile;
83 $configFile=new TXmlDocument;
84 $configFile->loadFromFile($this->_paramFile);
87 $this->loadParameters($configFile);
89 $this->_initialized=true;
93 * Loads parameters into application.
94 * @param mixed XML of PHP representation of the parameters
95 * @throws TConfigurationException if the parameter file format is invalid
97 protected function loadParameters($config)
100 if(is_array($config))
102 foreach($config as $id => $parameter)
104 if(is_array($parameter) && isset($parameter['class']))
106 $properties = isset($parameter['properties'])?$parameter['properties']:array();
107 $parameters[$id]=array($parameter['class'],$properties);
111 $parameters[$id] = $parameter;
115 else if($config instanceof TXmlElement)
117 foreach($config->getElementsByTagName('parameter') as $node)
119 $properties=$node->getAttributes();
120 if(($id=$properties->remove('id'))===null)
121 throw new TConfigurationException('parametermodule_parameterid_required');
122 if(($type=$properties->remove('class'))===null)
124 if(($value=$properties->remove('value'))===null)
125 $parameters[$id]=$node;
127 $parameters[$id]=$value;
130 $parameters[$id]=array($type,$properties->toArray());
134 $appParams=$this->getApplication()->getParameters();
135 foreach($parameters as $id=>$parameter)
137 if(is_array($parameter))
139 $component=Prado::createComponent($parameter[0]);
140 foreach($parameter[1] as $name=>$value)
141 $component->setSubProperty($name,$value);
142 $appParams->add($id,$component);
145 $appParams->add($id,$parameter);
150 * @return string the parameter file path
152 public function getParameterFile()
154 return $this->_paramFile;
158 * @param string the parameter file path. It must be in namespace format
159 * and the file extension is '.xml'.
160 * @throws TInvalidOperationException if the module is initialized
161 * @throws TConfigurationException if the file is invalid
163 public function setParameterFile($value)
165 if($this->_initialized)
166 throw new TInvalidOperationException('parametermodule_parameterfile_unchangeable');
167 else if(($this->_paramFile=Prado::getPathOfNamespace($value,$this->getApplication()->getConfigurationFileExt()))===null || !is_file($this->_paramFile))
168 throw new TConfigurationException('parametermodule_parameterfile_invalid',$value);