1
0

IAppConfig.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  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|int|float|bool|array> [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. * @param int|null $typedAs enforce type for the returned values {@see self::VALUE_STRING} and others
  145. *
  146. * @return array<string, string|int|float|bool|array> [appId => configValue]
  147. * @since 29.0.0
  148. */
  149. public function searchValues(string $key, bool $lazy = false, ?int $typedAs = null): array;
  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 string $default default value
  158. * @param bool $lazy search within lazy loaded config
  159. *
  160. * @return string 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 getValueInt()
  164. * @see getValueFloat()
  165. * @see getValueBool()
  166. * @see getValueArray()
  167. */
  168. public function getValueString(string $app, string $key, string $default = '', bool $lazy = false): string;
  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 int $default default value
  177. * @param bool $lazy search within lazy loaded config
  178. *
  179. * @return int 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 getValueFloat()
  184. * @see getValueBool()
  185. * @see getValueArray()
  186. */
  187. public function getValueInt(string $app, string $key, int $default = 0, bool $lazy = false): int;
  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 float $default default value
  196. * @param bool $lazy search within lazy loaded config
  197. *
  198. * @return float 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 getValueBool()
  204. * @see getValueArray()
  205. */
  206. public function getValueFloat(string $app, string $key, float $default = 0, bool $lazy = false): float;
  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 bool $default default value
  215. * @param bool $lazy search within lazy loaded config
  216. *
  217. * @return bool 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 getValueArray()
  224. */
  225. public function getValueBool(string $app, string $key, bool $default = false, bool $lazy = false): bool;
  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 $app id of the app
  232. * @param string $key config key
  233. * @param array $default default value
  234. * @param bool $lazy search within lazy loaded config
  235. *
  236. * @return array stored config value or $default if not set in database
  237. * @since 29.0.0
  238. * @see IAppConfig for explanation about lazy loading
  239. * @see getValueString()
  240. * @see getValueInt()
  241. * @see getValueFloat()
  242. * @see getValueBool()
  243. */
  244. public function getValueArray(string $app, string $key, array $default = [], bool $lazy = false): array;
  245. /**
  246. * returns the type of config value
  247. *
  248. * **WARNING:** ignore lazy filtering, all config values are loaded from database
  249. * unless lazy is set to false
  250. *
  251. * @param string $app id of the app
  252. * @param string $key config key
  253. * @param bool|null $lazy
  254. *
  255. * @return int
  256. * @throws AppConfigUnknownKeyException
  257. * @since 29.0.0
  258. * @see VALUE_STRING
  259. * @see VALUE_INT
  260. * @see VALUE_FLOAT
  261. * @see VALUE_BOOL
  262. * @see VALUE_ARRAY
  263. */
  264. public function getValueType(string $app, string $key, ?bool $lazy = null): int;
  265. /**
  266. * Store a config key and its value in database
  267. *
  268. * If config key is already known with the exact same config value, the database is not updated.
  269. * If config key is not supposed to be read during the boot of the cloud, it is advised to set it as lazy loaded.
  270. *
  271. * If config value was previously stored as sensitive or lazy loaded, status cannot be altered without using {@see deleteKey()} first
  272. *
  273. * @param string $app id of the app
  274. * @param string $key config key
  275. * @param string $value config value
  276. * @param bool $sensitive if TRUE value will be hidden when listing config values.
  277. * @param bool $lazy set config as lazy loaded
  278. *
  279. * @return bool TRUE if value was different, therefor updated in database
  280. * @since 29.0.0
  281. * @see IAppConfig for explanation about lazy loading
  282. * @see setValueInt()
  283. * @see setValueFloat()
  284. * @see setValueBool()
  285. * @see setValueArray()
  286. */
  287. public function setValueString(string $app, string $key, string $value, bool $lazy = false, bool $sensitive = false): bool;
  288. /**
  289. * Store a config key and its value in database
  290. *
  291. * When handling huge value around and/or above 2,147,483,647, a debug log will be generated
  292. * on 64bits system, as php int type reach its limit (and throw an exception) on 32bits when using huge numbers.
  293. *
  294. * When using huge numbers, it is advised to use {@see \OCP\Util::numericToNumber()} and {@see setValueString()}
  295. *
  296. * If config key is already known with the exact same config value, the database is not updated.
  297. * If config key is not supposed to be read during the boot of the cloud, it is advised to set it as lazy loaded.
  298. *
  299. * If config value was previously stored as sensitive or lazy loaded, status cannot be altered without using {@see deleteKey()} first
  300. *
  301. * @param string $app id of the app
  302. * @param string $key config key
  303. * @param int $value config value
  304. * @param bool $sensitive if TRUE value will be hidden when listing config values.
  305. * @param bool $lazy set config as lazy loaded
  306. *
  307. * @return bool TRUE if value was different, therefor updated in database
  308. * @since 29.0.0
  309. * @see IAppConfig for explanation about lazy loading
  310. * @see setValueString()
  311. * @see setValueFloat()
  312. * @see setValueBool()
  313. * @see setValueArray()
  314. */
  315. public function setValueInt(string $app, string $key, int $value, bool $lazy = false, bool $sensitive = false): bool;
  316. /**
  317. * Store a config key and its value in database.
  318. *
  319. * If config key is already known with the exact same config value, the database is not updated.
  320. * If config key is not supposed to be read during the boot of the cloud, it is advised to set it as lazy loaded.
  321. *
  322. * If config value was previously stored as sensitive or lazy loaded, status cannot be altered without using {@see deleteKey()} first
  323. *
  324. * @param string $app id of the app
  325. * @param string $key config key
  326. * @param float $value config value
  327. * @param bool $sensitive if TRUE value will be hidden when listing config values.
  328. * @param bool $lazy set config as lazy loaded
  329. *
  330. * @return bool TRUE if value was different, therefor updated in database
  331. * @since 29.0.0
  332. * @see IAppConfig for explanation about lazy loading
  333. * @see setValueString()
  334. * @see setValueInt()
  335. * @see setValueBool()
  336. * @see setValueArray()
  337. */
  338. public function setValueFloat(string $app, string $key, float $value, bool $lazy = false, bool $sensitive = false): bool;
  339. /**
  340. * Store a config key and its value in database
  341. *
  342. * If config key is already known with the exact same config value, the database is not updated.
  343. * If config key is not supposed to be read during the boot of the cloud, it is advised to set it as lazy loaded.
  344. *
  345. * If config value was previously stored as lazy loaded, status cannot be altered without using {@see deleteKey()} first
  346. *
  347. * @param string $app id of the app
  348. * @param string $key config key
  349. * @param bool $value config value
  350. * @param bool $lazy set config as lazy loaded
  351. *
  352. * @return bool TRUE if value was different, therefor updated in database
  353. * @since 29.0.0
  354. * @see IAppConfig for explanation about lazy loading
  355. * @see setValueString()
  356. * @see setValueInt()
  357. * @see setValueFloat()
  358. * @see setValueArray()
  359. */
  360. public function setValueBool(string $app, string $key, bool $value, bool $lazy = false): bool;
  361. /**
  362. * Store a config key and its value in database
  363. *
  364. * If config key is already known with the exact same config value, the database is not updated.
  365. * If config key is not supposed to be read during the boot of the cloud, it is advised to set it as lazy loaded.
  366. *
  367. * If config value was previously stored as sensitive or lazy loaded, status cannot be altered without using {@see deleteKey()} first
  368. *
  369. * @param string $app id of the app
  370. * @param string $key config key
  371. * @param array $value config value
  372. * @param bool $sensitive if TRUE value will be hidden when listing config values.
  373. * @param bool $lazy set config as lazy loaded
  374. *
  375. * @return bool TRUE if value was different, therefor updated in database
  376. * @since 29.0.0
  377. * @see IAppConfig for explanation about lazy loading
  378. * @see setValueString()
  379. * @see setValueInt()
  380. * @see setValueFloat()
  381. * @see setValueBool()
  382. */
  383. public function setValueArray(string $app, string $key, array $value, bool $lazy = false, bool $sensitive = false): bool;
  384. /**
  385. * switch sensitive status of a config value
  386. *
  387. * **WARNING:** ignore lazy filtering, all config values are loaded from database
  388. *
  389. * @param string $app id of the app
  390. * @param string $key config key
  391. * @param bool $sensitive TRUE to set as sensitive, FALSE to unset
  392. *
  393. * @return bool TRUE if database update were necessary
  394. * @since 29.0.0
  395. */
  396. public function updateSensitive(string $app, string $key, bool $sensitive): bool;
  397. /**
  398. * switch lazy loading status of a config value
  399. *
  400. * @param string $app id of the app
  401. * @param string $key config key
  402. * @param bool $lazy TRUE to set as lazy loaded, FALSE to unset
  403. *
  404. * @return bool TRUE if database update was necessary
  405. * @since 29.0.0
  406. */
  407. public function updateLazy(string $app, string $key, bool $lazy): bool;
  408. /**
  409. * returns an array contains details about a config value
  410. *
  411. * ```
  412. * [
  413. * "app" => "myapp",
  414. * "key" => "mykey",
  415. * "value" => "its_value",
  416. * "lazy" => false,
  417. * "type" => 4,
  418. * "typeString" => "string",
  419. * 'sensitive' => true
  420. * ]
  421. * ```
  422. *
  423. * @param string $app id of the app
  424. * @param string $key config key
  425. *
  426. * @return array
  427. * @throws AppConfigUnknownKeyException if config key is not known in database
  428. * @since 29.0.0
  429. */
  430. public function getDetails(string $app, string $key): array;
  431. /**
  432. * Convert string like 'string', 'integer', 'float', 'bool' or 'array' to
  433. * to bitflag {@see VALUE_STRING}, {@see VALUE_INT}, {@see VALUE_FLOAT},
  434. * {@see VALUE_BOOL} and {@see VALUE_ARRAY}
  435. *
  436. * @param string $type
  437. *
  438. * @return int
  439. * @since 29.0.0
  440. */
  441. public function convertTypeToInt(string $type): int;
  442. /**
  443. * Convert bitflag {@see VALUE_STRING}, {@see VALUE_INT}, {@see VALUE_FLOAT},
  444. * {@see VALUE_BOOL} and {@see VALUE_ARRAY} to human-readable string
  445. *
  446. * @param int $type
  447. *
  448. * @return string
  449. * @since 29.0.0
  450. */
  451. public function convertTypeToString(int $type): string;
  452. /**
  453. * Delete single config key from database.
  454. *
  455. * @param string $app id of the app
  456. * @param string $key config key
  457. * @since 29.0.0
  458. */
  459. public function deleteKey(string $app, string $key): void;
  460. /**
  461. * delete all config keys linked to an app
  462. *
  463. * @param string $app id of the app
  464. * @since 29.0.0
  465. */
  466. public function deleteApp(string $app): void;
  467. /**
  468. * Clear the cache.
  469. *
  470. * The cache will be rebuilt only the next time a config value is requested.
  471. *
  472. * @param bool $reload set to TRUE to refill cache instantly after clearing it
  473. * @since 29.0.0
  474. */
  475. public function clearCache(bool $reload = false): void;
  476. /**
  477. * get multiply values, either the app or key can be used as wildcard by setting it to false
  478. *
  479. * @param string|false $key
  480. * @param string|false $app
  481. *
  482. * @return array|false
  483. * @since 7.0.0
  484. * @deprecated 29.0.0 Use {@see getAllValues()} or {@see searchValues()}
  485. */
  486. public function getValues($app, $key);
  487. /**
  488. * get all values of the app or and filters out sensitive data
  489. *
  490. * @param string $app
  491. *
  492. * @return array
  493. * @since 12.0.0
  494. * @deprecated 29.0.0 Use {@see getAllValues()} or {@see searchValues()}
  495. */
  496. public function getFilteredValues($app);
  497. }