IAppConfig.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  6. * SPDX-License-Identifier: AGPL-3.0-only
  7. */
  8. namespace OCP;
  9. use OCP\Exceptions\AppConfigUnknownKeyException;
  10. /**
  11. * This class provides an easy way for apps to store config values in the
  12. * database.
  13. *
  14. * **Note:** since 29.0.0, it supports **lazy loading**
  15. *
  16. * ### What is lazy loading ?
  17. * In order to avoid loading useless config values into memory for each request,
  18. * only non-lazy values are now loaded.
  19. *
  20. * Once a value that is lazy is requested, all lazy values will be loaded.
  21. *
  22. * Similarly, some methods from this class are marked with a warning about ignoring
  23. * lazy loading. Use them wisely and only on parts of the code that are called
  24. * during specific requests or actions to avoid loading the lazy values all the time.
  25. *
  26. * @since 7.0.0
  27. * @since 29.0.0 - Supporting types and lazy loading
  28. */
  29. interface IAppConfig {
  30. /** @since 29.0.0 */
  31. public const VALUE_SENSITIVE = 1;
  32. /** @since 29.0.0 */
  33. public const VALUE_MIXED = 2;
  34. /** @since 29.0.0 */
  35. public const VALUE_STRING = 4;
  36. /** @since 29.0.0 */
  37. public const VALUE_INT = 8;
  38. /** @since 29.0.0 */
  39. public const VALUE_FLOAT = 16;
  40. /** @since 29.0.0 */
  41. public const VALUE_BOOL = 32;
  42. /** @since 29.0.0 */
  43. public const VALUE_ARRAY = 64;
  44. /** @since 31.0.0 */
  45. public const FLAG_SENSITIVE = 1; // value is sensitive
  46. /**
  47. * Get list of all apps that have at least one config value stored in database
  48. *
  49. * **WARNING:** ignore lazy filtering, all config values are loaded from database
  50. *
  51. * @return list<string> list of app ids
  52. * @since 7.0.0
  53. */
  54. public function getApps(): array;
  55. /**
  56. * Returns all keys stored in database, related to an app.
  57. * Please note that the values are not returned.
  58. *
  59. * **WARNING:** ignore lazy filtering, all config values are loaded from database
  60. *
  61. * @param string $app id of the app
  62. *
  63. * @return list<string> list of stored config keys
  64. * @since 29.0.0
  65. */
  66. public function getKeys(string $app): array;
  67. /**
  68. * Check if a key exists in the list of stored config values.
  69. *
  70. * @param string $app id of the app
  71. * @param string $key config key
  72. * @param bool $lazy search within lazy loaded config
  73. *
  74. * @return bool TRUE if key exists
  75. * @since 29.0.0 Added the $lazy argument
  76. * @since 7.0.0
  77. */
  78. public function hasKey(string $app, string $key, ?bool $lazy = false): bool;
  79. /**
  80. * best way to see if a value is set as sensitive (not displayed in report)
  81. *
  82. * @param string $app id of the app
  83. * @param string $key config key
  84. * @param bool|null $lazy search within lazy loaded config
  85. *
  86. * @return bool TRUE if value is sensitive
  87. * @throws AppConfigUnknownKeyException if config key is not known
  88. * @since 29.0.0
  89. */
  90. public function isSensitive(string $app, string $key, ?bool $lazy = false): bool;
  91. /**
  92. * Returns if the config key stored in database is lazy loaded
  93. *
  94. * **WARNING:** ignore lazy filtering, all config values are loaded from database
  95. *
  96. * @param string $app id of the app
  97. * @param string $key config key
  98. *
  99. * @return bool TRUE if config is lazy loaded
  100. * @throws AppConfigUnknownKeyException if config key is not known
  101. * @see IAppConfig for details about lazy loading
  102. * @since 29.0.0
  103. */
  104. public function isLazy(string $app, string $key): bool;
  105. /**
  106. * List all config values from an app with config key starting with $key.
  107. * Returns an array with config key as key, stored value as value.
  108. *
  109. * **WARNING:** ignore lazy filtering, all config values are loaded from database
  110. *
  111. * @param string $app id of the app
  112. * @param string $prefix config keys prefix to search, can be empty.
  113. * @param bool $filtered filter sensitive config values
  114. *
  115. * @return array<string, string|int|float|bool|array> [configKey => configValue]
  116. * @since 29.0.0
  117. */
  118. public function getAllValues(string $app, string $prefix = '', bool $filtered = false): array;
  119. /**
  120. * List all apps storing a specific config key and its stored value.
  121. * Returns an array with appId as key, stored value as value.
  122. *
  123. * @param string $key config key
  124. * @param bool $lazy search within lazy loaded config
  125. * @param int|null $typedAs enforce type for the returned values {@see self::VALUE_STRING} and others
  126. *
  127. * @return array<string, string|int|float|bool|array> [appId => configValue]
  128. * @since 29.0.0
  129. */
  130. public function searchValues(string $key, bool $lazy = false, ?int $typedAs = null): array;
  131. /**
  132. * Get config value assigned to a config key.
  133. * If config key is not found in database, default value is returned.
  134. * If config key is set as lazy loaded, the $lazy argument needs to be set to TRUE.
  135. *
  136. * @param string $app id of the app
  137. * @param string $key config key
  138. * @param string $default default value
  139. * @param bool $lazy search within lazy loaded config
  140. *
  141. * @return string stored config value or $default if not set in database
  142. * @since 29.0.0
  143. * @see IAppConfig for explanation about lazy loading
  144. * @see getValueInt()
  145. * @see getValueFloat()
  146. * @see getValueBool()
  147. * @see getValueArray()
  148. */
  149. public function getValueString(string $app, string $key, string $default = '', bool $lazy = false): string;
  150. /**
  151. * Get config value assigned to a config key.
  152. * If config key is not found in database, default value is returned.
  153. * If config key is set as lazy loaded, the $lazy argument needs to be set to TRUE.
  154. *
  155. * @param string $app id of the app
  156. * @param string $key config key
  157. * @param int $default default value
  158. * @param bool $lazy search within lazy loaded config
  159. *
  160. * @return int stored config value or $default if not set in database
  161. * @since 29.0.0
  162. * @see IAppConfig for explanation about lazy loading
  163. * @see getValueString()
  164. * @see getValueFloat()
  165. * @see getValueBool()
  166. * @see getValueArray()
  167. */
  168. public function getValueInt(string $app, string $key, int $default = 0, bool $lazy = false): int;
  169. /**
  170. * Get config value assigned to a config key.
  171. * If config key is not found in database, default value is returned.
  172. * If config key is set as lazy loaded, the $lazy argument needs to be set to TRUE.
  173. *
  174. * @param string $app id of the app
  175. * @param string $key config key
  176. * @param float $default default value
  177. * @param bool $lazy search within lazy loaded config
  178. *
  179. * @return float stored config value or $default if not set in database
  180. * @since 29.0.0
  181. * @see IAppConfig for explanation about lazy loading
  182. * @see getValueString()
  183. * @see getValueInt()
  184. * @see getValueBool()
  185. * @see getValueArray()
  186. */
  187. public function getValueFloat(string $app, string $key, float $default = 0, bool $lazy = false): float;
  188. /**
  189. * Get config value assigned to a config key.
  190. * If config key is not found in database, default value is returned.
  191. * If config key is set as lazy loaded, the $lazy argument needs to be set to TRUE.
  192. *
  193. * @param string $app id of the app
  194. * @param string $key config key
  195. * @param bool $default default value
  196. * @param bool $lazy search within lazy loaded config
  197. *
  198. * @return bool stored config value or $default if not set in database
  199. * @since 29.0.0
  200. * @see IAppConfig for explanation about lazy loading
  201. * @see getValueString()
  202. * @see getValueInt()
  203. * @see getValueFloat()
  204. * @see getValueArray()
  205. */
  206. public function getValueBool(string $app, string $key, bool $default = false, bool $lazy = false): bool;
  207. /**
  208. * Get config value assigned to a config key.
  209. * If config key is not found in database, default value is returned.
  210. * If config key is set as lazy loaded, the $lazy argument needs to be set to TRUE.
  211. *
  212. * @param string $app id of the app
  213. * @param string $key config key
  214. * @param array $default default value
  215. * @param bool $lazy search within lazy loaded config
  216. *
  217. * @return array stored config value or $default if not set in database
  218. * @since 29.0.0
  219. * @see IAppConfig for explanation about lazy loading
  220. * @see getValueString()
  221. * @see getValueInt()
  222. * @see getValueFloat()
  223. * @see getValueBool()
  224. */
  225. public function getValueArray(string $app, string $key, array $default = [], bool $lazy = false): array;
  226. /**
  227. * returns the type of config value
  228. *
  229. * **WARNING:** ignore lazy filtering, all config values are loaded from database
  230. * unless lazy is set to false
  231. *
  232. * @param string $app id of the app
  233. * @param string $key config key
  234. * @param bool|null $lazy
  235. *
  236. * @return int
  237. * @throws AppConfigUnknownKeyException
  238. * @since 29.0.0
  239. * @see VALUE_STRING
  240. * @see VALUE_INT
  241. * @see VALUE_FLOAT
  242. * @see VALUE_BOOL
  243. * @see VALUE_ARRAY
  244. */
  245. public function getValueType(string $app, string $key, ?bool $lazy = null): int;
  246. /**
  247. * Store a config key and its value in database
  248. *
  249. * If config key is already known with the exact same config value, the database is not updated.
  250. * If config key is not supposed to be read during the boot of the cloud, it is advised to set it as lazy loaded.
  251. *
  252. * If config value was previously stored as sensitive or lazy loaded, status cannot be altered without using {@see deleteKey()} first
  253. *
  254. * @param string $app id of the app
  255. * @param string $key config key
  256. * @param string $value config value
  257. * @param bool $sensitive if TRUE value will be hidden when listing config values.
  258. * @param bool $lazy set config as lazy loaded
  259. *
  260. * @return bool TRUE if value was different, therefor updated in database
  261. * @since 29.0.0
  262. * @see IAppConfig for explanation about lazy loading
  263. * @see setValueInt()
  264. * @see setValueFloat()
  265. * @see setValueBool()
  266. * @see setValueArray()
  267. */
  268. public function setValueString(string $app, string $key, string $value, bool $lazy = false, bool $sensitive = false): bool;
  269. /**
  270. * Store a config key and its value in database
  271. *
  272. * When handling huge value around and/or above 2,147,483,647, a debug log will be generated
  273. * on 64bits system, as php int type reach its limit (and throw an exception) on 32bits when using huge numbers.
  274. *
  275. * When using huge numbers, it is advised to use {@see \OCP\Util::numericToNumber()} and {@see setValueString()}
  276. *
  277. * If config key is already known with the exact same config value, the database is not updated.
  278. * If config key is not supposed to be read during the boot of the cloud, it is advised to set it as lazy loaded.
  279. *
  280. * If config value was previously stored as sensitive or lazy loaded, status cannot be altered without using {@see deleteKey()} first
  281. *
  282. * @param string $app id of the app
  283. * @param string $key config key
  284. * @param int $value config value
  285. * @param bool $sensitive if TRUE value will be hidden when listing config values.
  286. * @param bool $lazy set config as lazy loaded
  287. *
  288. * @return bool TRUE if value was different, therefor updated in database
  289. * @since 29.0.0
  290. * @see IAppConfig for explanation about lazy loading
  291. * @see setValueString()
  292. * @see setValueFloat()
  293. * @see setValueBool()
  294. * @see setValueArray()
  295. */
  296. public function setValueInt(string $app, string $key, int $value, bool $lazy = false, bool $sensitive = false): bool;
  297. /**
  298. * Store a config key and its value in database.
  299. *
  300. * If config key is already known with the exact same config value, the database is not updated.
  301. * If config key is not supposed to be read during the boot of the cloud, it is advised to set it as lazy loaded.
  302. *
  303. * If config value was previously stored as sensitive or lazy loaded, status cannot be altered without using {@see deleteKey()} first
  304. *
  305. * @param string $app id of the app
  306. * @param string $key config key
  307. * @param float $value config value
  308. * @param bool $sensitive if TRUE value will be hidden when listing config values.
  309. * @param bool $lazy set config as lazy loaded
  310. *
  311. * @return bool TRUE if value was different, therefor updated in database
  312. * @since 29.0.0
  313. * @see IAppConfig for explanation about lazy loading
  314. * @see setValueString()
  315. * @see setValueInt()
  316. * @see setValueBool()
  317. * @see setValueArray()
  318. */
  319. public function setValueFloat(string $app, string $key, float $value, bool $lazy = false, bool $sensitive = false): bool;
  320. /**
  321. * Store a config key and its value in database
  322. *
  323. * If config key is already known with the exact same config value, the database is not updated.
  324. * If config key is not supposed to be read during the boot of the cloud, it is advised to set it as lazy loaded.
  325. *
  326. * If config value was previously stored as lazy loaded, status cannot be altered without using {@see deleteKey()} first
  327. *
  328. * @param string $app id of the app
  329. * @param string $key config key
  330. * @param bool $value config value
  331. * @param bool $lazy set config as lazy loaded
  332. *
  333. * @return bool TRUE if value was different, therefor updated in database
  334. * @since 29.0.0
  335. * @see IAppConfig for explanation about lazy loading
  336. * @see setValueString()
  337. * @see setValueInt()
  338. * @see setValueFloat()
  339. * @see setValueArray()
  340. */
  341. public function setValueBool(string $app, string $key, bool $value, bool $lazy = false): bool;
  342. /**
  343. * Store a config key and its value in database
  344. *
  345. * If config key is already known with the exact same config value, the database is not updated.
  346. * If config key is not supposed to be read during the boot of the cloud, it is advised to set it as lazy loaded.
  347. *
  348. * If config value was previously stored as sensitive or lazy loaded, status cannot be altered without using {@see deleteKey()} first
  349. *
  350. * @param string $app id of the app
  351. * @param string $key config key
  352. * @param array $value config value
  353. * @param bool $sensitive if TRUE value will be hidden when listing config values.
  354. * @param bool $lazy set config as lazy loaded
  355. *
  356. * @return bool TRUE if value was different, therefor updated in database
  357. * @since 29.0.0
  358. * @see IAppConfig for explanation about lazy loading
  359. * @see setValueString()
  360. * @see setValueInt()
  361. * @see setValueFloat()
  362. * @see setValueBool()
  363. */
  364. public function setValueArray(string $app, string $key, array $value, bool $lazy = false, bool $sensitive = false): bool;
  365. /**
  366. * switch sensitive status of a config value
  367. *
  368. * **WARNING:** ignore lazy filtering, all config values are loaded from database
  369. *
  370. * @param string $app id of the app
  371. * @param string $key config key
  372. * @param bool $sensitive TRUE to set as sensitive, FALSE to unset
  373. *
  374. * @return bool TRUE if database update were necessary
  375. * @since 29.0.0
  376. */
  377. public function updateSensitive(string $app, string $key, bool $sensitive): bool;
  378. /**
  379. * switch lazy loading status of a config value
  380. *
  381. * @param string $app id of the app
  382. * @param string $key config key
  383. * @param bool $lazy TRUE to set as lazy loaded, FALSE to unset
  384. *
  385. * @return bool TRUE if database update was necessary
  386. * @since 29.0.0
  387. */
  388. public function updateLazy(string $app, string $key, bool $lazy): bool;
  389. /**
  390. * returns an array contains details about a config value
  391. *
  392. * ```
  393. * [
  394. * "app" => "myapp",
  395. * "key" => "mykey",
  396. * "value" => "its_value",
  397. * "lazy" => false,
  398. * "type" => 4,
  399. * "typeString" => "string",
  400. * 'sensitive' => true
  401. * ]
  402. * ```
  403. *
  404. * @param string $app id of the app
  405. * @param string $key config key
  406. *
  407. * @return array
  408. * @throws AppConfigUnknownKeyException if config key is not known in database
  409. * @since 29.0.0
  410. */
  411. public function getDetails(string $app, string $key): array;
  412. /**
  413. * Convert string like 'string', 'integer', 'float', 'bool' or 'array' to
  414. * to bitflag {@see VALUE_STRING}, {@see VALUE_INT}, {@see VALUE_FLOAT},
  415. * {@see VALUE_BOOL} and {@see VALUE_ARRAY}
  416. *
  417. * @param string $type
  418. *
  419. * @return int
  420. * @since 29.0.0
  421. */
  422. public function convertTypeToInt(string $type): int;
  423. /**
  424. * Convert bitflag {@see VALUE_STRING}, {@see VALUE_INT}, {@see VALUE_FLOAT},
  425. * {@see VALUE_BOOL} and {@see VALUE_ARRAY} to human-readable string
  426. *
  427. * @param int $type
  428. *
  429. * @return string
  430. * @since 29.0.0
  431. */
  432. public function convertTypeToString(int $type): string;
  433. /**
  434. * Delete single config key from database.
  435. *
  436. * @param string $app id of the app
  437. * @param string $key config key
  438. * @since 29.0.0
  439. */
  440. public function deleteKey(string $app, string $key): void;
  441. /**
  442. * delete all config keys linked to an app
  443. *
  444. * @param string $app id of the app
  445. * @since 29.0.0
  446. */
  447. public function deleteApp(string $app): void;
  448. /**
  449. * Clear the cache.
  450. *
  451. * The cache will be rebuilt only the next time a config value is requested.
  452. *
  453. * @param bool $reload set to TRUE to refill cache instantly after clearing it
  454. * @since 29.0.0
  455. */
  456. public function clearCache(bool $reload = false): void;
  457. /**
  458. * get multiply values, either the app or key can be used as wildcard by setting it to false
  459. *
  460. * @param string|false $key
  461. * @param string|false $app
  462. *
  463. * @return array|false
  464. * @since 7.0.0
  465. * @deprecated 29.0.0 Use {@see getAllValues()} or {@see searchValues()}
  466. */
  467. public function getValues($app, $key);
  468. /**
  469. * get all values of the app or and filters out sensitive data
  470. *
  471. * @param string $app
  472. *
  473. * @return array
  474. * @since 12.0.0
  475. * @deprecated 29.0.0 Use {@see getAllValues()} or {@see searchValues()}
  476. */
  477. public function getFilteredValues($app);
  478. }