]> git.sur5r.net Git - bacula/bacula/blob - gui/baculum/framework/Util/TParameterModule.php
baculum: New Baculum API and Baculum Web
[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 https://github.com/pradosoft/prado
7  * @copyright Copyright &copy; 2005-2016 The PRADO Group
8  * @license https://github.com/pradosoft/prado/blob/master/COPYRIGHT
9  * @package System.Util
10  */
11
12 /**
13  * TParameterModule class
14  *
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,
22  * <code>
23  * <parameters>
24  *   <parameter id="param1" value="paramValue1" />
25  *   <parameter id="param2" Property1="Value1" Property2="Value2" ... />
26  * </parameters>
27  * </code>
28  *
29  * In addition, any content enclosed within the module tag is also treated
30  * as parameters, e.g.,
31  * <code>
32  * <module class="System.Util.TParameterModule">
33  *   <parameter id="param1" value="paramValue1" />
34  *   <parameter id="param2" Property1="Value1" Property2="Value2" ... />
35  * </module>
36  * </code>
37  *
38  * If a parameter is defined both in the external file and within the module
39  * tag, the former takes precedence.
40  *
41  * @author Qiang Xue <qiang.xue@gmail.com>
42  * @author Carl G. Mathisen <carlgmathisen@gmail.com>
43  * @package System.Util
44  * @since 3.0
45  */
46 class TParameterModule extends TModule
47 {
48         /**
49          * @deprecated since 3.2
50          */
51         const PARAM_FILE_EXT='.xml';
52         private $_initialized=false;
53         private $_paramFile=null;
54
55         /**
56          * Initializes the module by loading parameters.
57          * @param mixed content enclosed within the module tag
58          */
59         public function init($config)
60         {
61                 $this->loadParameters($config);
62                 if($this->_paramFile!==null)
63                 {
64                         $configFile = null;
65                         if($this->getApplication()->getConfigurationType()==TApplication::CONFIG_TYPE_XML && ($cache=$this->getApplication()->getCache())!==null)
66                         {
67                                 $cacheKey='TParameterModule:'.$this->_paramFile;
68                                 if(($configFile=$cache->get($cacheKey))===false)
69                                 {
70                                         $configFile=new TXmlDocument;
71                                         $configFile->loadFromFile($this->_paramFile);
72                                         $cache->set($cacheKey,$configFile,0,new TFileCacheDependency($this->_paramFile));
73                                 }
74                         }
75                         else
76                         {
77                                 if($this->getApplication()->getConfigurationType()==TApplication::CONFIG_TYPE_PHP)
78                                 {
79                                         $configFile = include $this->_paramFile;
80                                 }
81                                 else
82                                 {
83                                         $configFile=new TXmlDocument;
84                                         $configFile->loadFromFile($this->_paramFile);
85                                 }
86                         }
87                         $this->loadParameters($configFile);
88                 }
89                 $this->_initialized=true;
90         }
91
92         /**
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
96          */
97         protected function loadParameters($config)
98         {
99                 $parameters=array();
100                 if(is_array($config))
101                 {
102                         foreach($config as $id => $parameter)
103                         {
104                                 if(is_array($parameter) && isset($parameter['class']))
105                                 {
106                                         $properties = isset($parameter['properties'])?$parameter['properties']:array();
107                                         $parameters[$id]=array($parameter['class'],$properties);
108                                 }
109                                 else
110                                 {
111                                         $parameters[$id] = $parameter;
112                                 }
113                         }
114                 }
115                 else if($config instanceof TXmlElement)
116                 {
117                         foreach($config->getElementsByTagName('parameter') as $node)
118                         {
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)
123                                 {
124                                         if(($value=$properties->remove('value'))===null)
125                                                 $parameters[$id]=$node;
126                                         else
127                                                 $parameters[$id]=$value;
128                                 }
129                                 else
130                                         $parameters[$id]=array($type,$properties->toArray());
131                         }
132                 }
133
134                 $appParams=$this->getApplication()->getParameters();
135                 foreach($parameters as $id=>$parameter)
136                 {
137                         if(is_array($parameter))
138                         {
139                                 $component=Prado::createComponent($parameter[0]);
140                                 foreach($parameter[1] as $name=>$value)
141                                         $component->setSubProperty($name,$value);
142                                 $appParams->add($id,$component);
143                         }
144                         else
145                                 $appParams->add($id,$parameter);
146                 }
147         }
148
149         /**
150          * @return string the parameter file path
151          */
152         public function getParameterFile()
153         {
154                 return $this->_paramFile;
155         }
156
157         /**
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
162          */
163         public function setParameterFile($value)
164         {
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);
169         }
170 }
171