Configuration.php 5.9 KB

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