PageRenderTime 22ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://github.com/symphonycms/symphony-2
PHP | 209 lines | 82 code | 21 blank | 106 comment | 16 complexity | ce7f40fc6d5f0dbef661c4f4afff6d17 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 constructor for the Configuration class takes one parameter,
  28. * `$forceLowerCase` which will make all property and
  29. * group names lowercase or not. By default they are left to the case
  30. * the user provides
  31. *
  32. * @param boolean $forceLowerCase
  33. * False by default, if true this will make all property and group names
  34. * lowercase
  35. */
  36. public function __construct($forceLowerCase = false)
  37. {
  38. $this->_forceLowerCase = $forceLowerCase;
  39. }
  40. /**
  41. * Setter for the `$this->_properties`. The properties array
  42. * can be grouped to be an 'array' of an 'array' of properties. For instance
  43. * a 'region' key may be an array of 'properties' (that is name/value), or it
  44. * may be a 'value' itself.
  45. *
  46. * @param string $name
  47. * The name of the property to set, eg 'timezone'
  48. * @param array|string|integer|float|boolean $value
  49. * The value for the property to set, eg. '+10:00'
  50. * @param string $group
  51. * The group for this property, eg. 'region'
  52. */
  53. public function set($name, $value, $group = null)
  54. {
  55. if ($this->_forceLowerCase) {
  56. $name = strtolower($name);
  57. $group = strtolower($group);
  58. }
  59. if ($group) {
  60. $this->_properties[$group][$name] = $value;
  61. } else {
  62. $this->_properties[$name] = $value;
  63. }
  64. }
  65. /**
  66. * A quick way to set a large number of properties. Given an associative
  67. * array or a nested associative array (where the key will be the group),
  68. * this function will merge the `$array` with the existing configuration.
  69. * By default the given `$array` will overwrite any existing keys unless
  70. * the `$overwrite` parameter is passed as false.
  71. *
  72. * @since Symphony 2.3.2 The `$overwrite` parameter is available
  73. * @param array $array
  74. * An associative array of properties, 'property' => 'value' or
  75. * 'group' => array('property' => 'value')
  76. * @param boolean $overwrite
  77. * An optional boolean parameter to indicate if it is safe to use array_merge
  78. * or if the provided array should be integrated using the 'set()' method
  79. * to avoid possible change collision. Defaults to false.
  80. */
  81. public function setArray(array $array, $overwrite = false)
  82. {
  83. if ($overwrite) {
  84. $this->_properties = array_merge($this->_properties, $array);
  85. } else {
  86. foreach ($array as $set => $values) {
  87. foreach ($values as $key => $val) {
  88. self::set($key, $val, $set);
  89. }
  90. }
  91. }
  92. }
  93. /**
  94. * Accessor function for the `$this->_properties`.
  95. *
  96. * @param string $name
  97. * The name of the property to retrieve
  98. * @param string $group
  99. * The group that this property will be in
  100. * @return array|string|integer|float|boolean
  101. * If `$name` or `$group` are not
  102. * provided this function will return the full `$this->_properties`
  103. * array.
  104. */
  105. public function get($name = null, $group = null)
  106. {
  107. // Return the whole array if no name or index is requested
  108. if (!$name && !$group) {
  109. return $this->_properties;
  110. }
  111. if ($this->_forceLowerCase) {
  112. $name = strtolower($name);
  113. $group = strtolower($group);
  114. }
  115. if ($group) {
  116. return (isset($this->_properties[$group][$name]) ? $this->_properties[$group][$name] : null);
  117. }
  118. return (isset($this->_properties[$name]) ? $this->_properties[$name] : null);
  119. }
  120. /**
  121. * The remove function will unset a property by `$name`.
  122. * It is possible to remove an entire 'group' by passing the group
  123. * name as the `$name`
  124. *
  125. * @param string $name
  126. * The name of the property to unset. This can also be the group name
  127. * @param string $group
  128. * The group of the property to unset
  129. */
  130. public function remove($name, $group = null)
  131. {
  132. if ($this->_forceLowerCase) {
  133. $name = strtolower($name);
  134. $group = strtolower($group);
  135. }
  136. if ($group && isset($this->_properties[$group][$name])) {
  137. unset($this->_properties[$group][$name]);
  138. } elseif ($this->_properties[$name]) {
  139. unset($this->_properties[$name]);
  140. }
  141. }
  142. /**
  143. * Empties all the Configuration values by setting `$this->_properties`
  144. * to an empty array
  145. */
  146. public function flush()
  147. {
  148. $this->_properties = array();
  149. }
  150. /**
  151. * This magic `__toString` function converts the internal `$this->_properties`
  152. * array into a string representation. Symphony generates the `MANIFEST/config.php`
  153. * file in this manner.
  154. * @see ArraySerializer::asPHPFile()
  155. * @return string
  156. * A string that contains a array representation of `$this->_properties`.
  157. * This is used by Symphony to write the `config.php` file.
  158. */
  159. public function __toString()
  160. {
  161. return (new ArraySerializer($this->_properties))->asPHPFile();
  162. }
  163. /**
  164. * Function will write the current Configuration object to
  165. * a specified `$file` with the given `$permissions`.
  166. *
  167. * @param string $file
  168. * the path of the file to write.
  169. * @param integer|null $permissions (optional)
  170. * the permissions as an octal number to set set on the resulting file.
  171. * If this is not provided it will use the permissions defined in [`write_mode`][`file`]
  172. * @return boolean
  173. */
  174. public function write($file = null, $permissions = null)
  175. {
  176. if (is_null($permissions) && isset($this->_properties['file']['write_mode'])) {
  177. $permissions = $this->_properties['file']['write_mode'];
  178. }
  179. if (is_null($file)) {
  180. $file = CONFIG;
  181. }
  182. $string = (string)$this;
  183. if (function_exists('opcache_invalidate')) {
  184. opcache_invalidate($file, true);
  185. }
  186. return General::writeFile($file, $string, $permissions);
  187. }
  188. }