diff options
Diffstat (limited to 'lib/private/AppConfig.php')
-rw-r--r-- | lib/private/AppConfig.php | 147 |
1 files changed, 121 insertions, 26 deletions
diff --git a/lib/private/AppConfig.php b/lib/private/AppConfig.php index 476adbb93e0..2280ac1a79f 100644 --- a/lib/private/AppConfig.php +++ b/lib/private/AppConfig.php @@ -11,11 +11,13 @@ namespace OC; use InvalidArgumentException; use JsonException; -use NCU\Config\Lexicon\ConfigLexiconEntry; -use NCU\Config\Lexicon\ConfigLexiconStrictness; -use NCU\Config\Lexicon\IConfigLexicon; use OC\AppFramework\Bootstrap\Coordinator; use OC\Config\ConfigManager; +use OCP\Config\Lexicon\Entry; +use OCP\Config\Lexicon\ILexicon; +use OCP\Config\Lexicon\Preset; +use OCP\Config\Lexicon\Strictness; +use OCP\Config\ValueType; use OCP\DB\Exception as DBException; use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\Exceptions\AppConfigIncorrectTypeException; @@ -61,15 +63,16 @@ class AppConfig implements IAppConfig { private array $valueTypes = []; // type for all config values private bool $fastLoaded = false; private bool $lazyLoaded = false; - /** @var array<string, array{entries: array<string, ConfigLexiconEntry>, aliases: array<string, string>, strictness: ConfigLexiconStrictness}> ['app_id' => ['strictness' => ConfigLexiconStrictness, 'entries' => ['config_key' => ConfigLexiconEntry[]]] */ + /** @var array<string, array{entries: array<string, Entry>, aliases: array<string, string>, strictness: Strictness}> ['app_id' => ['strictness' => ConfigLexiconStrictness, 'entries' => ['config_key' => ConfigLexiconEntry[]]] */ private array $configLexiconDetails = []; private bool $ignoreLexiconAliases = false; - + private ?Preset $configLexiconPreset = null; /** @var ?array<string, string> */ private ?array $appVersionsCache = null; public function __construct( protected IDBConnection $connection, + protected IConfig $config, protected LoggerInterface $logger, protected ICrypto $crypto, ) { @@ -93,8 +96,9 @@ class AppConfig implements IAppConfig { * @inheritDoc * * @param string $app id of the app - * * @return list<string> list of stored config keys + * @see searchKeys to not load lazy config keys + * * @since 29.0.0 */ public function getKeys(string $app): array { @@ -110,6 +114,32 @@ class AppConfig implements IAppConfig { * @inheritDoc * * @param string $app id of the app + * @param string $prefix returns only keys starting with this value + * @param bool $lazy TRUE to search in lazy config keys + * @return list<string> list of stored config keys + * @since 32.0.0 + */ + public function searchKeys(string $app, string $prefix = '', bool $lazy = false): array { + $this->assertParams($app); + $this->loadConfig($app, $lazy); + if ($lazy) { + $keys = array_keys($this->lazyCache[$app] ?? []); + } else { + $keys = array_keys($this->fastCache[$app] ?? []); + } + + if ($prefix !== '') { + $keys = array_filter($keys, static fn (string $key): bool => str_starts_with($key, $prefix)); + } + + sort($keys); + return array_values(array_unique($keys)); + } + + /** + * @inheritDoc + * + * @param string $app id of the app * @param string $key config key * @param bool|null $lazy TRUE to search within lazy loaded config, NULL to search within all config * @@ -438,9 +468,17 @@ class AppConfig implements IAppConfig { ): string { $this->assertParams($app, $key, valueType: $type); $origKey = $key; - if (!$this->matchAndApplyLexiconDefinition($app, $key, $lazy, $type, $default)) { - return $default; // returns default if strictness of lexicon is set to WARNING (block and report) + $matched = $this->matchAndApplyLexiconDefinition($app, $key, $lazy, $type, $default); + if ($default === null) { + // there is no logical reason for it to be null + throw new \Exception('default cannot be null'); + } + + // returns default if strictness of lexicon is set to WARNING (block and report) + if (!$matched) { + return $default; } + $this->loadConfig($app, $lazy); /** @@ -1058,6 +1096,49 @@ class AppConfig implements IAppConfig { } /** + * @inheritDoc + * + * @param string $app id of the app + * @param string $key config key + * + * @return array{app: string, key: string, lazy?: bool, valueType?: ValueType, valueTypeName?: string, sensitive?: bool, default?: string, definition?: string, note?: string} + * @since 32.0.0 + */ + public function getKeyDetails(string $app, string $key): array { + $this->assertParams($app, $key); + try { + $details = $this->getDetails($app, $key); + } catch (AppConfigUnknownKeyException $e) { + $details = [ + 'app' => $app, + 'key' => $key + ]; + } + + /** @var Entry $lexiconEntry */ + try { + $lazy = false; + $this->matchAndApplyLexiconDefinition($app, $key, $lazy, lexiconEntry: $lexiconEntry); + } catch (AppConfigTypeConflictException|AppConfigUnknownKeyException) { + // can be ignored + } + + if ($lexiconEntry !== null) { + $details = array_merge($details, [ + 'lazy' => $lexiconEntry->isLazy(), + 'valueType' => $lexiconEntry->getValueType(), + 'valueTypeName' => $lexiconEntry->getValueType()->name, + 'sensitive' => $lexiconEntry->isFlagged(self::FLAG_SENSITIVE), + 'default' => $lexiconEntry->getDefault($this->getLexiconPreset()), + 'definition' => $lexiconEntry->getDefinition(), + 'note' => $lexiconEntry->getNote(), + ]); + } + + return array_filter($details); + } + + /** * @param string $type * * @return int @@ -1146,7 +1227,8 @@ class AppConfig implements IAppConfig { */ public function clearCache(bool $reload = false): void { $this->lazyLoaded = $this->fastLoaded = false; - $this->lazyCache = $this->fastCache = $this->valueTypes = []; + $this->lazyCache = $this->fastCache = $this->valueTypes = $this->configLexiconDetails = []; + $this->configLexiconPreset = null; if (!$reload) { return; @@ -1592,7 +1674,8 @@ class AppConfig implements IAppConfig { string &$key, ?bool &$lazy = null, int &$type = self::VALUE_MIXED, - string &$default = '', + ?string &$default = null, + ?Entry &$lexiconEntry = null, ): bool { if (in_array($key, [ @@ -1617,23 +1700,27 @@ class AppConfig implements IAppConfig { return true; } - /** @var ConfigLexiconEntry $configValue */ - $configValue = $configDetails['entries'][$key]; + /** @var Entry $lexiconEntry */ + $lexiconEntry = $configDetails['entries'][$key]; $type &= ~self::VALUE_SENSITIVE; - $appConfigValueType = $configValue->getValueType()->toAppConfigFlag(); + $appConfigValueType = $lexiconEntry->getValueType()->toAppConfigFlag(); if ($type === self::VALUE_MIXED) { $type = $appConfigValueType; // we overwrite if value was requested as mixed } elseif ($appConfigValueType !== $type) { throw new AppConfigTypeConflictException('The app config key ' . $app . '/' . $key . ' is typed incorrectly in relation to the config lexicon'); } - $lazy = $configValue->isLazy(); - $default = $configValue->getDefault() ?? $default; // default from Lexicon got priority - if ($configValue->isFlagged(self::FLAG_SENSITIVE)) { + $lazy = $lexiconEntry->isLazy(); + // only look for default if needed, default from Lexicon got priority + if ($default !== null) { + $default = $lexiconEntry->getDefault($this->getLexiconPreset()) ?? $default; + } + + if ($lexiconEntry->isFlagged(self::FLAG_SENSITIVE)) { $type |= self::VALUE_SENSITIVE; } - if ($configValue->isDeprecated()) { + if ($lexiconEntry->isDeprecated()) { $this->logger->notice('App config key ' . $app . '/' . $key . ' is set as deprecated.'); } @@ -1643,15 +1730,15 @@ class AppConfig implements IAppConfig { /** * manage ConfigLexicon behavior based on strictness set in IConfigLexicon * - * @param ConfigLexiconStrictness|null $strictness + * @param Strictness|null $strictness * @param string $line * * @return bool TRUE if conflict can be fully ignored, FALSE if action should be not performed * @throws AppConfigUnknownKeyException if strictness implies exception - * @see IConfigLexicon::getStrictness() + * @see ILexicon::getStrictness() */ private function applyLexiconStrictness( - ?ConfigLexiconStrictness $strictness, + ?Strictness $strictness, string $line = '', ): bool { if ($strictness === null) { @@ -1659,12 +1746,12 @@ class AppConfig implements IAppConfig { } switch ($strictness) { - case ConfigLexiconStrictness::IGNORE: + case Strictness::IGNORE: return true; - case ConfigLexiconStrictness::NOTICE: + case Strictness::NOTICE: $this->logger->notice($line); return true; - case ConfigLexiconStrictness::WARNING: + case Strictness::WARNING: $this->logger->warning($line); return false; } @@ -1678,7 +1765,7 @@ class AppConfig implements IAppConfig { * @param string $appId * @internal * - * @return array{entries: array<string, ConfigLexiconEntry>, aliases: array<string, string>, strictness: ConfigLexiconStrictness} + * @return array{entries: array<string, Entry>, aliases: array<string, string>, strictness: Strictness} */ public function getConfigDetailsFromLexicon(string $appId): array { if (!array_key_exists($appId, $this->configLexiconDetails)) { @@ -1695,14 +1782,14 @@ class AppConfig implements IAppConfig { $this->configLexiconDetails[$appId] = [ 'entries' => $entries, 'aliases' => $aliases, - 'strictness' => $configLexicon?->getStrictness() ?? ConfigLexiconStrictness::IGNORE + 'strictness' => $configLexicon?->getStrictness() ?? Strictness::IGNORE ]; } return $this->configLexiconDetails[$appId]; } - private function getLexiconEntry(string $appId, string $key): ?ConfigLexiconEntry { + private function getLexiconEntry(string $appId, string $key): ?Entry { return $this->getConfigDetailsFromLexicon($appId)['entries'][$key] ?? null; } @@ -1715,6 +1802,14 @@ class AppConfig implements IAppConfig { $this->ignoreLexiconAliases = $ignore; } + private function getLexiconPreset(): Preset { + if ($this->configLexiconPreset === null) { + $this->configLexiconPreset = Preset::tryFrom($this->config->getSystemValueInt(ConfigManager::PRESET_CONFIGKEY, 0)) ?? Preset::NONE; + } + + return $this->configLexiconPreset; + } + /** * Returns the installed versions of all apps * |