Configuration.php 8.5 KB

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