4
0

Configuration.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * Manage configuration for the application.
  5. * @property string $base_url
  6. * @property array{type:'mysql'|'pgsql'|'sqlite',host:string,user:string,password:string,base:string,prefix:string,
  7. * connection_uri_params:string,pdo_options:array<int,int|string|bool>} $db
  8. * @property bool $disable_update
  9. * @property string $environment
  10. * @property array<string,bool> $extensions_enabled
  11. * @property ''|'error'|'warning'|'notice'|'info'|'debug' $log_level
  12. * @property-read string $mailer
  13. * @property-read array{'hostname':string,'host':string,'auth':bool,'username':string,'password':string,
  14. * 'secure':string,'auto_tls':bool,'port':int,'from':string} $smtp
  15. * @property string $title
  16. */
  17. class Minz_Configuration {
  18. /**
  19. * The list of configurations.
  20. * @var array<string,static>
  21. */
  22. private static array $config_list = [];
  23. /**
  24. * Add a new configuration to the list of configuration.
  25. *
  26. * @param string $namespace the name of the current configuration
  27. * @param string $config_filename the filename of the configuration
  28. * @param string $default_filename a filename containing default values for the configuration
  29. * @param Minz_ConfigurationSetterInterface $configuration_setter an optional helper to set values in configuration @deprecated
  30. * @throws Minz_FileNotExistException
  31. */
  32. public static function register(string $namespace, string $config_filename, ?string $default_filename = null,
  33. ?Minz_ConfigurationSetterInterface $configuration_setter = null): void {
  34. self::$config_list[$namespace] = new static(
  35. $namespace, $config_filename, $default_filename, $configuration_setter
  36. );
  37. }
  38. /**
  39. * Parse a file and return its data.
  40. *
  41. * @param string $filename the name of the file to parse.
  42. * @return array<string,mixed> of values
  43. * @throws Minz_FileNotExistException if the file does not exist or is invalid.
  44. */
  45. public static function load(string $filename): array {
  46. $data = @include $filename;
  47. if (is_array($data) && is_array_keys_string($data)) {
  48. return $data;
  49. } else {
  50. throw new Minz_FileNotExistException($filename);
  51. }
  52. }
  53. /**
  54. * Return the configuration related to a given namespace.
  55. *
  56. * @param string $namespace the name of the configuration to get.
  57. * @throws Minz_ConfigurationNamespaceException if the namespace does not exist.
  58. */
  59. public static function get(string $namespace): static {
  60. if (!isset(self::$config_list[$namespace])) {
  61. throw new Minz_ConfigurationNamespaceException(
  62. $namespace . ' namespace does not exist'
  63. );
  64. }
  65. return self::$config_list[$namespace];
  66. }
  67. /**
  68. * The namespace of the current configuration.
  69. * Unused.
  70. * @phpstan-ignore property.onlyWritten
  71. */
  72. private string $namespace = '';
  73. /**
  74. * The filename for the current configuration.
  75. */
  76. private string $config_filename = '';
  77. /**
  78. * The filename for the current default values, null by default.
  79. */
  80. private ?string $default_filename = null;
  81. /**
  82. * The configuration values, an empty array by default.
  83. * @var array<string,mixed>
  84. */
  85. private array $data = [];
  86. /**
  87. * An object which help to set good values in configuration.
  88. */
  89. private ?Minz_ConfigurationSetterInterface $configuration_setter = null;
  90. /**
  91. * Create a new Minz_Configuration object.
  92. *
  93. * @param string $namespace the name of the current configuration.
  94. * @param string $config_filename the file containing configuration values.
  95. * @param string $default_filename the file containing default values, null by default.
  96. * @param Minz_ConfigurationSetterInterface $configuration_setter an optional helper to set values in configuration @deprecated
  97. * @throws Minz_FileNotExistException
  98. */
  99. final private function __construct(string $namespace, string $config_filename, ?string $default_filename = null,
  100. ?Minz_ConfigurationSetterInterface $configuration_setter = null) {
  101. $this->namespace = $namespace;
  102. $this->config_filename = $config_filename;
  103. $this->default_filename = $default_filename;
  104. $this->_configurationSetter($configuration_setter);
  105. if ($this->default_filename != null) {
  106. $this->data = self::load($this->default_filename);
  107. }
  108. try {
  109. $overloaded = array_replace_recursive(
  110. $this->data, self::load($this->config_filename)
  111. );
  112. $this->data = array_filter($overloaded, 'is_string', ARRAY_FILTER_USE_KEY);
  113. } catch (Minz_FileNotExistException $e) {
  114. if ($this->default_filename == null) {
  115. throw $e;
  116. }
  117. }
  118. }
  119. /**
  120. * Set a configuration setter for the current configuration.
  121. * @param Minz_ConfigurationSetterInterface|null $configuration_setter the setter to call when modifying data.
  122. */
  123. public function _configurationSetter(?Minz_ConfigurationSetterInterface $configuration_setter): void {
  124. if (is_callable([$configuration_setter, 'handle'])) {
  125. $this->configuration_setter = $configuration_setter;
  126. }
  127. }
  128. #[Deprecated]
  129. public function configurationSetter(): ?Minz_ConfigurationSetterInterface {
  130. return $this->configuration_setter;
  131. }
  132. /**
  133. * Check if a parameter is defined in the configuration
  134. */
  135. public function hasParam(string $key): bool {
  136. return isset($this->data[$key]);
  137. }
  138. /** @return array<string,mixed> */
  139. public function toArray(): array {
  140. return $this->data;
  141. }
  142. /**
  143. * Return the value of the given param.
  144. *
  145. * @param string $key the name of the param.
  146. * @param mixed $default default value to return if key does not exist.
  147. * @return array|mixed value corresponding to the key.
  148. */
  149. #[Deprecated('Use `attribute*()` methods instead.')]
  150. public function param(string $key, mixed $default = null): mixed {
  151. if (isset($this->data[$key])) {
  152. return $this->data[$key];
  153. } elseif ($default !== null) {
  154. return $default;
  155. } else {
  156. Minz_Log::warning($key . ' does not exist in configuration');
  157. return null;
  158. }
  159. }
  160. /**
  161. * @return array|mixed
  162. */
  163. public function __get(string $key): mixed {
  164. if (isset($this->data[$key])) {
  165. return $this->data[$key];
  166. }
  167. Minz_Log::warning($key . ' does not exist in configuration');
  168. return null;
  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. #[Deprecated('Use `_attribute()` instead.')]
  177. public function _param(string $key, mixed $value = null): void {
  178. if ($this->configuration_setter !== null && $this->configuration_setter->support($key)) {
  179. $this->configuration_setter->handle($this->data, $key, $value);
  180. } elseif (isset($this->data[$key]) && $value === null) {
  181. unset($this->data[$key]);
  182. } elseif ($value !== null) {
  183. $this->data[$key] = $value;
  184. }
  185. }
  186. /**
  187. * {@see Minz_Configuration::_attribute()} instead.
  188. * @param string $key the param name to set.
  189. * @param mixed $value the value to set. If null, the key is removed.
  190. */
  191. public function __set(string $key, mixed $value): void {
  192. if ($value === null) {
  193. unset($this->data[$key]);
  194. } else {
  195. $this->data[$key] = $value;
  196. }
  197. }
  198. /**
  199. * Save the current configuration in the configuration file.
  200. */
  201. public function save(): bool {
  202. $tmp_filename = $this->config_filename . '.tmp.php';
  203. $back_filename = $this->config_filename . '.bak.php';
  204. if (!file_put_contents($tmp_filename,
  205. "<?php\nreturn " . var_export($this->data, true) . ';', LOCK_EX)) {
  206. @unlink($tmp_filename);
  207. return false;
  208. }
  209. if (!copy($this->config_filename, $back_filename)) {
  210. @unlink($tmp_filename);
  211. return false;
  212. }
  213. @rename($tmp_filename, $this->config_filename);
  214. // Clear PHP cache for include
  215. if (function_exists('opcache_invalidate')) {
  216. opcache_invalidate($this->config_filename);
  217. }
  218. return true;
  219. }
  220. /**
  221. * @param non-empty-string $key
  222. * @return array<int|string,mixed>|null
  223. */
  224. public function attributeArray(string $key): ?array {
  225. $a = $this->data[$key] ?? null;
  226. return is_array($a) ? $a : null;
  227. }
  228. /** @param non-empty-string $key */
  229. public function attributeBool(string $key): ?bool {
  230. $a = $this->data[$key] ?? null;
  231. return is_bool($a) ? $a : null;
  232. }
  233. /** @param non-empty-string $key */
  234. public function attributeInt(string $key): ?int {
  235. $a = $this->data[$key] ?? null;
  236. return is_numeric($a) ? (int)$a : null;
  237. }
  238. /** @param non-empty-string $key */
  239. public function attributeString(string $key): ?string {
  240. $a = $this->data[$key] ?? null;
  241. return is_string($a) ? $a : null;
  242. }
  243. /**
  244. * @param non-empty-string $key
  245. * @param array<string,mixed>|mixed|null $value Value, not HTML-encoded
  246. */
  247. public function _attribute(string $key, $value = null): void {
  248. if (isset($this->data[$key]) && $value === null) {
  249. unset($this->data[$key]);
  250. } elseif ($value !== null) {
  251. $this->data[$key] = $value;
  252. }
  253. }
  254. }