Configuration.php 5.8 KB

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