]> git.sur5r.net Git - bacula/bacula/blob - gui/baculum/framework/Util/TParameterModule.php
Add Baculum
[bacula/bacula] / gui / baculum / framework / Util / TParameterModule.php
1 <?php
2 /**
3  * TParameterModule class
4  *
5  * @author Qiang Xue <qiang.xue@gmail.com>
6  * @link http://www.pradosoft.com/
7  * @copyright Copyright &copy; 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
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  * @author Carl G. Mathisen <carlgmathisen@gmail.com>
44  * @version $Id: TParameterModule.php 3245 2013-01-07 20:23:32Z ctrlaltca $
45  * @package System.Util
46  * @since 3.0
47  */
48 class TParameterModule extends TModule
49 {
50         /**
51          * @deprecated since 3.2
52          */
53         const PARAM_FILE_EXT='.xml';
54         private $_initialized=false;
55         private $_paramFile=null;
56
57         /**
58          * Initializes the module by loading parameters.
59          * @param mixed content enclosed within the module tag
60          */
61         public function init($config)
62         {
63                 $this->loadParameters($config);
64                 if($this->_paramFile!==null)
65                 {
66                         $configFile = null;
67                         if($this->getApplication()->getConfigurationType()==TApplication::CONFIG_TYPE_XML && ($cache=$this->getApplication()->getCache())!==null)
68                         {
69                                 $cacheKey='TParameterModule:'.$this->_paramFile;
70                                 if(($configFile=$cache->get($cacheKey))===false)
71                                 {
72                                         $configFile=new TXmlDocument;
73                                         $configFile->loadFromFile($this->_paramFile);
74                                         $cache->set($cacheKey,$configFile,0,new TFileCacheDependency($this->_paramFile));
75                                 }
76                         }
77                         else
78                         {
79                                 if($this->getApplication()->getConfigurationType()==TApplication::CONFIG_TYPE_PHP)
80                                 {
81                                         $configFile = include $this->_paramFile;
82                                 }
83                                 else
84                                 {
85                                         $configFile=new TXmlDocument;
86                                         $configFile->loadFromFile($this->_paramFile);
87                                 }
88                         }
89                         $this->loadParameters($configFile);
90                 }
91                 $this->_initialized=true;
92         }
93
94         /**
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
98          */
99         protected function loadParameters($config)
100         {
101                 $parameters=array();
102                 if(is_array($config))
103                 {
104                         foreach($config as $id => $parameter)
105                         {
106                                 if(is_array($parameter) && isset($parameter['class']))
107                                 {
108                                         $properties = isset($parameter['properties'])?$parameter['properties']:array();
109                                         $parameters[$id]=array($parameter['class'],$properties);
110                                 }
111                                 else
112                                 {
113                                         $parameters[$id] = $parameter;
114                                 }
115                         }
116                 }
117                 else if($config instanceof TXmlElement)
118                 {
119                         foreach($config->getElementsByTagName('parameter') as $node)
120                         {
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)
125                                 {
126                                         if(($value=$properties->remove('value'))===null)
127                                                 $parameters[$id]=$node;
128                                         else
129                                                 $parameters[$id]=$value;
130                                 }
131                                 else
132                                         $parameters[$id]=array($type,$properties->toArray());
133                         }
134                 }
135
136                 $appParams=$this->getApplication()->getParameters();
137                 foreach($parameters as $id=>$parameter)
138                 {
139                         if(is_array($parameter))
140                         {
141                                 $component=Prado::createComponent($parameter[0]);
142                                 foreach($parameter[1] as $name=>$value)
143                                         $component->setSubProperty($name,$value);
144                                 $appParams->add($id,$component);
145                         }
146                         else
147                                 $appParams->add($id,$parameter);
148                 }
149         }
150
151         /**
152          * @return string the parameter file path
153          */
154         public function getParameterFile()
155         {
156                 return $this->_paramFile;
157         }
158
159         /**
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
164          */
165         public function setParameterFile($value)
166         {
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);
171         }
172 }
173