| 1 |
lars |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* TParameterModule class
|
|
|
4 |
*
|
|
|
5 |
* @author Qiang Xue <qiang.xue@gmail.com>
|
|
|
6 |
* @link http://www.pradosoft.com/
|
|
|
7 |
* @copyright Copyright © 2005-2008 PradoSoft
|
|
|
8 |
* @license http://www.pradosoft.com/license/
|
|
|
9 |
* @version $Id: TParameterModule.php 2541 2008-10-21 15:05:13Z qiang.xue $
|
|
|
10 |
* @package System.Util
|
|
|
11 |
*/
|
|
|
12 |
|
|
|
13 |
/**
|
|
|
14 |
* TParameterModule class
|
|
|
15 |
*
|
|
|
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,
|
|
|
23 |
* <code>
|
|
|
24 |
* <parameters>
|
|
|
25 |
* <parameter id="param1" value="paramValue1" />
|
|
|
26 |
* <parameter id="param2" Property1="Value1" Property2="Value2" ... />
|
|
|
27 |
* </parameters>
|
|
|
28 |
* </code>
|
|
|
29 |
*
|
|
|
30 |
* In addition, any content enclosed within the module tag is also treated
|
|
|
31 |
* as parameters, e.g.,
|
|
|
32 |
* <code>
|
|
|
33 |
* <module class="System.Util.TParameterModule">
|
|
|
34 |
* <parameter id="param1" value="paramValue1" />
|
|
|
35 |
* <parameter id="param2" Property1="Value1" Property2="Value2" ... />
|
|
|
36 |
* </module>
|
|
|
37 |
* </code>
|
|
|
38 |
*
|
|
|
39 |
* If a parameter is defined both in the external file and within the module
|
|
|
40 |
* tag, the former takes precedence.
|
|
|
41 |
*
|
|
|
42 |
* @author Qiang Xue <qiang.xue@gmail.com>
|
|
|
43 |
* @version $Id: TParameterModule.php 2541 2008-10-21 15:05:13Z qiang.xue $
|
|
|
44 |
* @package System.Util
|
|
|
45 |
* @since 3.0
|
|
|
46 |
*/
|
|
|
47 |
class TParameterModule extends TModule
|
|
|
48 |
{
|
|
|
49 |
const PARAM_FILE_EXT='.xml';
|
|
|
50 |
private $_initialized=false;
|
|
|
51 |
private $_paramFile=null;
|
|
|
52 |
|
|
|
53 |
/**
|
|
|
54 |
* Initializes the module by loading parameters.
|
|
|
55 |
* @param TXmlElement content enclosed within the module tag
|
|
|
56 |
*/
|
|
|
57 |
public function init($config)
|
|
|
58 |
{
|
|
|
59 |
$this->loadParameters($config);
|
|
|
60 |
if($this->_paramFile!==null)
|
|
|
61 |
{
|
|
|
62 |
if(($cache=$this->getApplication()->getCache())!==null)
|
|
|
63 |
{
|
|
|
64 |
$cacheKey='TParameterModule:'.$this->_paramFile;
|
|
|
65 |
if(($dom=$cache->get($cacheKey))===false)
|
|
|
66 |
{
|
|
|
67 |
$dom=new TXmlDocument;
|
|
|
68 |
$dom->loadFromFile($this->_paramFile);
|
|
|
69 |
$cache->set($cacheKey,$dom,0,new TFileCacheDependency($this->_paramFile));
|
|
|
70 |
}
|
|
|
71 |
}
|
|
|
72 |
else
|
|
|
73 |
{
|
|
|
74 |
$dom=new TXmlDocument;
|
|
|
75 |
$dom->loadFromFile($this->_paramFile);
|
|
|
76 |
}
|
|
|
77 |
$this->loadParameters($dom);
|
|
|
78 |
}
|
|
|
79 |
$this->_initialized=true;
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
/**
|
|
|
83 |
* Loads parameters into application.
|
|
|
84 |
* @param TXmlElement XML representation of the parameters
|
|
|
85 |
* @throws TConfigurationException if the parameter file format is invalid
|
|
|
86 |
*/
|
|
|
87 |
protected function loadParameters($xmlNode)
|
|
|
88 |
{
|
|
|
89 |
$parameters=array();
|
|
|
90 |
foreach($xmlNode->getElementsByTagName('parameter') as $node)
|
|
|
91 |
{
|
|
|
92 |
$properties=$node->getAttributes();
|
|
|
93 |
if(($id=$properties->remove('id'))===null)
|
|
|
94 |
throw new TConfigurationException('parametermodule_parameterid_required');
|
|
|
95 |
if(($type=$properties->remove('class'))===null)
|
|
|
96 |
{
|
|
|
97 |
if(($value=$properties->remove('value'))===null)
|
|
|
98 |
$parameters[$id]=$node;
|
|
|
99 |
else
|
|
|
100 |
$parameters[$id]=$value;
|
|
|
101 |
}
|
|
|
102 |
else
|
|
|
103 |
$parameters[$id]=array($type,$properties->toArray());
|
|
|
104 |
}
|
|
|
105 |
|
|
|
106 |
$appParams=$this->getApplication()->getParameters();
|
|
|
107 |
foreach($parameters as $id=>$parameter)
|
|
|
108 |
{
|
|
|
109 |
if(is_array($parameter))
|
|
|
110 |
{
|
|
|
111 |
$component=Prado::createComponent($parameter[0]);
|
|
|
112 |
foreach($parameter[1] as $name=>$value)
|
|
|
113 |
$component->setSubProperty($name,$value);
|
|
|
114 |
$appParams->add($id,$component);
|
|
|
115 |
}
|
|
|
116 |
else
|
|
|
117 |
$appParams->add($id,$parameter);
|
|
|
118 |
}
|
|
|
119 |
}
|
|
|
120 |
|
|
|
121 |
/**
|
|
|
122 |
* @return string the parameter file path
|
|
|
123 |
*/
|
|
|
124 |
public function getParameterFile()
|
|
|
125 |
{
|
|
|
126 |
return $this->_paramFile;
|
|
|
127 |
}
|
|
|
128 |
|
|
|
129 |
/**
|
|
|
130 |
* @param string the parameter file path. It must be in namespace format
|
|
|
131 |
* and the file extension is '.xml'.
|
|
|
132 |
* @throws TInvalidOperationException if the module is initialized
|
|
|
133 |
* @throws TConfigurationException if the file is invalid
|
|
|
134 |
*/
|
|
|
135 |
public function setParameterFile($value)
|
|
|
136 |
{
|
|
|
137 |
if($this->_initialized)
|
|
|
138 |
throw new TInvalidOperationException('parametermodule_parameterfile_unchangeable');
|
|
|
139 |
else if(($this->_paramFile=Prado::getPathOfNamespace($value,self::PARAM_FILE_EXT))===null || !is_file($this->_paramFile))
|
|
|
140 |
throw new TConfigurationException('parametermodule_parameterfile_invalid',$value);
|
|
|
141 |
}
|
|
|
142 |
}
|
|
|
143 |
|