1
0

IAppConfig.php 17 KB

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