[ Index ] |
|
Code source de PRADO 3.0.6 |
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 PradoSoft 8 * @license http://www.pradosoft.com/license/ 9 * @version $Id: TParameterModule.php 1397 2006-09-07 07:55:53Z wei $ 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 1397 2006-09-07 07:55:53Z wei $ 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 $dom=new TXmlDocument; 63 $dom->loadFromFile($this->_paramFile); 64 $this->loadParameters($dom); 65 } 66 $this->_initialized=true; 67 } 68 69 /** 70 * Loads parameters into application. 71 * @param TXmlElement XML representation of the parameters 72 * @throws TConfigurationException if the parameter file format is invalid 73 */ 74 protected function loadParameters($xmlNode) 75 { 76 $parameters=array(); 77 foreach($xmlNode->getElementsByTagName('parameter') as $node) 78 { 79 $properties=$node->getAttributes(); 80 if(($id=$properties->remove('id'))===null) 81 throw new TConfigurationException('parametermodule_parameterid_required'); 82 if(($type=$properties->remove('class'))===null) 83 { 84 if(($value=$properties->remove('value'))===null) 85 $parameters[$id]=$node; 86 else 87 $parameters[$id]=$value; 88 } 89 else 90 $parameters[$id]=array($type,$properties->toArray()); 91 } 92 93 $appParams=$this->getApplication()->getParameters(); 94 foreach($parameters as $id=>$parameter) 95 { 96 if(is_array($parameter)) 97 { 98 $component=Prado::createComponent($parameter[0]); 99 foreach($parameter[1] as $name=>$value) 100 $component->setSubProperty($name,$value); 101 $appParams->add($id,$component); 102 } 103 else 104 $appParams->add($id,$parameter); 105 } 106 } 107 108 /** 109 * @return string the parameter file path 110 */ 111 public function getParameterFile() 112 { 113 return $this->_paramFile; 114 } 115 116 /** 117 * @param string the parameter file path. It must be in namespace format 118 * and the file extension is '.xml'. 119 * @throws TInvalidOperationException if the module is initialized 120 * @throws TConfigurationException if the file is invalid 121 */ 122 public function setParameterFile($value) 123 { 124 if($this->_initialized) 125 throw new TInvalidOperationException('parametermodule_parameterfile_unchangeable'); 126 else if(($this->_paramFile=Prado::getPathOfNamespace($value,self::PARAM_FILE_EXT))===null || !is_file($this->_paramFile)) 127 throw new TConfigurationException('parametermodule_parameterfile_invalid',$value); 128 } 129 } 130 131 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sun Feb 25 21:07:04 2007 | par Balluche grâce à PHPXref 0.7 |