Configuration.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <?php
  2. /**
  3. * Manage configuration for the application.
  4. */
  5. class Minz_Configuration {
  6. /**
  7. * The list of configurations.
  8. */
  9. private static $config_list = array();
  10. /**
  11. * Add a new configuration to the list of configuration.
  12. *
  13. * @param $namespace the name of the current configuration
  14. * @param $config_filename the filename of the configuration
  15. * @param $default_filename a filename containing default values for the configuration
  16. * @param $configuration_setter an optional helper to set values in configuration
  17. */
  18. public static function register($namespace, $config_filename, $default_filename = null,
  19. $configuration_setter = null) {
  20. self::$config_list[$namespace] = new Minz_Configuration(
  21. $namespace, $config_filename, $default_filename, $configuration_setter
  22. );
  23. }
  24. /**
  25. * Parse a file and return its data.
  26. *
  27. * If the file does not contain a valid PHP code returning an array, an
  28. * empty array is returned anyway.
  29. *
  30. * @param $filename the name of the file to parse.
  31. * @return an array of values
  32. * @throws Minz_FileNotExistException if the file does not exist.
  33. */
  34. public static function load($filename) {
  35. if (!file_exists($filename)) {
  36. throw new Minz_FileNotExistException($filename);
  37. }
  38. $data = include($filename);
  39. if (is_array($data)) {
  40. return $data;
  41. } else {
  42. return array();
  43. }
  44. }
  45. /**
  46. * Return the configuration related to a given namespace.
  47. *
  48. * @param $namespace the name of the configuration to get.
  49. * @return a Minz_Configuration object
  50. * @throws Minz_ConfigurationNamespaceException if the namespace does not exist.
  51. */
  52. public static function get($namespace) {
  53. if (!isset(self::$config_list[$namespace])) {
  54. throw new Minz_ConfigurationNamespaceException(
  55. $namespace . ' namespace does not exist'
  56. );
  57. }
  58. return self::$config_list[$namespace];
  59. }
  60. /**
  61. * The namespace of the current configuration.
  62. */
  63. private $namespace = '';
  64. /**
  65. * The filename for the current configuration.
  66. */
  67. private $config_filename = '';
  68. /**
  69. * The filename for the current default values, null by default.
  70. */
  71. private $default_filename = null;
  72. /**
  73. * The configuration values, an empty array by default.
  74. */
  75. private $data = array();
  76. /**
  77. * An object which help to set good values in configuration.
  78. */
  79. private $configuration_setter = null;
  80. public function removeExtension($ext_name) {
  81. self::$extensions_enabled = array_diff(
  82. self::$extensions_enabled,
  83. array($ext_name)
  84. );
  85. }
  86. public function addExtension($ext_name) {
  87. $found = array_search($ext_name, self::$extensions_enabled) !== false;
  88. if (!$found) {
  89. self::$extensions_enabled[] = $ext_name;
  90. }
  91. }
  92. /**
  93. * Create a new Minz_Configuration object.
  94. *
  95. * @param $namespace the name of the current configuration.
  96. * @param $config_filename the file containing configuration values.
  97. * @param $default_filename the file containing default values, null by default.
  98. * @param $configuration_setter an optional helper to set values in configuration
  99. */
  100. private function __construct($namespace, $config_filename, $default_filename = null,
  101. $configuration_setter = null) {
  102. $this->namespace = $namespace;
  103. $this->config_filename = $config_filename;
  104. $this->default_filename = $default_filename;
  105. $this->_configurationSetter($configuration_setter);
  106. if (!is_null($this->default_filename)) {
  107. $this->data = self::load($this->default_filename);
  108. }
  109. try {
  110. $this->data = array_replace_recursive(
  111. $this->data, self::load($this->config_filename)
  112. );
  113. } catch (Minz_FileNotExistException $e) {
  114. if (is_null($this->default_filename)) {
  115. throw $e;
  116. }
  117. }
  118. }
  119. /**
  120. * Set a configuration setter for the current configuration.
  121. * @param $configuration_setter the setter to call when modifying data. It
  122. * must implement an handle($key, $value) method.
  123. */
  124. public function _configurationSetter($configuration_setter) {
  125. if (is_callable(array($configuration_setter, 'handle'))) {
  126. $this->configuration_setter = $configuration_setter;
  127. }
  128. }
  129. /**
  130. * Return the value of the given param.
  131. *
  132. * @param $key the name of the param.
  133. * @param $default default value to return if key does not exist.
  134. * @return the value corresponding to the key.
  135. * @throws Minz_ConfigurationParamException if the param does not exist
  136. */
  137. public function param($key, $default = null) {
  138. if (isset($this->data[$key])) {
  139. return $this->data[$key];
  140. } elseif (!is_null($default)) {
  141. return $default;
  142. } else {
  143. Minz_Log::warning($key . ' does not exist in configuration');
  144. return null;
  145. }
  146. }
  147. /**
  148. * A wrapper for param().
  149. */
  150. public function __get($key) {
  151. return $this->param($key);
  152. }
  153. /**
  154. * Set or remove a param.
  155. *
  156. * @param $key the param name to set.
  157. * @param $value the value to set. If null, the key is removed from the configuration.
  158. */
  159. public function _param($key, $value = null) {
  160. if (!is_null($this->configuration_setter) && $this->configuration_setter->support($key)) {
  161. $this->configuration_setter->handle($this->data, $key, $value);
  162. } elseif (isset($this->data[$key]) && is_null($value)) {
  163. unset($this->data[$key]);
  164. } elseif (!is_null($value)) {
  165. $this->data[$key] = $value;
  166. }
  167. }
  168. /**
  169. * A wrapper for _param().
  170. */
  171. public function __set($key, $value) {
  172. $this->_param($key, $value);
  173. }
  174. /**
  175. * Save the current configuration in the configuration file.
  176. */
  177. public function save() {
  178. $back_filename = $this->config_filename . '.bak.php';
  179. @rename($this->config_filename, $back_filename);
  180. if (file_put_contents($this->config_filename,
  181. "<?php\nreturn " . var_export($this->data, true) . ';',
  182. LOCK_EX) === false) {
  183. return false;
  184. }
  185. // Clear PHP 5.5+ cache for include
  186. if (function_exists('opcache_invalidate')) {
  187. opcache_invalidate($this->config_filename);
  188. }
  189. return true;
  190. }
  191. }