Configuration.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. * The default values, an empty array by default.
  78. */
  79. private $data_default = array();
  80. /**
  81. * An object which help to set good values in configuration.
  82. */
  83. private $configuration_setter = null;
  84. public function removeExtension($ext_name) {
  85. self::$extensions_enabled = array_diff(
  86. self::$extensions_enabled,
  87. array($ext_name)
  88. );
  89. }
  90. public function addExtension($ext_name) {
  91. $found = array_search($ext_name, self::$extensions_enabled) !== false;
  92. if (!$found) {
  93. self::$extensions_enabled[] = $ext_name;
  94. }
  95. }
  96. /**
  97. * Create a new Minz_Configuration object.
  98. *
  99. * @param $namespace the name of the current configuration.
  100. * @param $config_filename the file containing configuration values.
  101. * @param $default_filename the file containing default values, null by default.
  102. * @param $configuration_setter an optional helper to set values in configuration
  103. */
  104. private function __construct($namespace, $config_filename, $default_filename = null,
  105. $configuration_setter = null) {
  106. $this->namespace = $namespace;
  107. $this->config_filename = $config_filename;
  108. try {
  109. $this->data = self::load($this->config_filename);
  110. } catch (Minz_FileNotExistException $e) {
  111. if (is_null($default_filename)) {
  112. throw $e;
  113. }
  114. }
  115. $this->default_filename = $default_filename;
  116. if (!is_null($this->default_filename)) {
  117. $this->data_default = self::load($this->default_filename);
  118. }
  119. $this->_configurationSetter($configuration_setter);
  120. }
  121. /**
  122. * Set a configuration setter for the current configuration.
  123. * @param $configuration_setter the setter to call when modifying data. It
  124. * must implement an handle($key, $value) method.
  125. */
  126. public function _configurationSetter($configuration_setter) {
  127. if (is_callable(array($configuration_setter, 'handle'))) {
  128. $this->configuration_setter = $configuration_setter;
  129. }
  130. }
  131. /**
  132. * Return the value of the given param.
  133. *
  134. * @param $key the name of the param.
  135. * @param $default default value to return if key does not exist.
  136. * @return the value corresponding to the key.
  137. * @throws Minz_ConfigurationParamException if the param does not exist
  138. */
  139. public function param($key, $default = null) {
  140. if (isset($this->data[$key])) {
  141. return $this->data[$key];
  142. } elseif (!is_null($default)) {
  143. return $default;
  144. } elseif (isset($this->data_default[$key])) {
  145. return $this->data_default[$key];
  146. } else {
  147. Minz_Log::warning($key . ' does not exist in configuration');
  148. return null;
  149. }
  150. }
  151. /**
  152. * A wrapper for param().
  153. */
  154. public function __get($key) {
  155. return $this->param($key);
  156. }
  157. /**
  158. * Set or remove a param.
  159. *
  160. * @param $key the param name to set.
  161. * @param $value the value to set. If null, the key is removed from the configuration.
  162. */
  163. public function _param($key, $value = null) {
  164. if (!is_null($this->configuration_setter) && $this->configuration_setter->support($key)) {
  165. $this->configuration_setter->handle($this->data, $key, $value);
  166. } elseif (isset($this->data[$key]) && is_null($value)) {
  167. unset($this->data[$key]);
  168. } elseif (!is_null($value)) {
  169. $this->data[$key] = $value;
  170. }
  171. }
  172. /**
  173. * A wrapper for _param().
  174. */
  175. public function __set($key, $value) {
  176. $this->_param($key, $value);
  177. }
  178. /**
  179. * Save the current configuration in the configuration file.
  180. */
  181. public function save() {
  182. $back_filename = $this->config_filename . '.bak.php';
  183. @rename($this->config_filename, $back_filename);
  184. if (file_put_contents($this->config_filename,
  185. "<?php\nreturn " . var_export($this->data, true) . ';',
  186. LOCK_EX) === false) {
  187. return false;
  188. }
  189. // Clear PHP 5.5+ cache for include
  190. if (function_exists('opcache_invalidate')) {
  191. opcache_invalidate($this->config_filename);
  192. }
  193. return true;
  194. }
  195. }