Configuration.php 6.2 KB

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