PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/vendors/PEAR/Config/Container/PHPArray.php

https://github.com/rogerwu99/punch_bantana
PHP | 258 lines | 164 code | 8 blank | 86 comment | 38 complexity | 0696ac025b2d741f2b0636c28b049512 MD5 | raw file
  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 306488 2010-12-20 08:45:09Z cweiske $
  19. /**
  20. * Config parser for common PHP configuration array
  21. * such as found in the horde project.
  22. *
  23. * Options expected is:
  24. * 'name' => 'conf'
  25. * Name of the configuration array.
  26. * Default is $conf[].
  27. * 'useAttr' => true
  28. * Whether we render attributes
  29. *
  30. * @author Bertrand Mansion <bmansion@mamasam.com>
  31. * @package Config
  32. */
  33. class Config_Container_PHPArray {
  34. /**
  35. * This class options:
  36. * - name of the config array to parse/output
  37. * Ex: $options['name'] = 'myconf';
  38. * - Whether to add attributes to the array
  39. * Ex: $options['useAttr'] = false;
  40. * - Whether to treat numbered arrays as duplicates of their parent directive
  41. * or as individual directives
  42. * Ex: $options['duplicateDirectives'] = false;
  43. *
  44. * @var array
  45. */
  46. var $options = array('name' => 'conf',
  47. 'useAttr' => true,
  48. 'duplicateDirectives' => true);
  49. /**
  50. * Constructor
  51. *
  52. * @access public
  53. * @param string $options Options to be used by renderer
  54. */
  55. function Config_Container_PHPArray($options = array())
  56. {
  57. foreach ($options as $key => $value) {
  58. $this->options[$key] = $value;
  59. }
  60. } // end constructor
  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. * Parses the PHP array recursively
  92. * @param array $array array values from the config file
  93. * @param object $container reference to the container object
  94. * @access private
  95. * @return void
  96. */
  97. function _parseArray($array, &$container)
  98. {
  99. foreach ($array as $key => $value) {
  100. switch ((string)$key) {
  101. case '@':
  102. $container->setAttributes($value);
  103. break;
  104. case '#':
  105. $container->setType('directive');
  106. $container->setContent($value);
  107. break;
  108. default:
  109. if (is_array($value)) {
  110. if ($this->options['duplicateDirectives'] == true
  111. //speed (first/one key is numeric)
  112. && is_integer(key($value))
  113. //accuracy (all keys are numeric)
  114. && 1 == count(array_unique(array_map('is_numeric', array_keys($value))))
  115. ) {
  116. foreach ($value as $nestedValue) {
  117. if (is_array($nestedValue)) {
  118. $section =& $container->createSection($key);
  119. $this->_parseArray($nestedValue, $section);
  120. } else {
  121. $container->createDirective($key, $nestedValue);
  122. }
  123. }
  124. } else {
  125. $section =& $container->createSection($key);
  126. $this->_parseArray($value, $section);
  127. }
  128. } else {
  129. $container->createDirective($key, $value);
  130. }
  131. }
  132. }
  133. } // end func _parseArray
  134. /**
  135. * Returns a formatted string of the object
  136. * @param object $obj Container object to be output as string
  137. * @access public
  138. * @return string
  139. */
  140. function toString(&$obj)
  141. {
  142. if (!isset($string)) {
  143. $string = '';
  144. }
  145. switch ($obj->type) {
  146. case 'blank':
  147. $string .= "\n";
  148. break;
  149. case 'comment':
  150. $string .= '// '.$obj->content."\n";
  151. break;
  152. case 'directive':
  153. $attrString = '';
  154. $parentString = $this->_getParentString($obj);
  155. $attributes = $obj->getAttributes();
  156. if ($this->options['useAttr'] && is_array($attributes) && count($attributes) > 0) {
  157. // Directive with attributes '@' and value '#'
  158. $string .= $parentString."['#']";
  159. foreach ($attributes as $attr => $val) {
  160. $attrString .= $parentString."['@']"
  161. ."['".$attr."'] = '".addcslashes($val, "\\'")."';\n";
  162. }
  163. } else {
  164. $string .= $parentString;
  165. }
  166. $string .= ' = ';
  167. if (is_string($obj->content)) {
  168. $string .= "'".addcslashes($obj->content, "\\'")."'";
  169. } elseif (is_int($obj->content) || is_float($obj->content)) {
  170. $string .= $obj->content;
  171. } elseif (is_bool($obj->content)) {
  172. $string .= ($obj->content) ? 'true' : 'false';
  173. } elseif ($obj->content === null) {
  174. $string .= 'null';
  175. }
  176. $string .= ";\n";
  177. $string .= $attrString;
  178. break;
  179. case 'section':
  180. $attrString = '';
  181. $attributes = $obj->getAttributes();
  182. if ($this->options['useAttr'] && is_array($attributes) && count($attributes) > 0) {
  183. $parentString = $this->_getParentString($obj);
  184. foreach ($attributes as $attr => $val) {
  185. $attrString .= $parentString."['@']"
  186. ."['".$attr."'] = '".addcslashes($val, "\\'")."';\n";
  187. }
  188. }
  189. $string .= $attrString;
  190. if ($count = count($obj->children)) {
  191. for ($i = 0; $i < $count; $i++) {
  192. $string .= $this->toString($obj->getChild($i));
  193. }
  194. }
  195. break;
  196. default:
  197. $string = '';
  198. }
  199. return $string;
  200. } // end func toString
  201. /**
  202. * Returns a formatted string of the object parents
  203. * @access private
  204. * @return string
  205. */
  206. function _getParentString(&$obj)
  207. {
  208. $string = '';
  209. if (!$obj->isRoot()) {
  210. $string = is_int($obj->name) ? "[".$obj->name."]" : "['".$obj->name."']";
  211. $string = $this->_getParentString($obj->parent).$string;
  212. $count = $obj->parent->countChildren(null, $obj->name);
  213. if ($count > 1) {
  214. $string .= '['.$obj->getItemPosition(false).']';
  215. }
  216. }
  217. else {
  218. if (empty($this->options['name'])) {
  219. $string .= '$'.$obj->name;
  220. } else {
  221. $string .= '$'.$this->options['name'];
  222. }
  223. }
  224. return $string;
  225. } // end func _getParentString
  226. /**
  227. * Writes the configuration to a file
  228. *
  229. * @param mixed datasrc info on datasource such as path to the configuraton file
  230. * @param string configType (optional)type of configuration
  231. * @access public
  232. * @return string
  233. */
  234. function writeDatasrc($datasrc, &$obj)
  235. {
  236. $fp = @fopen($datasrc, 'w');
  237. if ($fp) {
  238. $string = "<?php\n". $this->toString($obj) ."?>"; // <? : Fix my syntax coloring
  239. $len = strlen($string);
  240. @flock($fp, LOCK_EX);
  241. @fwrite($fp, $string, $len);
  242. @flock($fp, LOCK_UN);
  243. @fclose($fp);
  244. return true;
  245. } else {
  246. return PEAR::raiseError('Cannot open datasource for writing.', 1, PEAR_ERROR_RETURN);
  247. }
  248. } // end func writeDatasrc
  249. } // end class Config_Container_PHPArray
  250. ?>