[ Index ] |
|
Code source de PHP PEAR 1.4.5 |
1 <?php 2 // +----------------------------------------------------------------------+ 3 // | PHP Version 4 | 4 // +----------------------------------------------------------------------+ 5 // | Copyright (c) 1997-2003 The PHP Group | 6 // +----------------------------------------------------------------------+ 7 // | This source file is subject to version 2.0 of the PHP license, | 8 // | that is bundled with this package in the file LICENSE, and is | 9 // | available at through the world-wide-web at | 10 // | http://www.php.net/license/2_02.txt. | 11 // | If you did not receive a copy of the PHP license and are unable to | 12 // | obtain it through the world-wide-web, please send a note to | 13 // | license@php.net so we can mail you a copy immediately. | 14 // +----------------------------------------------------------------------+ 15 // | Authors: Bertrand Mansion <bmansion@mamasam.com> | 16 // +----------------------------------------------------------------------+ 17 // 18 // $Id: PHPArray.php,v 1.29 2006/10/20 03:13:35 aashley Exp $ 19 20 /** 21 * Config parser for common PHP configuration array 22 * such as found in the horde project. 23 * 24 * Options expected is: 25 * 'name' => 'conf' 26 * Name of the configuration array. 27 * Default is $conf[]. 28 * 'useAttr' => true 29 * Whether we render attributes 30 * 31 * @author Bertrand Mansion <bmansion@mamasam.com> 32 * @package Config 33 */ 34 class Config_Container_PHPArray { 35 36 /** 37 * This class options: 38 * - name of the config array to parse/output 39 * Ex: $options['name'] = 'myconf'; 40 * - Whether to add attributes to the array 41 * Ex: $options['useAttr'] = false; 42 * 43 * @var array 44 */ 45 var $options = array('name' => 'conf', 46 'useAttr' => true); 47 48 /** 49 * Constructor 50 * 51 * @access public 52 * @param string $options Options to be used by renderer 53 */ 54 function Config_Container_PHPArray($options = array()) 55 { 56 foreach ($options as $key => $value) { 57 $this->options[$key] = $value; 58 } 59 } // end constructor 60 61 /** 62 * Parses the data of the given configuration file 63 * 64 * @access public 65 * @param string $datasrc path to the configuration file 66 * @param object $obj reference to a config object 67 * @return mixed returns a PEAR_ERROR, if error occurs or true if ok 68 */ 69 function &parseDatasrc($datasrc, &$obj) 70 { 71 $return = true; 72 if (empty($datasrc)) { 73 return PEAR::raiseError("Datasource file path is empty.", null, PEAR_ERROR_RETURN); 74 } 75 if (is_array($datasrc)) { 76 $this->_parseArray($datasrc, $obj->container); 77 } else { 78 if (!file_exists($datasrc)) { 79 return PEAR::raiseError("Datasource file does not exist.", null, PEAR_ERROR_RETURN); 80 } else { 81 include($datasrc); 82 if (!isset(${$this->options['name']}) || !is_array(${$this->options['name']})) { 83 return PEAR::raiseError("File '$datasrc' does not contain a required '".$this->options['name']."' array.", null, PEAR_ERROR_RETURN); 84 } 85 } 86 $this->_parseArray(${$this->options['name']}, $obj->container); 87 } 88 return $return; 89 } // end func parseDatasrc 90 91 /** 92 * Parses the PHP array recursively 93 * @param array $array array values from the config file 94 * @param object $container reference to the container object 95 * @access private 96 * @return void 97 */ 98 function _parseArray($array, &$container) 99 { 100 foreach ($array as $key => $value) { 101 switch ((string)$key) { 102 case '@': 103 $container->setAttributes($value); 104 break; 105 case '#': 106 $container->setType('directive'); 107 $container->setContent($value); 108 break; 109 default: 110 /* if (is_array($value)) { 111 $section =& $container->createSection($key); 112 $this->_parseArray($value, $section); 113 } else { 114 $container->createDirective($key, $value); 115 }*/ 116 117 if (is_array($value)) { 118 if (is_integer(key($value))) { 119 foreach ($value as $nestedValue) { 120 if (is_array($nestedValue)) { 121 $section =& $container->createSection($key); 122 $this->_parseArray($nestedValue, $section); 123 } else { 124 $container->createDirective($key, $nestedValue); 125 } 126 } 127 } else { 128 $section =& $container->createSection($key); 129 $this->_parseArray($value, $section); 130 } 131 } else { 132 $container->createDirective($key, $value); 133 } 134 135 } 136 } 137 } // end func _parseArray 138 139 /** 140 * Returns a formatted string of the object 141 * @param object $obj Container object to be output as string 142 * @access public 143 * @return string 144 */ 145 function toString(&$obj) 146 { 147 if (!isset($string)) { 148 $string = ''; 149 } 150 switch ($obj->type) { 151 case 'blank': 152 $string .= "\n"; 153 break; 154 case 'comment': 155 $string .= '// '.$obj->content."\n"; 156 break; 157 case 'directive': 158 $attrString = ''; 159 $parentString = $this->_getParentString($obj); 160 $attributes = $obj->getAttributes(); 161 if ($this->options['useAttr'] && is_array($attributes) && count($attributes) > 0) { 162 // Directive with attributes '@' and value '#' 163 $string .= $parentString."['#']"; 164 foreach ($attributes as $attr => $val) { 165 $attrString .= $parentString."['@']" 166 ."['".$attr."'] = '".addslashes($val)."';\n"; 167 } 168 } else { 169 $string .= $parentString; 170 } 171 $string .= ' = '; 172 if (is_string($obj->content)) { 173 $string .= "'".addslashes($obj->content)."'"; 174 } elseif (is_int($obj->content) || is_float($obj->content)) { 175 $string .= $obj->content; 176 } elseif (is_bool($obj->content)) { 177 $string .= ($obj->content) ? 'true' : 'false'; 178 } 179 $string .= ";\n"; 180 $string .= $attrString; 181 break; 182 case 'section': 183 $attrString = ''; 184 $attributes = $obj->getAttributes(); 185 if ($this->options['useAttr'] && is_array($attributes) && count($attributes) > 0) { 186 $parentString = $this->_getParentString($obj); 187 foreach ($attributes as $attr => $val) { 188 $attrString .= $parentString."['@']" 189 ."['".$attr."'] = '".addslashes($val)."';\n"; 190 } 191 } 192 $string .= $attrString; 193 if ($count = count($obj->children)) { 194 for ($i = 0; $i < $count; $i++) { 195 $string .= $this->toString($obj->getChild($i)); 196 } 197 } 198 break; 199 default: 200 $string = ''; 201 } 202 return $string; 203 } // end func toString 204 205 /** 206 * Returns a formatted string of the object parents 207 * @access private 208 * @return string 209 */ 210 function _getParentString(&$obj) 211 { 212 $string = ''; 213 if (!$obj->isRoot()) { 214 $string = is_int($obj->name) ? "[".$obj->name."]" : "['".$obj->name."']"; 215 $string = $this->_getParentString($obj->parent).$string; 216 $count = $obj->parent->countChildren(null, $obj->name); 217 if ($count > 1) { 218 $string .= '['.$obj->getItemPosition(false).']'; 219 } 220 } 221 else { 222 if (empty($this->options['name'])) { 223 $string .= '$'.$obj->name; 224 } else { 225 $string .= '$'.$this->options['name']; 226 } 227 } 228 return $string; 229 } // end func _getParentString 230 231 /** 232 * Writes the configuration to a file 233 * 234 * @param mixed datasrc info on datasource such as path to the configuraton file 235 * @param string configType (optional)type of configuration 236 * @access public 237 * @return string 238 */ 239 function writeDatasrc($datasrc, &$obj) 240 { 241 $fp = @fopen($datasrc, 'w'); 242 if ($fp) { 243 $string = "<?php\n". $this->toString($obj) ."?>"; // <? : Fix my syntax coloring 244 $len = strlen($string); 245 @flock($fp, LOCK_EX); 246 @fwrite($fp, $string, $len); 247 @flock($fp, LOCK_UN); 248 @fclose($fp); 249 return true; 250 } else { 251 return PEAR::raiseError('Cannot open datasource for writing.', 1, PEAR_ERROR_RETURN); 252 } 253 } // end func writeDatasrc 254 } // end class Config_Container_PHPArray 255 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sun Feb 25 14:08:00 2007 | par Balluche grâce à PHPXref 0.7 |