Private GetConfig and WriteConfig by providing generic set() method

This is the minimum required change that keeps the current behaviour to
read the configuration file from disk before setting a value, and writes
it back immediately after that.

A second new method, setMultiple(), allows to set multiple values at
once avoiding read/write-cycles in between.

Signed-off-by: Pablo Zmdl <pablo@nextcloud.com>
This commit is contained in:
Pablo Zmdl 2026-01-07 16:16:20 +01:00
parent e8d1bce4f6
commit 7159bc68d3
3 changed files with 22 additions and 15 deletions

View file

@ -124,14 +124,14 @@ readonly class DockerController {
public function StartBackupContainerRestore(Request $request, Response $response, array $args) : Response {
$this->configurationManager->SetBackupMode('restore');
$config = $this->configurationManager->GetConfig();
$config = [];
$config['selected-restore-time'] = $request->getParsedBody()['selected_restore_time'] ?? '';
if (isset($request->getParsedBody()['restore-exclude-previews'])) {
$config['restore-exclude-previews'] = 1;
} else {
$config['restore-exclude-previews'] = '';
}
$this->configurationManager->WriteConfig($config);
$this->configurationManager->setMultiple($config);
$id = self::TOP_CONTAINER;
$forceStopNextcloud = true;
@ -157,9 +157,7 @@ readonly class DockerController {
public function StartBackupContainerTest(Request $request, Response $response, array $args) : Response {
$this->configurationManager->SetBackupMode('test');
$config = $this->configurationManager->GetConfig();
$config['instance_restore_attempt'] = 0;
$this->configurationManager->WriteConfig($config);
$this->configurationManager->set('instance_restore_attempt', 0);
$id = self::TOP_CONTAINER;
$this->PerformRecursiveContainerStop($id);
@ -187,14 +185,14 @@ readonly class DockerController {
$installLatestMajor = "";
}
$config = $this->configurationManager->GetConfig();
$config = [];
// set AIO_URL
$config['AIO_URL'] = $host . ':' . (string)$port . $path;
// set wasStartButtonClicked
$config['wasStartButtonClicked'] = 1;
// set install_latest_major
$config['install_latest_major'] = $installLatestMajor;
$this->configurationManager->WriteConfig($config);
$this->configurationManager->setMultiple($config);
// Do not pull container images in case 'bypass_container_update' is set via url params
// Needed for local testing
@ -213,10 +211,8 @@ readonly class DockerController {
}
public function startTopContainer(bool $pullImage) : void {
$config = $this->configurationManager->GetConfig();
// set AIO_TOKEN
$config['AIO_TOKEN'] = bin2hex(random_bytes(24));
$this->configurationManager->WriteConfig($config);
$this->configurationManager->set('AIO_TOKEN', bin2hex(random_bytes(24)));
// Stop domaincheck since apache would not be able to start otherwise
$this->StopDomaincheckContainer();

View file

@ -9,7 +9,7 @@ class ConfigurationManager
{
private array $secrets = [];
public function GetConfig() : array
private function GetConfig() : array
{
if(file_exists(DataConst::GetConfigFile()))
{
@ -20,6 +20,18 @@ class ConfigurationManager
return [];
}
public function set(string $key, mixed $value) : void {
$this->setMultiple([$key => $value]);
}
public function setMultiple(array $keyValuePairs) : void {
$config = $this->GetConfig();
foreach ($keyValuePairs as $key => $value) {
$config[$key] = $value;
}
$this->WriteConfig($config);
}
public function GetPassword() : string {
return $this->GetConfig()['password'];
}
@ -599,7 +611,7 @@ class ConfigurationManager
/**
* @throws InvalidSettingConfigurationException
*/
public function WriteConfig(array $config) : void {
private function WriteConfig(array $config) : void {
if(!is_dir(DataConst::GetDataDirectory())) {
throw new InvalidSettingConfigurationException(DataConst::GetDataDirectory() . " does not exist! Something was set up falsely!");
}

View file

@ -229,14 +229,13 @@ readonly class DockerActionManager {
}
$aioVariables = $container->GetAioVariables()->GetVariables();
$config = [];
foreach ($aioVariables as $variable) {
$config = $this->configurationManager->GetConfig();
$variable = $this->replaceEnvPlaceholders($variable);
$variableArray = explode('=', $variable);
$config[$variableArray[0]] = $variableArray[1];
$this->configurationManager->WriteConfig($config);
sleep(1);
}
$this->configurationManager->setMultiple($config);
$envs = $container->GetEnvironmentVariables()->GetVariables();
// Special thing for the nextcloud container