Configuration.php 8.4 KB

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