/lib/pear/Config/Container/PHPArray.php

https://bitbucket.org/valmy/openx · PHP · 236 lines · 147 code · 8 blank · 81 comment · 31 complexity · 36d8976d2f7542e689f865d462c5cf64 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$
  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. *
  41. * @var array
  42. */
  43. var $options = array('name' => 'conf',
  44. 'useAttr' => true);
  45. /**
  46. * Constructor
  47. *
  48. * @access public
  49. * @param string $options Options to be used by renderer
  50. */
  51. function Config_Container_PHPArray($options = array())
  52. {
  53. foreach ($options as $key => $value) {
  54. $this->options[$key] = $value;
  55. }
  56. } // end constructor
  57. /**
  58. * Parses the data of the given configuration file
  59. *
  60. * @access public
  61. * @param string $datasrc path to the configuration file
  62. * @param object $obj reference to a config object
  63. * @return mixed returns a PEAR_ERROR, if error occurs or true if ok
  64. */
  65. function &parseDatasrc($datasrc, &$obj)
  66. {
  67. if (empty($datasrc)) {
  68. return PEAR::raiseError("Datasource file path is empty.", null, PEAR_ERROR_RETURN);
  69. }
  70. if (is_array($datasrc)) {
  71. $this->_parseArray($datasrc, $obj->container);
  72. } else {
  73. if (!file_exists($datasrc)) {
  74. return PEAR::raiseError("Datasource file does not exist.", null, PEAR_ERROR_RETURN);
  75. } else {
  76. include($datasrc);
  77. if (!isset(${$this->options['name']}) || !is_array(${$this->options['name']})) {
  78. return PEAR::raiseError("File '$datasrc' does not contain a required '".$this->options['name']."' array.", null, PEAR_ERROR_RETURN);
  79. }
  80. }
  81. $this->_parseArray(${$this->options['name']}, $obj->container);
  82. }
  83. return true;
  84. } // end func parseDatasrc
  85. /**
  86. * Parses the PHP array recursively
  87. * @param array $array array values from the config file
  88. * @param object $container reference to the container object
  89. * @access private
  90. * @return void
  91. */
  92. function _parseArray($array, &$container)
  93. {
  94. foreach ($array as $key => $value) {
  95. switch ((string)$key) {
  96. case '@':
  97. $container->setAttributes($value);
  98. break;
  99. case '#':
  100. $container->setType('directive');
  101. $container->setContent($value);
  102. break;
  103. default:
  104. if (is_array($value)) {
  105. $section =& $container->createSection($key);
  106. $this->_parseArray($value, $section);
  107. } else {
  108. $container->createDirective($key, $value);
  109. }
  110. }
  111. }
  112. } // end func _parseArray
  113. /**
  114. * Returns a formatted string of the object
  115. * @param object $obj Container object to be output as string
  116. * @access public
  117. * @return string
  118. */
  119. function toString(&$obj)
  120. {
  121. if (!isset($string)) {
  122. $string = '';
  123. }
  124. switch ($obj->type) {
  125. case 'blank':
  126. $string .= "\n";
  127. break;
  128. case 'comment':
  129. $string .= '// '.$obj->content."\n";
  130. break;
  131. case 'directive':
  132. $attrString = '';
  133. $parentString = $this->_getParentString($obj);
  134. $attributes = $obj->getAttributes();
  135. if ($this->options['useAttr'] && is_array($attributes) && count($attributes) > 0) {
  136. // Directive with attributes '@' and value '#'
  137. $string .= $parentString."['#']";
  138. foreach ($attributes as $attr => $val) {
  139. $attrString .= $parentString."['@']"
  140. ."['".$attr."'] = '".addslashes($val)."';\n";
  141. }
  142. } else {
  143. $string .= $parentString;
  144. }
  145. $string .= ' = ';
  146. if (is_string($obj->content)) {
  147. $string .= "'".addslashes($obj->content)."'";
  148. } elseif (is_int($obj->content) || is_float($obj->content)) {
  149. $string .= $obj->content;
  150. } elseif (is_bool($obj->content)) {
  151. $string .= ($obj->content) ? 'true' : 'false';
  152. }
  153. $string .= ";\n";
  154. $string .= $attrString;
  155. break;
  156. case 'section':
  157. $attrString = '';
  158. $attributes = $obj->getAttributes();
  159. if ($this->options['useAttr'] && is_array($attributes) && count($attributes) > 0) {
  160. $parentString = $this->_getParentString($obj);
  161. foreach ($attributes as $attr => $val) {
  162. $attrString .= $parentString."['@']"
  163. ."['".$attr."'] = '".addslashes($val)."';\n";
  164. }
  165. }
  166. $string .= $attrString;
  167. if ($count = count($obj->children)) {
  168. for ($i = 0; $i < $count; $i++) {
  169. $string .= $this->toString($obj->getChild($i));
  170. }
  171. }
  172. break;
  173. default:
  174. $string = '';
  175. }
  176. return $string;
  177. } // end func toString
  178. /**
  179. * Returns a formatted string of the object parents
  180. * @access private
  181. * @return string
  182. */
  183. function _getParentString(&$obj)
  184. {
  185. $string = '';
  186. if (!$obj->isRoot()) {
  187. if (!$obj->parent->isRoot()) {
  188. $string = is_int($obj->name) ? "[".$obj->name."]" : "['".$obj->name."']";
  189. } else {
  190. if (empty($this->options['name'])) {
  191. $string .= '$'.$obj->name;
  192. } else {
  193. $string .= '$'.$this->options['name']."['".$obj->name."']";
  194. }
  195. }
  196. $string = $this->_getParentString($obj->parent).$string;
  197. $count = $obj->parent->countChildren(null, $obj->name);
  198. if ($count > 1) {
  199. $string .= '['.$obj->getItemPosition().']';
  200. }
  201. }
  202. return $string;
  203. } // end func _getParentString
  204. /**
  205. * Writes the configuration to a file
  206. *
  207. * @param mixed datasrc info on datasource such as path to the configuraton file
  208. * @param string configType (optional)type of configuration
  209. * @access public
  210. * @return string
  211. */
  212. function writeDatasrc($datasrc, &$obj)
  213. {
  214. $fp = @fopen($datasrc, 'w');
  215. if ($fp) {
  216. $string = "<?php\n". $this->toString($obj) ."?>"; // <? : Fix my syntax coloring
  217. $len = strlen($string);
  218. @flock($fp, LOCK_EX);
  219. @fwrite($fp, $string, $len);
  220. @flock($fp, LOCK_UN);
  221. @fclose($fp);
  222. return true;
  223. } else {
  224. return PEAR::raiseError('Cannot open datasource for writing.', 1, PEAR_ERROR_RETURN);
  225. }
  226. } // end func writeDatasrc
  227. } // end class Config_Container_PHPArray
  228. ?>