Browse Source

feat(preset): compare userconfig lexicon entries

Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
pull/54520/head
Maxence Lange 3 months ago
parent
commit
b7cdfddeda
  1. 4
      core/Command/Config/Preset.php
  2. 1
      lib/private/AppConfig.php
  3. 45
      lib/private/Config/PresetManager.php
  4. 9
      lib/private/Config/UserConfig.php

4
core/Command/Config/Preset.php

@ -44,10 +44,10 @@ class Preset extends Base {
$list = $this->presetManager->retrieveLexiconPreset();
if ($input->getOption('output') === 'plain') {
$table = new Table($output);
$table->setHeaders(['app', 'config key', 'value', ...array_map(static fn (ConfigLexiconPreset $p): string => $p->name, ConfigLexiconPreset::cases())]);
$table->setHeaders(['app', 'config', 'config key', 'value', ...array_map(static fn (ConfigLexiconPreset $p): string => $p->name, ConfigLexiconPreset::cases())]);
foreach ($list as $appId => $entries) {
foreach ($entries as $item) {
$table->addRow([$appId, $item['entry']['key'], '<comment>' . ($item['value'] ?? '') . '</comment>', ...($item['defaults'] ?? [])]);
$table->addRow([$appId, $item['config'], $item['entry']['key'], '<comment>' . ($item['value'] ?? '') . '</comment>', ...($item['defaults'] ?? [])]);
}
}
$table->render();

1
lib/private/AppConfig.php

@ -1232,6 +1232,7 @@ class AppConfig implements IAppConfig {
*
* @param bool $reload set to TRUE to refill cache instantly after clearing it
*
* @internal
* @since 29.0.0
*/
public function clearCache(bool $reload = false): void {

45
lib/private/Config/PresetManager.php

@ -13,6 +13,7 @@ use OC\AppConfig;
use OC\Installer;
use OCP\App\AppPathNotFoundException;
use OCP\App\IAppManager;
use OCP\Config\IUserConfig;
use OCP\Config\Lexicon\Preset;
use OCP\Exceptions\AppConfigUnknownKeyException;
use OCP\IAppConfig;
@ -65,7 +66,7 @@ class PresetManager {
*
* **Warning** This method MUST be considered resource-needy!
*
* @return array<string, list<array{defaults: array{CLUB: null|string, FAMILY: null|string, LARGE: null|string, MEDIUM: null|string, NONE: null|string, PRIVATE: null|string, SCHOOL: null|string, SHARED: null|string, SMALL: null|string, UNIVERSITY: null|string}, entry: array{definition: string, deprecated: bool, key: string, lazy: bool, note: string, type: 'ARRAY'|'BOOL'|'FLOAT'|'INT'|'MIXED'|'STRING'}, value?: mixed}>>
* @return array<string, list<array{config: string, defaults: array{CLUB: null|string, FAMILY: null|string, LARGE: null|string, MEDIUM: null|string, NONE: null|string, PRIVATE: null|string, SCHOOL: null|string, SHARED: null|string, SMALL: null|string, UNIVERSITY: null|string}, entry: array{definition: string, deprecated: bool, key: string, lazy: bool, note: string, type: 'ARRAY'|'BOOL'|'FLOAT'|'INT'|'MIXED'|'STRING'}, value?: mixed}>>
*/
public function retrieveLexiconPreset(?string $appId = null): array {
if ($appId === null) {
@ -77,17 +78,41 @@ class PresetManager {
return $apps;
}
/** @var AppConfig|null $appConfig */
$appConfig = Server::get(IAppConfig::class);
$lexicon = $appConfig->getConfigDetailsFromLexicon($appId);
return [
$appId => array_merge(
$this->extractLexiconPresetFromConfigClass($appId, 'app', Server::get(IAppConfig::class)),
$this->extractLexiconPresetFromConfigClass($appId, 'user', Server::get(IUserConfig::class))
),
];
}
/**
* @param string $appId
*
* @return list<array{config: string, defaults: array{CLUB: null|string, FAMILY: null|string, LARGE: null|string, MEDIUM: null|string, NONE: null|string, PRIVATE: null|string, SCHOOL: null|string, SHARED: null|string, SMALL: null|string, UNIVERSITY: null|string}, entry: array{definition: string, deprecated: bool, key: string, lazy: bool, note: string, type: 'ARRAY'|'BOOL'|'FLOAT'|'INT'|'MIXED'|'STRING'}, value?: mixed}>
*/
private function extractLexiconPresetFromConfigClass(
string $appId,
string $configType,
AppConfig|UserConfig $config,
): array {
$presets = [];
$lexicon = $config->getConfigDetailsFromLexicon($appId);
foreach ($lexicon['entries'] as $entry) {
$defaults = [];
foreach (Preset::cases() as $case) {
// for each case, we need to use a fresh IAppConfig with clear cache
// cloning to avoid conflict while emulating preset
$newConfig = clone $appConfig;
$newConfig->clearCache(); // needed to ignore cache and rebuild default
$newConfig = clone $config;
if ($newConfig instanceof AppConfig) {
// needed to ignore cache and rebuild default
$newConfig->clearCache();
}
if ($newConfig instanceof UserConfig) {
// in the case of IUserConfig, clear all users' cache
$newConfig->clearCacheAll();
}
$newLexicon = $newConfig->getLexiconEntry($appId, $entry->getKey());
$defaults[$case->name] = $newLexicon?->getDefault($case);
}
@ -99,6 +124,7 @@ class PresetManager {
}
$details = [
'config' => $configType,
'entry' => [
'key' => $entry->getKey(),
'type' => $entry->getValueType()->name,
@ -111,14 +137,17 @@ class PresetManager {
];
try {
$details['value'] = $appConfig->getDetails($appId, $entry->getKey())['value'];
// not interested if a users config value is already set
if ($config instanceof AppConfig) {
$details['value'] = $config->getDetails($appId, $entry->getKey())['value'];
}
} catch (AppConfigUnknownKeyException) {
}
$presets[] = $details;
}
return [$appId => $presets];
return $presets;
}
/**

9
lib/private/Config/UserConfig.php

@ -2000,7 +2000,6 @@ class UserConfig implements IUserConfig {
*
* @return array{entries: array<string, Entry>, aliases: array<string, string>, strictness: Strictness}
* @internal
*
*/
public function getConfigDetailsFromLexicon(string $appId): array {
if (!array_key_exists($appId, $this->configLexiconDetails)) {
@ -2024,7 +2023,13 @@ class UserConfig implements IUserConfig {
return $this->configLexiconDetails[$appId];
}
private function getLexiconEntry(string $appId, string $key): ?Entry {
/**
* get Lexicon Entry using appId and config key entry
*
* @return Entry|null NULL if entry does not exist in user's Lexicon
* @internal
*/
public function getLexiconEntry(string $appId, string $key): ?Entry {
return $this->getConfigDetailsFromLexicon($appId)['entries'][$key] ?? null;
}

Loading…
Cancel
Save