Configuration.php 6.2 KB

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