PageRenderTime 60ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/symphony/lib/core/class.configuration.php

https://github.com/bauhouse/sym-designprojectx
PHP | 275 lines | 121 code | 28 blank | 126 comment | 21 complexity | bd315a2c2b674e6de3c7504e4d2e52e6 MD5 | raw file
  1. <?php
  2. /**
  3. * @package core
  4. */
  5. /**
  6. * The Configuration class acts as a property => value store for settings
  7. * used throughout Symphony. The result of this class is a string containing
  8. * a PHP representation of the properties (and their values) set by the Configuration.
  9. * Symphony's configuration file is saved at `CONFIG`. The initial
  10. * file is generated by the Symphony installer, and then subsequent use of Symphony
  11. * loads in this file for each page view. Like minded properties can be grouped.
  12. */
  13. class Configuration
  14. {
  15. /**
  16. * An associative array of the properties for this Configuration object
  17. * @var array
  18. */
  19. private $_properties = array();
  20. /**
  21. * Whether all properties and group keys will be forced to be lowercase.
  22. * By default this is false, which makes all properties case sensitive
  23. * @var boolean
  24. */
  25. private $_forceLowerCase = false;
  26. /**
  27. * The string representing the tab characters used to serialize the configuration
  28. * @var string
  29. */
  30. const TAB = ' ';
  31. /**
  32. * The constructor for the Configuration class takes one parameter,
  33. * `$forceLowerCase` which will make all property and
  34. * group names lowercase or not. By default they are left to the case
  35. * the user provides
  36. *
  37. * @param boolean $forceLowerCase
  38. * False by default, if true this will make all property and group names
  39. * lowercase
  40. */
  41. public function __construct($forceLowerCase = false)
  42. {
  43. $this->_forceLowerCase = $forceLowerCase;
  44. }
  45. /**
  46. * Setter for the `$this->_properties`. The properties array
  47. * can be grouped to be an 'array' of an 'array' of properties. For instance
  48. * a 'region' key may be an array of 'properties' (that is name/value), or it
  49. * may be a 'value' itself.
  50. *
  51. * @param string $name
  52. * The name of the property to set, eg 'timezone'
  53. * @param array|string|integer|float|boolean $value
  54. * The value for the property to set, eg. '+10:00'
  55. * @param string $group
  56. * The group for this property, eg. 'region'
  57. */
  58. public function set($name, $value, $group = null)
  59. {
  60. if ($this->_forceLowerCase) {
  61. $name = strtolower($name);
  62. $group = strtolower($group);
  63. }
  64. if ($group) {
  65. $this->_properties[$group][$name] = $value;
  66. } else {
  67. $this->_properties[$name] = $value;
  68. }
  69. }
  70. /**
  71. * A quick way to set a large number of properties. Given an associative
  72. * array or a nested associative array (where the key will be the group),
  73. * this function will merge the `$array` with the existing configuration.
  74. * By default the given `$array` will overwrite any existing keys unless
  75. * the `$overwrite` parameter is passed as false.
  76. *
  77. * @since Symphony 2.3.2 The `$overwrite` parameter is available
  78. * @param array $array
  79. * An associative array of properties, 'property' => 'value' or
  80. * 'group' => array('property' => 'value')
  81. * @param boolean $overwrite
  82. * An optional boolean parameter to indicate if it is safe to use array_merge
  83. * or if the provided array should be integrated using the 'set()' method
  84. * to avoid possible change collision. Defaults to false.
  85. */
  86. public function setArray(array $array, $overwrite = false)
  87. {
  88. if ($overwrite) {
  89. $this->_properties = array_merge($this->_properties, $array);
  90. } else {
  91. foreach ($array as $set => $values) {
  92. foreach ($values as $key => $val) {
  93. self::set($key, $val, $set);
  94. }
  95. }
  96. }
  97. }
  98. /**
  99. * Accessor function for the `$this->_properties`.
  100. *
  101. * @param string $name
  102. * The name of the property to retrieve
  103. * @param string $group
  104. * The group that this property will be in
  105. * @return array|string|integer|float|boolean
  106. * If `$name` or `$group` are not
  107. * provided this function will return the full `$this->_properties`
  108. * array.
  109. */
  110. public function get($name = null, $group = null)
  111. {
  112. // Return the whole array if no name or index is requested
  113. if (!$name && !$group) {
  114. return $this->_properties;
  115. }
  116. if ($this->_forceLowerCase) {
  117. $name = strtolower($name);
  118. $group = strtolower($group);
  119. }
  120. if ($group) {
  121. return (isset($this->_properties[$group][$name]) ? $this->_properties[$group][$name] : null);
  122. }
  123. return (isset($this->_properties[$name]) ? $this->_properties[$name] : null);
  124. }
  125. /**
  126. * The remove function will unset a property by `$name`.
  127. * It is possible to remove an entire 'group' by passing the group
  128. * name as the `$name`
  129. *
  130. * @param string $name
  131. * The name of the property to unset. This can also be the group name
  132. * @param string $group
  133. * The group of the property to unset
  134. */
  135. public function remove($name, $group = null)
  136. {
  137. if ($this->_forceLowerCase) {
  138. $name = strtolower($name);
  139. $group = strtolower($group);
  140. }
  141. if ($group && isset($this->_properties[$group][$name])) {
  142. unset($this->_properties[$group][$name]);
  143. } elseif ($this->_properties[$name]) {
  144. unset($this->_properties[$name]);
  145. }
  146. }
  147. /**
  148. * Empties all the Configuration values by setting `$this->_properties`
  149. * to an empty array
  150. */
  151. public function flush()
  152. {
  153. $this->_properties = array();
  154. }
  155. /**
  156. * This magic `__toString` function converts the internal `$this->_properties`
  157. * array into a string representation. Symphony generates the `MANIFEST/config.php`
  158. * file in this manner.
  159. * @see Configuration::serializeArray()
  160. * @return string
  161. * A string that contains a array representation of `$this->_properties`.
  162. * This is used by Symphony to write the `config.php` file.
  163. */
  164. public function __toString()
  165. {
  166. $string = 'array(';
  167. $tab = static::TAB;
  168. foreach ($this->_properties as $group => $data) {
  169. $string .= str_repeat(PHP_EOL, 3) . "$tab$tab###### ".strtoupper($group)." ######";
  170. $group = addslashes($group);
  171. $string .= PHP_EOL . "$tab$tab'$group' => ";
  172. $string .= $this->serializeArray($data, 3, $tab);
  173. $string .= ",";
  174. $string .= PHP_EOL . "$tab$tab########";
  175. }
  176. $string .= PHP_EOL . "$tab)";
  177. return $string;
  178. }
  179. /**
  180. * The `serializeArray` function will properly format and indent multidimensional
  181. * arrays using recursivity.
  182. *
  183. * `__toString()` call `serializeArray` to use the recursive condition.
  184. * The keys (int) in array won't have apostrophe.
  185. * Array without multidimensional array will be output with normal indentation.
  186. * @return string
  187. * A string that contains a array representation of the '$data parameter'.
  188. * @param array $arr
  189. * A array of properties to serialize.
  190. * @param integer $indentation
  191. * The current level of indentation.
  192. * @param string $tab
  193. * A horizontal tab
  194. */
  195. protected function serializeArray(array $arr, $indentation = 0, $tab = self::TAB)
  196. {
  197. $tabs = '';
  198. $closeTabs = '';
  199. for ($i = 0; $i < $indentation; $i++) {
  200. $tabs .= $tab;
  201. if ($i < $indentation - 1) {
  202. $closeTabs .= $tab;
  203. }
  204. }
  205. $string = 'array(';
  206. foreach ($arr as $key => $value) {
  207. $key = addslashes($key);
  208. $string .= (is_numeric($key) ? PHP_EOL . "$tabs $key => " : PHP_EOL . "$tabs'$key' => ");
  209. if (is_array($value)) {
  210. if (empty($value)) {
  211. $string .= 'array()';
  212. } else {
  213. $string .= $this->serializeArray($value, $indentation + 1, $tab);
  214. }
  215. } else {
  216. $string .= (General::strlen($value) > 0 ? var_export($value, true) : 'null');
  217. }
  218. $string .= ",";
  219. }
  220. $string .= PHP_EOL . "$closeTabs)";
  221. return $string;
  222. }
  223. /**
  224. * Function will write the current Configuration object to
  225. * a specified `$file` with the given `$permissions`.
  226. *
  227. * @param string $file
  228. * the path of the file to write.
  229. * @param integer|null $permissions (optional)
  230. * the permissions as an octal number to set set on the resulting file.
  231. * If this is not provided it will use the permissions defined in [`write_mode`][`file`]
  232. * @return boolean
  233. */
  234. public function write($file = null, $permissions = null)
  235. {
  236. if (is_null($permissions) && isset($this->_properties['file']['write_mode'])) {
  237. $permissions = $this->_properties['file']['write_mode'];
  238. }
  239. if (is_null($file)) {
  240. $file = CONFIG;
  241. }
  242. $tab = static::TAB;
  243. $eol = PHP_EOL;
  244. $string = "<?php$eol$tab\$settings = " . (string)$this . ";$eol";
  245. return General::writeFile($file, $string, $permissions);
  246. }
  247. }