From 7159bc68d3f09cbce16f5b0756d89684bef47484 Mon Sep 17 00:00:00 2001 From: Pablo Zmdl Date: Wed, 7 Jan 2026 16:16:20 +0100 Subject: [PATCH] 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 --- php/src/Controller/DockerController.php | 16 ++++++---------- php/src/Data/ConfigurationManager.php | 16 ++++++++++++++-- php/src/Docker/DockerActionManager.php | 5 ++--- 3 files changed, 22 insertions(+), 15 deletions(-) diff --git a/php/src/Controller/DockerController.php b/php/src/Controller/DockerController.php index 27a06bc8..74b206f8 100644 --- a/php/src/Controller/DockerController.php +++ b/php/src/Controller/DockerController.php @@ -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(); diff --git a/php/src/Data/ConfigurationManager.php b/php/src/Data/ConfigurationManager.php index 320bc477..678cfbe4 100644 --- a/php/src/Data/ConfigurationManager.php +++ b/php/src/Data/ConfigurationManager.php @@ -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!"); } diff --git a/php/src/Docker/DockerActionManager.php b/php/src/Docker/DockerActionManager.php index 9e8a8ff2..313e74d0 100644 --- a/php/src/Docker/DockerActionManager.php +++ b/php/src/Docker/DockerActionManager.php @@ -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