Configuration.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <?php
  2. /**
  3. * Manage configuration for the application.
  4. * @property string $base_url
  5. * @property array{'type':string,'host':string,'user':string,'password':string,'base':string,'prefix':string,
  6. * 'connection_uri_params':string,'pdo_options':array<int,int|string|bool>} $db
  7. * @property-read string $disable_update
  8. * @property-read string $environment
  9. * @property array<string,bool> $extensions_enabled
  10. * @property-read string $mailer
  11. * @property-read array{'hostname':string,'host':string,'auth':bool,'username':string,'password':string,'secure':string,'port':int,'from':string} $smtp
  12. * @property string $title
  13. */
  14. class Minz_Configuration {
  15. /**
  16. * The list of configurations.
  17. * @var array<string,static>
  18. */
  19. private static $config_list = array();
  20. /**
  21. * Add a new configuration to the list of configuration.
  22. *
  23. * @param string $namespace the name of the current configuration
  24. * @param string $config_filename the filename of the configuration
  25. * @param string $default_filename a filename containing default values for the configuration
  26. * @param Minz_ConfigurationSetterInterface $configuration_setter an optional helper to set values in configuration
  27. */
  28. public static function register(string $namespace, string $config_filename, string $default_filename = null,
  29. Minz_ConfigurationSetterInterface $configuration_setter = null): void {
  30. self::$config_list[$namespace] = new static(
  31. $namespace, $config_filename, $default_filename, $configuration_setter
  32. );
  33. }
  34. /**
  35. * Parse a file and return its data.
  36. *
  37. * @param string $filename the name of the file to parse.
  38. * @return array<string,mixed> of values
  39. * @throws Minz_FileNotExistException if the file does not exist or is invalid.
  40. */
  41. public static function load(string $filename): array {
  42. $data = @include($filename);
  43. if (is_array($data)) {
  44. return $data;
  45. } else {
  46. throw new Minz_FileNotExistException($filename);
  47. }
  48. }
  49. /**
  50. * Return the configuration related to a given namespace.
  51. *
  52. * @param string $namespace the name of the configuration to get.
  53. * @return static object
  54. * @throws Minz_ConfigurationNamespaceException if the namespace does not exist.
  55. */
  56. public static function get(string $namespace) {
  57. if (!isset(self::$config_list[$namespace])) {
  58. throw new Minz_ConfigurationNamespaceException(
  59. $namespace . ' namespace does not exist'
  60. );
  61. }
  62. return self::$config_list[$namespace];
  63. }
  64. /**
  65. * The namespace of the current configuration.
  66. * Unused.
  67. * @phpstan-ignore-next-line
  68. */
  69. private $namespace = '';
  70. /**
  71. * The filename for the current configuration.
  72. * @var string
  73. */
  74. private $config_filename = '';
  75. /**
  76. * The filename for the current default values, null by default.
  77. * @var string|null
  78. */
  79. private $default_filename = null;
  80. /**
  81. * The configuration values, an empty array by default.
  82. * @var array<string,mixed>
  83. */
  84. private $data = array();
  85. /**
  86. * An object which help to set good values in configuration.
  87. * @var Minz_ConfigurationSetterInterface|null
  88. */
  89. private $configuration_setter;
  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
  97. */
  98. private final 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. $this->data = array_replace_recursive(
  109. $this->data, self::load($this->config_filename)
  110. );
  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(array($configuration_setter, 'handle'))) {
  123. $this->configuration_setter = $configuration_setter;
  124. }
  125. }
  126. public function configurationSetter(): ?Minz_ConfigurationSetterInterface {
  127. return $this->configuration_setter;
  128. }
  129. /**
  130. * Check if a parameter is defined in the configuration
  131. */
  132. public function hasParam(string $key): bool {
  133. return isset($this->data[$key]);
  134. }
  135. /**
  136. * Return the value of the given param.
  137. *
  138. * @param string $key the name of the param.
  139. * @param mixed $default default value to return if key does not exist.
  140. * @return array|mixed value corresponding to the key.
  141. * @throws Minz_ConfigurationParamException if the param does not exist
  142. */
  143. public function param(string $key, $default = null) {
  144. if (isset($this->data[$key])) {
  145. return $this->data[$key];
  146. } elseif (!is_null($default)) {
  147. return $default;
  148. } else {
  149. Minz_Log::warning($key . ' does not exist in configuration');
  150. return null;
  151. }
  152. }
  153. /**
  154. * A wrapper for param().
  155. * @return array|mixed
  156. */
  157. public function __get(string $key) {
  158. return $this->param($key);
  159. }
  160. /**
  161. * Set or remove a param.
  162. *
  163. * @param string $key the param name to set.
  164. * @param mixed $value the value to set. If null, the key is removed from the configuration.
  165. */
  166. public function _param(string $key, $value = null): void {
  167. if ($this->configuration_setter !== null && $this->configuration_setter->support($key)) {
  168. $this->configuration_setter->handle($this->data, $key, $value);
  169. } elseif (isset($this->data[$key]) && is_null($value)) {
  170. unset($this->data[$key]);
  171. } elseif ($value !== null) {
  172. $this->data[$key] = $value;
  173. }
  174. }
  175. /**
  176. * A wrapper for _param().
  177. * @param mixed $value
  178. */
  179. public function __set(string $key, $value): void {
  180. $this->_param($key, $value);
  181. }
  182. /**
  183. * Save the current configuration in the configuration file.
  184. */
  185. public function save(): bool {
  186. $back_filename = $this->config_filename . '.bak.php';
  187. @rename($this->config_filename, $back_filename);
  188. if (file_put_contents($this->config_filename,
  189. "<?php\nreturn " . var_export($this->data, true) . ';', LOCK_EX) === false) {
  190. return false;
  191. }
  192. // Clear PHP cache for include
  193. if (function_exists('opcache_invalidate')) {
  194. opcache_invalidate($this->config_filename);
  195. }
  196. return true;
  197. }
  198. }