feat(preset): new PresetManager

Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
This commit is contained in:
Maxence Lange 2025-07-29 13:47:23 -01:00
parent 8783679a49
commit 33c4fe504d
5 changed files with 69 additions and 49 deletions

View file

@ -8,10 +8,9 @@ declare(strict_types=1);
*/
namespace OC\Core\Command\Config;
use OC\Config\ConfigManager;
use OC\Config\PresetManager;
use OC\Core\Command\Base;
use OCP\Config\Lexicon\Preset as ConfigLexiconPreset;
use OCP\IConfig;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
@ -19,8 +18,7 @@ use Symfony\Component\Console\Output\OutputInterface;
class Preset extends Base {
public function __construct(
private readonly IConfig $config,
private readonly ConfigManager $configManager,
private readonly PresetManager $presetManager,
) {
parent::__construct();
}
@ -49,10 +47,10 @@ class Preset extends Base {
return self::INVALID;
}
$this->configManager->setLexiconPreset($preset);
$this->presetManager->setLexiconPreset($preset);
}
$current = ConfigLexiconPreset::tryFrom($this->config->getSystemValueInt(ConfigManager::PRESET_CONFIGKEY, 0)) ?? ConfigLexiconPreset::NONE;
$current = $this->presetManager->getLexiconPreset();
$this->writeArrayInOutputFormat($input, $output, [$current->name], 'current preset: ');
return self::SUCCESS;
}

View file

@ -13,6 +13,7 @@ use InvalidArgumentException;
use JsonException;
use OC\AppFramework\Bootstrap\Coordinator;
use OC\Config\ConfigManager;
use OC\Config\PresetManager;
use OCP\Config\Lexicon\Entry;
use OCP\Config\Lexicon\ILexicon;
use OCP\Config\Lexicon\Preset;
@ -66,13 +67,14 @@ class AppConfig implements IAppConfig {
/** @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,
private readonly ConfigManager $configManager,
private readonly PresetManager $presetManager,
protected LoggerInterface $logger,
protected ICrypto $crypto,
) {
@ -520,8 +522,7 @@ class AppConfig implements IAppConfig {
// interested to check options in case a modification of the value is needed
// ie inverting value from previous key when using lexicon option RENAME_INVERT_BOOLEAN
if ($origKey !== $key && $type === self::VALUE_BOOL) {
$configManager = Server::get(ConfigManager::class);
$value = ($configManager->convertToBool($value, $this->getLexiconEntry($app, $key))) ? '1' : '0';
$value = ($this->configManager->convertToBool($value, $this->getLexiconEntry($app, $key))) ? '1' : '0';
}
return $value;
@ -1108,7 +1109,7 @@ class AppConfig implements IAppConfig {
$this->assertParams($app, $key);
try {
$details = $this->getDetails($app, $key);
} catch (AppConfigUnknownKeyException $e) {
} catch (AppConfigUnknownKeyException) {
$details = [
'app' => $app,
'key' => $key
@ -1129,7 +1130,7 @@ class AppConfig implements IAppConfig {
'valueType' => $lexiconEntry->getValueType(),
'valueTypeName' => $lexiconEntry->getValueType()->name,
'sensitive' => $lexiconEntry->isFlagged(self::FLAG_SENSITIVE),
'default' => $lexiconEntry->getDefault($this->getLexiconPreset()),
'default' => $lexiconEntry->getDefault($this->presetManager->getLexiconPreset()),
'definition' => $lexiconEntry->getDefinition(),
'note' => $lexiconEntry->getNote(),
]);
@ -1228,7 +1229,6 @@ class AppConfig implements IAppConfig {
public function clearCache(bool $reload = false): void {
$this->lazyLoaded = $this->fastLoaded = false;
$this->lazyCache = $this->fastCache = $this->valueTypes = $this->configLexiconDetails = [];
$this->configLexiconPreset = null;
if (!$reload) {
return;
@ -1714,7 +1714,7 @@ class AppConfig implements IAppConfig {
$lazy = $lexiconEntry->isLazy();
// only look for default if needed, default from Lexicon got priority
if ($default !== null) {
$default = $lexiconEntry->getDefault($this->getLexiconPreset()) ?? $default;
$default = $lexiconEntry->getDefault($this->presetManager->getLexiconPreset()) ?? $default;
}
if ($lexiconEntry->isFlagged(self::FLAG_SENSITIVE)) {
@ -1802,14 +1802,6 @@ 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
*

View file

@ -27,20 +27,23 @@ use Psr\Log\LoggerInterface;
* @since 32.0.0
*/
class ConfigManager {
/** @since 32.0.0 */
public const PRESET_CONFIGKEY = 'config_preset';
/** @var AppConfig|null $appConfig */
private ?IAppConfig $appConfig = null;
/** @var UserConfig|null $userConfig */
private ?IUserConfig $userConfig = null;
public function __construct(
private readonly IConfig $config,
private readonly LoggerInterface $logger,
) {
}
public function clearConfigCaches(): void {
$this->loadConfigServices();
$this->appConfig->clearCache();
$this->userConfig->clearCacheAll();
}
/**
* Use the rename values from the list of ConfigLexiconEntry defined in each app ConfigLexicon
* to migrate config value to a new config key.
@ -81,17 +84,6 @@ class ConfigManager {
$this->userConfig->ignoreLexiconAliases(false);
}
/**
* store in config.php the new preset
* refresh cached preset
*/
public function setLexiconPreset(Preset $preset): void {
$this->config->setSystemValue(self::PRESET_CONFIGKEY, $preset->value);
$this->loadConfigServices();
$this->appConfig->clearCache();
$this->userConfig->clearCacheAll();
}
/**
* config services cannot be load at __construct() or install will fail
*/

View file

@ -0,0 +1,47 @@
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OC\Config;
use OCP\Config\Lexicon\Preset;
use OCP\IConfig;
/**
* tools to maintains configurations
*
* @since 32.0.0
*/
class PresetManager {
private const PRESET_CONFIGKEY = 'config_preset';
private ?Preset $configLexiconPreset = null;
public function __construct(
private readonly IConfig $config,
private readonly ConfigManager $configManager,
) {
}
/**
* store in config.php the new preset
* refresh cached preset
*/
public function setLexiconPreset(Preset $preset): void {
$this->config->setSystemValue(self::PRESET_CONFIGKEY, $preset->value);
$this->configLexiconPreset = $preset;
$this->configManager->clearConfigCaches();
}
public function getLexiconPreset(): Preset {
if ($this->configLexiconPreset === null) {
$this->configLexiconPreset = Preset::tryFrom($this->config->getSystemValueInt(self::PRESET_CONFIGKEY, 0)) ?? Preset::NONE;
}
return $this->configLexiconPreset;
}
}

View file

@ -68,11 +68,12 @@ class UserConfig implements IUserConfig {
/** @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;
public function __construct(
protected IDBConnection $connection,
protected IConfig $config,
private readonly ConfigManager $configManager,
private readonly PresetManager $presetManager,
protected LoggerInterface $logger,
protected ICrypto $crypto,
) {
@ -772,8 +773,7 @@ class UserConfig implements IUserConfig {
// interested to check options in case a modification of the value is needed
// ie inverting value from previous key when using lexicon option RENAME_INVERT_BOOLEAN
if ($origKey !== $key && $type === ValueType::BOOL) {
$configManager = Server::get(ConfigManager::class);
$value = ($configManager->convertToBool($value, $this->getLexiconEntry($app, $key))) ? '1' : '0';
$value = ($this->configManager->convertToBool($value, $this->getLexiconEntry($app, $key))) ? '1' : '0';
}
return $value;
@ -1636,7 +1636,6 @@ class UserConfig implements IUserConfig {
public function clearCacheAll(): void {
$this->lazyLoaded = $this->fastLoaded = [];
$this->lazyCache = $this->fastCache = $this->valueDetails = $this->configLexiconDetails = [];
$this->configLexiconPreset = null;
}
/**
@ -1937,7 +1936,7 @@ class UserConfig implements IUserConfig {
// only look for default if needed, default from Lexicon got priority if not overwritten by admin
if ($default !== null) {
$default = $this->getSystemDefault($app, $configValue) ?? $configValue->getDefault($this->getLexiconPreset()) ?? $default;
$default = $this->getSystemDefault($app, $configValue) ?? $configValue->getDefault($this->presetManager->getLexiconPreset()) ?? $default;
}
// returning false will make get() returning $default and set() not changing value in database
@ -2039,12 +2038,4 @@ class UserConfig implements IUserConfig {
public function ignoreLexiconAliases(bool $ignore): void {
$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;
}
}