4
0

Configuration.php 5.8 KB

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