AppConfig.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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 OC\AppFramework\Services;
  8. use InvalidArgumentException;
  9. use JsonException;
  10. use OCP\AppFramework\Services\IAppConfig;
  11. use OCP\Exceptions\AppConfigTypeConflictException;
  12. use OCP\Exceptions\AppConfigUnknownKeyException;
  13. use OCP\IConfig;
  14. class AppConfig implements IAppConfig {
  15. public function __construct(
  16. private IConfig $config,
  17. /** @var \OC\AppConfig */
  18. private \OCP\IAppConfig $appConfig,
  19. private string $appName,
  20. ) {
  21. }
  22. /**
  23. * @inheritDoc
  24. *
  25. * @return string[] list of stored config keys
  26. * @since 20.0.0
  27. */
  28. public function getAppKeys(): array {
  29. return $this->appConfig->getKeys($this->appName);
  30. }
  31. /**
  32. * @inheritDoc
  33. *
  34. * @param string $key config key
  35. * @param bool|null $lazy TRUE to search within lazy loaded config, NULL to search within all config
  36. *
  37. * @return bool TRUE if key exists
  38. * @since 29.0.0
  39. */
  40. public function hasAppKey(string $key, ?bool $lazy = false): bool {
  41. return $this->appConfig->hasKey($this->appName, $key, $lazy);
  42. }
  43. /**
  44. * @param string $key config key
  45. * @param bool|null $lazy TRUE to search within lazy loaded config, NULL to search within all config
  46. *
  47. * @return bool
  48. * @throws AppConfigUnknownKeyException if config key is not known
  49. * @since 29.0.0
  50. */
  51. public function isSensitive(string $key, ?bool $lazy = false): bool {
  52. return $this->appConfig->isSensitive($this->appName, $key, $lazy);
  53. }
  54. /**
  55. * @inheritDoc
  56. *
  57. * @param string $key config key
  58. *
  59. * @return bool TRUE if config is lazy loaded
  60. * @throws AppConfigUnknownKeyException if config key is not known
  61. * @see \OCP\IAppConfig for details about lazy loading
  62. * @since 29.0.0
  63. */
  64. public function isLazy(string $key): bool {
  65. return $this->appConfig->isLazy($this->appName, $key);
  66. }
  67. /**
  68. * @inheritDoc
  69. *
  70. * @param string $key config keys prefix to search
  71. * @param bool $filtered TRUE to hide sensitive config values. Value are replaced by {@see IConfig::SENSITIVE_VALUE}
  72. *
  73. * @return array<string, string|int|float|bool|array> [configKey => configValue]
  74. * @since 29.0.0
  75. */
  76. public function getAllAppValues(string $key = '', bool $filtered = false): array {
  77. return $this->appConfig->getAllValues($this->appName, $key, $filtered);
  78. }
  79. /**
  80. * @inheritDoc
  81. *
  82. * @param string $key the key of the value, under which will be saved
  83. * @param string $value the value that should be stored
  84. * @since 20.0.0
  85. * @deprecated 29.0.0 use {@see setAppValueString()}
  86. */
  87. public function setAppValue(string $key, string $value): void {
  88. /** @psalm-suppress InternalMethod */
  89. $this->appConfig->setValueMixed($this->appName, $key, $value);
  90. }
  91. /**
  92. * @inheritDoc
  93. *
  94. * @param string $key config key
  95. * @param string $value config value
  96. * @param bool $lazy set config as lazy loaded
  97. * @param bool $sensitive if TRUE value will be hidden when listing config values.
  98. *
  99. * @return bool TRUE if value was different, therefor updated in database
  100. * @throws AppConfigTypeConflictException if type from database is not VALUE_MIXED and different from the requested one
  101. * @since 29.0.0
  102. * @see \OCP\IAppConfig for explanation about lazy loading
  103. */
  104. public function setAppValueString(
  105. string $key,
  106. string $value,
  107. bool $lazy = false,
  108. bool $sensitive = false,
  109. ): bool {
  110. return $this->appConfig->setValueString($this->appName, $key, $value, $lazy, $sensitive);
  111. }
  112. /**
  113. * @inheritDoc
  114. *
  115. * @param string $key config key
  116. * @param int $value config value
  117. * @param bool $lazy set config as lazy loaded
  118. * @param bool $sensitive if TRUE value will be hidden when listing config values.
  119. *
  120. * @return bool TRUE if value was different, therefor updated in database
  121. * @throws AppConfigTypeConflictException if type from database is not VALUE_MIXED and different from the requested one
  122. * @since 29.0.0
  123. * @see \OCP\IAppConfig for explanation about lazy loading
  124. */
  125. public function setAppValueInt(
  126. string $key,
  127. int $value,
  128. bool $lazy = false,
  129. bool $sensitive = false,
  130. ): bool {
  131. return $this->appConfig->setValueInt($this->appName, $key, $value, $lazy, $sensitive);
  132. }
  133. /**
  134. * @inheritDoc
  135. *
  136. * @param string $key config key
  137. * @param float $value config value
  138. * @param bool $lazy set config as lazy loaded
  139. * @param bool $sensitive if TRUE value will be hidden when listing config values.
  140. *
  141. * @return bool TRUE if value was different, therefor updated in database
  142. * @throws AppConfigTypeConflictException if type from database is not VALUE_MIXED and different from the requested one
  143. * @since 29.0.0
  144. * @see \OCP\IAppConfig for explanation about lazy loading
  145. */
  146. public function setAppValueFloat(
  147. string $key,
  148. float $value,
  149. bool $lazy = false,
  150. bool $sensitive = false,
  151. ): bool {
  152. return $this->appConfig->setValueFloat($this->appName, $key, $value, $lazy, $sensitive);
  153. }
  154. /**
  155. * @inheritDoc
  156. *
  157. * @param string $key config key
  158. * @param bool $value config value
  159. * @param bool $lazy set config as lazy loaded
  160. *
  161. * @return bool TRUE if value was different, therefor updated in database
  162. * @throws AppConfigTypeConflictException if type from database is not VALUE_MIXED and different from the requested one
  163. * @since 29.0.0
  164. * @see \OCP\IAppConfig for explanation about lazy loading
  165. */
  166. public function setAppValueBool(
  167. string $key,
  168. bool $value,
  169. bool $lazy = false,
  170. ): bool {
  171. return $this->appConfig->setValueBool($this->appName, $key, $value, $lazy);
  172. }
  173. /**
  174. * @inheritDoc
  175. *
  176. * @param string $key config key
  177. * @param array $value config value
  178. * @param bool $lazy set config as lazy loaded
  179. * @param bool $sensitive if TRUE value will be hidden when listing config values.
  180. *
  181. * @return bool TRUE if value was different, therefor updated in database
  182. * @throws AppConfigTypeConflictException if type from database is not VALUE_MIXED and different from the requested one
  183. * @throws JsonException
  184. * @since 29.0.0
  185. * @see \OCP\IAppConfig for explanation about lazy loading
  186. */
  187. public function setAppValueArray(
  188. string $key,
  189. array $value,
  190. bool $lazy = false,
  191. bool $sensitive = false,
  192. ): bool {
  193. return $this->appConfig->setValueArray($this->appName, $key, $value, $lazy, $sensitive);
  194. }
  195. /**
  196. * @param string $key
  197. * @param string $default
  198. *
  199. * @since 20.0.0
  200. * @deprecated 29.0.0 use {@see getAppValueString()}
  201. * @return string
  202. */
  203. public function getAppValue(string $key, string $default = ''): string {
  204. /** @psalm-suppress InternalMethod */
  205. /** @psalm-suppress UndefinedInterfaceMethod */
  206. return $this->appConfig->getValueMixed($this->appName, $key, $default);
  207. }
  208. /**
  209. * @inheritDoc
  210. *
  211. * @param string $key config key
  212. * @param string $default default value
  213. * @param bool $lazy search within lazy loaded config
  214. *
  215. * @return string stored config value or $default if not set in database
  216. * @throws InvalidArgumentException if one of the argument format is invalid
  217. * @throws AppConfigTypeConflictException in case of conflict with the value type set in database
  218. * @since 29.0.0
  219. * @see \OCP\IAppConfig for explanation about lazy loading
  220. */
  221. public function getAppValueString(string $key, string $default = '', bool $lazy = false): string {
  222. return $this->appConfig->getValueString($this->appName, $key, $default, $lazy);
  223. }
  224. /**
  225. * @inheritDoc
  226. *
  227. * @param string $key config key
  228. * @param int $default default value
  229. * @param bool $lazy search within lazy loaded config
  230. *
  231. * @return int stored config value or $default if not set in database
  232. * @throws InvalidArgumentException if one of the argument format is invalid
  233. * @throws AppConfigTypeConflictException in case of conflict with the value type set in database
  234. * @since 29.0.0
  235. * @see \OCP\IAppConfig for explanation about lazy loading
  236. */
  237. public function getAppValueInt(string $key, int $default = 0, bool $lazy = false): int {
  238. return $this->appConfig->getValueInt($this->appName, $key, $default, $lazy);
  239. }
  240. /**
  241. * @inheritDoc
  242. *
  243. * @param string $key config key
  244. * @param float $default default value
  245. * @param bool $lazy search within lazy loaded config
  246. *
  247. * @return float stored config value or $default if not set in database
  248. * @throws InvalidArgumentException if one of the argument format is invalid
  249. * @throws AppConfigTypeConflictException in case of conflict with the value type set in database
  250. * @since 29.0.0
  251. * @see \OCP\IAppConfig for explanation about lazy loading
  252. */
  253. public function getAppValueFloat(string $key, float $default = 0, bool $lazy = false): float {
  254. return $this->appConfig->getValueFloat($this->appName, $key, $default, $lazy);
  255. }
  256. /**
  257. * @inheritDoc
  258. *
  259. * @param string $key config key
  260. * @param bool $default default value
  261. * @param bool $lazy search within lazy loaded config
  262. *
  263. * @return bool stored config value or $default if not set in database
  264. * @throws InvalidArgumentException if one of the argument format is invalid
  265. * @throws AppConfigTypeConflictException in case of conflict with the value type set in database
  266. * @since 29.0.0
  267. * @see \OCP\IAppConfig for explanation about lazy loading
  268. */
  269. public function getAppValueBool(string $key, bool $default = false, bool $lazy = false): bool {
  270. return $this->appConfig->getValueBool($this->appName, $key, $default, $lazy);
  271. }
  272. /**
  273. * @inheritDoc
  274. *
  275. * @param string $key config key
  276. * @param array $default default value
  277. * @param bool $lazy search within lazy loaded config
  278. *
  279. * @return array stored config value or $default if not set in database
  280. * @throws InvalidArgumentException if one of the argument format is invalid
  281. * @throws AppConfigTypeConflictException in case of conflict with the value type set in database
  282. * @since 29.0.0
  283. * @see \OCP\IAppConfig for explanation about lazy loading
  284. */
  285. public function getAppValueArray(string $key, array $default = [], bool $lazy = false): array {
  286. return $this->appConfig->getValueArray($this->appName, $key, $default, $lazy);
  287. }
  288. /**
  289. * @inheritDoc
  290. *
  291. * @param string $key the key of the value, under which it was saved
  292. * @since 20.0.0
  293. */
  294. public function deleteAppValue(string $key): void {
  295. $this->appConfig->deleteKey($this->appName, $key);
  296. }
  297. /**
  298. * @inheritDoc
  299. *
  300. * @since 20.0.0
  301. */
  302. public function deleteAppValues(): void {
  303. $this->appConfig->deleteApp($this->appName);
  304. }
  305. public function setUserValue(string $userId, string $key, string $value, ?string $preCondition = null): void {
  306. $this->config->setUserValue($userId, $this->appName, $key, $value, $preCondition);
  307. }
  308. public function getUserValue(string $userId, string $key, string $default = ''): string {
  309. return $this->config->getUserValue($userId, $this->appName, $key, $default);
  310. }
  311. public function deleteUserValue(string $userId, string $key): void {
  312. $this->config->deleteUserValue($userId, $this->appName, $key);
  313. }
  314. }