Configuration.php 6.0 KB

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