Configuration.php 5.9 KB

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