IAppConfig.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCP\AppFramework\Services;
  8. use OCP\Exceptions\AppConfigUnknownKeyException;
  9. /**
  10. * Wrapper for AppConfig for the AppFramework
  11. *
  12. * @since 20.0.0
  13. */
  14. interface IAppConfig {
  15. /**
  16. * Get all keys stored for this app
  17. *
  18. * @return string[] the keys stored for the app
  19. * @since 20.0.0
  20. */
  21. public function getAppKeys(): array;
  22. /**
  23. * Check if a key exists in the list of stored config values.
  24. *
  25. * @param string $key config key
  26. * @param bool $lazy search within lazy loaded config
  27. *
  28. * @return bool TRUE if key exists
  29. * @since 29.0.0
  30. */
  31. public function hasAppKey(string $key, ?bool $lazy = false): bool;
  32. /**
  33. * best way to see if a value is set as sensitive (not displayed in report)
  34. *
  35. * @param string $key config key
  36. * @param bool|null $lazy search within lazy loaded config
  37. *
  38. * @return bool TRUE if value is sensitive
  39. * @throws AppConfigUnknownKeyException if config key is not known
  40. * @since 29.0.0
  41. */
  42. public function isSensitive(string $key, ?bool $lazy = false): bool;
  43. /**
  44. * Returns if the config key stored in database is lazy loaded
  45. *
  46. * **WARNING:** ignore lazy filtering, all config values are loaded from database
  47. *
  48. * @param string $key config key
  49. *
  50. * @return bool TRUE if config is lazy loaded
  51. * @throws AppConfigUnknownKeyException if config key is not known
  52. * @see IAppConfig for details about lazy loading
  53. * @since 29.0.0
  54. */
  55. public function isLazy(string $key): bool;
  56. /**
  57. * List all config values from an app with config key starting with $key.
  58. * Returns an array with config key as key, stored value as value.
  59. *
  60. * **WARNING:** ignore lazy filtering, all config values are loaded from database
  61. *
  62. * @param string $key config keys prefix to search, can be empty.
  63. * @param bool $filtered filter sensitive config values
  64. *
  65. * @return array<string, string|int|float|bool|array> [configKey => configValue]
  66. * @since 29.0.0
  67. */
  68. public function getAllAppValues(string $key = '', bool $filtered = false): array;
  69. /**
  70. * Writes a new app wide value
  71. *
  72. * @param string $key the key of the value, under which will be saved
  73. * @param string $value the value that should be stored
  74. * @return void
  75. * @since 20.0.0
  76. * @deprecated 29.0.0 use {@see setAppValueString()}
  77. */
  78. public function setAppValue(string $key, string $value): void;
  79. /**
  80. * Store a config key and its value in database
  81. *
  82. * If config key is already known with the exact same config value, the database is not updated.
  83. * If config key is not supposed to be read during the boot of the cloud, it is advised to set it as lazy loaded.
  84. *
  85. * If config value was previously stored as sensitive or lazy loaded, status cannot be altered without using {@see deleteKey()} first
  86. *
  87. * @param string $key config key
  88. * @param string $value config value
  89. * @param bool $sensitive if TRUE value will be hidden when listing config values.
  90. * @param bool $lazy set config as lazy loaded
  91. *
  92. * @return bool TRUE if value was different, therefor updated in database
  93. * @since 29.0.0
  94. * @see \OCP\IAppConfig for explanation about lazy loading
  95. */
  96. public function setAppValueString(string $key, string $value, bool $lazy = false, bool $sensitive = false): bool;
  97. /**
  98. * Store a config key and its value in database
  99. *
  100. * When handling huge value around and/or above 2,147,483,647, a debug log will be generated
  101. * on 64bits system, as php int type reach its limit (and throw an exception) on 32bits when using huge numbers.
  102. *
  103. * When using huge numbers, it is advised to use {@see \OCP\Util::numericToNumber()} and {@see setValueString()}
  104. *
  105. * If config key is already known with the exact same config value, the database is not updated.
  106. * If config key is not supposed to be read during the boot of the cloud, it is advised to set it as lazy loaded.
  107. *
  108. * If config value was previously stored as sensitive or lazy loaded, status cannot be altered without using {@see deleteKey()} first
  109. *
  110. * @param string $key config key
  111. * @param int $value config value
  112. * @param bool $sensitive if TRUE value will be hidden when listing config values.
  113. * @param bool $lazy set config as lazy loaded
  114. *
  115. * @return bool TRUE if value was different, therefor updated in database
  116. * @since 29.0.0
  117. * @see \OCP\IAppConfig for explanation about lazy loading
  118. */
  119. public function setAppValueInt(string $key, int $value, bool $lazy = false, bool $sensitive = false): bool;
  120. /**
  121. * Store a config key and its value in database.
  122. *
  123. * If config key is already known with the exact same config value, the database is not updated.
  124. * If config key is not supposed to be read during the boot of the cloud, it is advised to set it as lazy loaded.
  125. *
  126. * If config value was previously stored as sensitive or lazy loaded, status cannot be altered without using {@see deleteKey()} first
  127. *
  128. * @param string $key config key
  129. * @param float $value config value
  130. * @param bool $sensitive if TRUE value will be hidden when listing config values.
  131. * @param bool $lazy set config as lazy loaded
  132. *
  133. * @return bool TRUE if value was different, therefor updated in database
  134. * @since 29.0.0
  135. * @see \OCP\IAppConfig for explanation about lazy loading
  136. */
  137. public function setAppValueFloat(string $key, float $value, bool $lazy = false, bool $sensitive = false): bool;
  138. /**
  139. * Store a config key and its value in database
  140. *
  141. * If config key is already known with the exact same config value, the database is not updated.
  142. * If config key is not supposed to be read during the boot of the cloud, it is advised to set it as lazy loaded.
  143. *
  144. * If config value was previously stored as lazy loaded, status cannot be altered without using {@see deleteKey()} first
  145. *
  146. * @param string $key config key
  147. * @param bool $value config value
  148. * @param bool $lazy set config as lazy loaded
  149. *
  150. * @return bool TRUE if value was different, therefor updated in database
  151. * @since 29.0.0
  152. * @see \OCP\IAppConfig for explanation about lazy loading
  153. */
  154. public function setAppValueBool(string $key, bool $value, bool $lazy = false): bool;
  155. /**
  156. * Store a config key and its value in database
  157. *
  158. * If config key is already known with the exact same config value, the database is not updated.
  159. * If config key is not supposed to be read during the boot of the cloud, it is advised to set it as lazy loaded.
  160. *
  161. * If config value was previously stored as sensitive or lazy loaded, status cannot be altered without using {@see deleteKey()} first
  162. *
  163. * @param string $key config key
  164. * @param array $value config value
  165. * @param bool $sensitive if TRUE value will be hidden when listing config values.
  166. * @param bool $lazy set config as lazy loaded
  167. *
  168. * @return bool TRUE if value was different, therefor updated in database
  169. * @since 29.0.0
  170. * @see \OCP\IAppConfig for explanation about lazy loading
  171. */
  172. public function setAppValueArray(string $key, array $value, bool $lazy = false, bool $sensitive = false): bool;
  173. /**
  174. * Looks up an app wide defined value
  175. *
  176. * @param string $key the key of the value, under which it was saved
  177. * @param string $default the default value to be returned if the value isn't set
  178. *
  179. * @return string the saved value
  180. * @since 20.0.0
  181. * @deprecated 29.0.0 use {@see getAppValueString()}
  182. */
  183. public function getAppValue(string $key, string $default = ''): string;
  184. /**
  185. * Get config value assigned to a config key.
  186. * If config key is not found in database, default value is returned.
  187. * If config key is set as lazy loaded, the $lazy argument needs to be set to TRUE.
  188. *
  189. * @param string $key config key
  190. * @param string $default default value
  191. * @param bool $lazy search within lazy loaded config
  192. *
  193. * @return string stored config value or $default if not set in database
  194. * @since 29.0.0
  195. * @see \OCP\IAppConfig for explanation about lazy loading
  196. */
  197. public function getAppValueString(string $key, string $default = '', bool $lazy = false): string;
  198. /**
  199. * Get config value assigned to a config key.
  200. * If config key is not found in database, default value is returned.
  201. * If config key is set as lazy loaded, the $lazy argument needs to be set to TRUE.
  202. *
  203. * @param string $key config key
  204. * @param int $default default value
  205. * @param bool $lazy search within lazy loaded config
  206. *
  207. * @return int stored config value or $default if not set in database
  208. * @since 29.0.0
  209. * @see \OCP\IAppConfig for explanation about lazy loading
  210. */
  211. public function getAppValueInt(string $key, int $default = 0, bool $lazy = false): int;
  212. /**
  213. * Get config value assigned to a config key.
  214. * If config key is not found in database, default value is returned.
  215. * If config key is set as lazy loaded, the $lazy argument needs to be set to TRUE.
  216. *
  217. * @param string $key config key
  218. * @param float $default default value
  219. * @param bool $lazy search within lazy loaded config
  220. *
  221. * @return float stored config value or $default if not set in database
  222. * @since 29.0.0
  223. * @see \OCP\IAppConfig for explanation about lazy loading
  224. */
  225. public function getAppValueFloat(string $key, float $default = 0, bool $lazy = false): float;
  226. /**
  227. * Get config value assigned to a config key.
  228. * If config key is not found in database, default value is returned.
  229. * If config key is set as lazy loaded, the $lazy argument needs to be set to TRUE.
  230. *
  231. * @param string $key config key
  232. * @param bool $default default value
  233. * @param bool $lazy search within lazy loaded config
  234. *
  235. * @return bool stored config value or $default if not set in database
  236. * @since 29.0.0
  237. * @see \OCP\IAppConfig for explanation about lazy loading
  238. */
  239. public function getAppValueBool(string $key, bool $default = false, bool $lazy = false): bool;
  240. /**
  241. * Get config value assigned to a config key.
  242. * If config key is not found in database, default value is returned.
  243. * If config key is set as lazy loaded, the $lazy argument needs to be set to TRUE.
  244. *
  245. * @param string $key config key
  246. * @param array $default default value
  247. * @param bool $lazy search within lazy loaded config
  248. *
  249. * @return array stored config value or $default if not set in database
  250. * @since 29.0.0
  251. * @see \OCP\IAppConfig for explanation about lazy loading
  252. */
  253. public function getAppValueArray(string $key, array $default = [], bool $lazy = false): array;
  254. /**
  255. * Delete an app wide defined value
  256. *
  257. * @param string $key the key of the value, under which it was saved
  258. * @return void
  259. * @since 20.0.0
  260. */
  261. public function deleteAppValue(string $key): void;
  262. /**
  263. * Removes all keys in appconfig belonging to the app
  264. *
  265. * @return void
  266. * @since 20.0.0
  267. */
  268. public function deleteAppValues(): void;
  269. /**
  270. * Set a user defined value
  271. *
  272. * @param string $userId the userId of the user that we want to store the value under
  273. * @param string $key the key under which the value is being stored
  274. * @param string $value the value that you want to store
  275. * @param string $preCondition only update if the config value was previously the value passed as $preCondition
  276. * @throws \OCP\PreConditionNotMetException if a precondition is specified and is not met
  277. * @throws \UnexpectedValueException when trying to store an unexpected value
  278. * @since 20.0.0
  279. */
  280. public function setUserValue(string $userId, string $key, string $value, ?string $preCondition = null): void;
  281. /**
  282. * Shortcut for getting a user defined value
  283. *
  284. * @param string $userId the userId of the user that we want to store the value under
  285. * @param string $key the key under which the value is being stored
  286. * @param mixed $default the default value to be returned if the value isn't set
  287. * @return string
  288. * @since 20.0.0
  289. */
  290. public function getUserValue(string $userId, string $key, string $default = ''): string;
  291. /**
  292. * Delete a user value
  293. *
  294. * @param string $userId the userId of the user that we want to store the value under
  295. * @param string $key the key under which the value is being stored
  296. * @since 20.0.0
  297. */
  298. public function deleteUserValue(string $userId, string $key): void;
  299. }