Configuration.php 6.4 KB

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