You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

IAppConfig.php 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2020, Roeland Jago Douma <roeland@famdouma.nl>
  5. *
  6. * @author Maxence Lange <maxence@artificial-owl.com>
  7. * @author Roeland Jago Douma <roeland@famdouma.nl>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OCP\AppFramework\Services;
  26. use OCP\Exceptions\AppConfigUnknownKeyException;
  27. /**
  28. * Wrapper for AppConfig for the AppFramework
  29. *
  30. * @since 20.0.0
  31. */
  32. interface IAppConfig {
  33. /**
  34. * Get all keys stored for this app
  35. *
  36. * @return string[] the keys stored for the app
  37. * @since 20.0.0
  38. */
  39. public function getAppKeys(): array;
  40. /**
  41. * Check if a key exists in the list of stored config values.
  42. *
  43. * @param string $key config key
  44. * @param bool $lazy search within lazy loaded config
  45. *
  46. * @return bool TRUE if key exists
  47. * @since 29.0.0
  48. */
  49. public function hasAppKey(string $key, ?bool $lazy = false): bool;
  50. /**
  51. * best way to see if a value is set as sensitive (not displayed in report)
  52. *
  53. * @param string $key config key
  54. * @param bool|null $lazy search within lazy loaded config
  55. *
  56. * @return bool TRUE if value is sensitive
  57. * @throws AppConfigUnknownKeyException if config key is not known
  58. * @since 29.0.0
  59. */
  60. public function isSensitive(string $key, ?bool $lazy = false): bool;
  61. /**
  62. * Returns if the config key stored in database is lazy loaded
  63. *
  64. * **WARNING:** ignore lazy filtering, all config values are loaded from database
  65. *
  66. * @param string $key config key
  67. *
  68. * @return bool TRUE if config is lazy loaded
  69. * @throws AppConfigUnknownKeyException if config key is not known
  70. * @see IAppConfig for details about lazy loading
  71. * @since 29.0.0
  72. */
  73. public function isLazy(string $key): bool;
  74. /**
  75. * List all config values from an app with config key starting with $key.
  76. * Returns an array with config key as key, stored value as value.
  77. *
  78. * **WARNING:** ignore lazy filtering, all config values are loaded from database
  79. *
  80. * @param string $key config keys prefix to search, can be empty.
  81. * @param bool $filtered filter sensitive config values
  82. *
  83. * @return array<string, string|int|float|bool|array> [configKey => configValue]
  84. * @since 29.0.0
  85. */
  86. public function getAllAppValues(string $key = '', bool $filtered = false): array;
  87. /**
  88. * Writes a new app wide value
  89. *
  90. * @param string $key the key of the value, under which will be saved
  91. * @param string $value the value that should be stored
  92. * @return void
  93. * @since 20.0.0
  94. * @deprecated 29.0.0 use {@see setAppValueString()}
  95. */
  96. public function setAppValue(string $key, string $value): void;
  97. /**
  98. * Store a config key and its value in database
  99. *
  100. * If config key is already known with the exact same config value, the database is not updated.
  101. * If config key is not supposed to be read during the boot of the cloud, it is advised to set it as lazy loaded.
  102. *
  103. * If config value was previously stored as sensitive or lazy loaded, status cannot be altered without using {@see deleteKey()} first
  104. *
  105. * @param string $key config key
  106. * @param string $value config value
  107. * @param bool $sensitive if TRUE value will be hidden when listing config values.
  108. * @param bool $lazy set config as lazy loaded
  109. *
  110. * @return bool TRUE if value was different, therefor updated in database
  111. * @since 29.0.0
  112. * @see \OCP\IAppConfig for explanation about lazy loading
  113. */
  114. public function setAppValueString(string $key, string $value, bool $lazy = false, bool $sensitive = false): bool;
  115. /**
  116. * Store a config key and its value in database
  117. *
  118. * When handling huge value around and/or above 2,147,483,647, a debug log will be generated
  119. * on 64bits system, as php int type reach its limit (and throw an exception) on 32bits when using huge numbers.
  120. *
  121. * When using huge numbers, it is advised to use {@see \OCP\Util::numericToNumber()} and {@see setValueString()}
  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 int $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 setAppValueInt(string $key, int $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 sensitive or lazy loaded, status cannot be altered without using {@see deleteKey()} first
  145. *
  146. * @param string $key config key
  147. * @param float $value config value
  148. * @param bool $sensitive if TRUE value will be hidden when listing config values.
  149. * @param bool $lazy set config as lazy loaded
  150. *
  151. * @return bool TRUE if value was different, therefor updated in database
  152. * @since 29.0.0
  153. * @see \OCP\IAppConfig for explanation about lazy loading
  154. */
  155. public function setAppValueFloat(string $key, float $value, bool $lazy = false, bool $sensitive = false): bool;
  156. /**
  157. * Store a config key and its value in database
  158. *
  159. * If config key is already known with the exact same config value, the database is not updated.
  160. * If config key is not supposed to be read during the boot of the cloud, it is advised to set it as lazy loaded.
  161. *
  162. * If config value was previously stored as lazy loaded, status cannot be altered without using {@see deleteKey()} first
  163. *
  164. * @param string $key config key
  165. * @param bool $value config value
  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 setAppValueBool(string $key, bool $value, bool $lazy = false): bool;
  173. /**
  174. * Store a config key and its value in database
  175. *
  176. * If config key is already known with the exact same config value, the database is not updated.
  177. * If config key is not supposed to be read during the boot of the cloud, it is advised to set it as lazy loaded.
  178. *
  179. * If config value was previously stored as sensitive or lazy loaded, status cannot be altered without using {@see deleteKey()} first
  180. *
  181. * @param string $key config key
  182. * @param array $value config value
  183. * @param bool $sensitive if TRUE value will be hidden when listing config values.
  184. * @param bool $lazy set config as lazy loaded
  185. *
  186. * @return bool TRUE if value was different, therefor updated in database
  187. * @since 29.0.0
  188. * @see \OCP\IAppConfig for explanation about lazy loading
  189. */
  190. public function setAppValueArray(string $key, array $value, bool $lazy = false, bool $sensitive = false): bool;
  191. /**
  192. * Looks up an app wide defined value
  193. *
  194. * @param string $key the key of the value, under which it was saved
  195. * @param string $default the default value to be returned if the value isn't set
  196. *
  197. * @return string the saved value
  198. * @since 20.0.0
  199. * @deprecated 29.0.0 use {@see getAppValueString()}
  200. */
  201. public function getAppValue(string $key, string $default = ''): string;
  202. /**
  203. * Get config value assigned to a config key.
  204. * If config key is not found in database, default value is returned.
  205. * If config key is set as lazy loaded, the $lazy argument needs to be set to TRUE.
  206. *
  207. * @param string $key config key
  208. * @param string $default default value
  209. * @param bool $lazy search within lazy loaded config
  210. *
  211. * @return string stored config value or $default if not set in database
  212. * @since 29.0.0
  213. * @see \OCP\IAppConfig for explanation about lazy loading
  214. */
  215. public function getAppValueString(string $key, string $default = '', bool $lazy = false): string;
  216. /**
  217. * Get config value assigned to a config key.
  218. * If config key is not found in database, default value is returned.
  219. * If config key is set as lazy loaded, the $lazy argument needs to be set to TRUE.
  220. *
  221. * @param string $key config key
  222. * @param int $default default value
  223. * @param bool $lazy search within lazy loaded config
  224. *
  225. * @return int stored config value or $default if not set in database
  226. * @since 29.0.0
  227. * @see \OCP\IAppConfig for explanation about lazy loading
  228. */
  229. public function getAppValueInt(string $key, int $default = 0, bool $lazy = false): int;
  230. /**
  231. * Get config value assigned to a config key.
  232. * If config key is not found in database, default value is returned.
  233. * If config key is set as lazy loaded, the $lazy argument needs to be set to TRUE.
  234. *
  235. * @param string $key config key
  236. * @param float $default default value
  237. * @param bool $lazy search within lazy loaded config
  238. *
  239. * @return float stored config value or $default if not set in database
  240. * @since 29.0.0
  241. * @see \OCP\IAppConfig for explanation about lazy loading
  242. */
  243. public function getAppValueFloat(string $key, float $default = 0, bool $lazy = false): float;
  244. /**
  245. * Get config value assigned to a config key.
  246. * If config key is not found in database, default value is returned.
  247. * If config key is set as lazy loaded, the $lazy argument needs to be set to TRUE.
  248. *
  249. * @param string $key config key
  250. * @param bool $default default value
  251. * @param bool $lazy search within lazy loaded config
  252. *
  253. * @return bool stored config value or $default if not set in database
  254. * @since 29.0.0
  255. * @see \OCP\IAppConfig for explanation about lazy loading
  256. */
  257. public function getAppValueBool(string $key, bool $default = false, bool $lazy = false): bool;
  258. /**
  259. * Get config value assigned to a config key.
  260. * If config key is not found in database, default value is returned.
  261. * If config key is set as lazy loaded, the $lazy argument needs to be set to TRUE.
  262. *
  263. * @param string $key config key
  264. * @param array $default default value
  265. * @param bool $lazy search within lazy loaded config
  266. *
  267. * @return array stored config value or $default if not set in database
  268. * @since 29.0.0
  269. * @see \OCP\IAppConfig for explanation about lazy loading
  270. */
  271. public function getAppValueArray(string $key, array $default = [], bool $lazy = false): array;
  272. /**
  273. * Delete an app wide defined value
  274. *
  275. * @param string $key the key of the value, under which it was saved
  276. * @return void
  277. * @since 20.0.0
  278. */
  279. public function deleteAppValue(string $key): void;
  280. /**
  281. * Removes all keys in appconfig belonging to the app
  282. *
  283. * @return void
  284. * @since 20.0.0
  285. */
  286. public function deleteAppValues(): void;
  287. /**
  288. * Set a user defined value
  289. *
  290. * @param string $userId the userId of the user that we want to store the value under
  291. * @param string $key the key under which the value is being stored
  292. * @param string $value the value that you want to store
  293. * @param string $preCondition only update if the config value was previously the value passed as $preCondition
  294. * @throws \OCP\PreConditionNotMetException if a precondition is specified and is not met
  295. * @throws \UnexpectedValueException when trying to store an unexpected value
  296. * @since 20.0.0
  297. */
  298. public function setUserValue(string $userId, string $key, string $value, ?string $preCondition = null): void;
  299. /**
  300. * Shortcut for getting a user defined value
  301. *
  302. * @param string $userId the userId of the user that we want to store the value under
  303. * @param string $key the key under which the value is being stored
  304. * @param mixed $default the default value to be returned if the value isn't set
  305. * @return string
  306. * @since 20.0.0
  307. */
  308. public function getUserValue(string $userId, string $key, string $default = ''): string;
  309. /**
  310. * Delete a user value
  311. *
  312. * @param string $userId the userId of the user that we want to store the value under
  313. * @param string $key the key under which the value is being stored
  314. * @since 20.0.0
  315. */
  316. public function deleteUserValue(string $userId, string $key): void;
  317. }