Introduce ISetConfigValuesTransactional for transactional config behaviour

This commit is contained in:
Philipp 2023-01-03 14:18:53 +01:00
parent 4d4b4a8858
commit 65d79d4c93
No known key found for this signature in database
GPG key ID: 24A7501396EB5432
14 changed files with 588 additions and 150 deletions

View file

@ -22,6 +22,7 @@
namespace Friendica\Core\Config\Model;
use Friendica\Core\Config\Capability\IManageConfigValues;
use Friendica\Core\Config\Capability\ISetConfigValuesTransactional;
use Friendica\Core\Config\Exception\ConfigFileException;
use Friendica\Core\Config\Exception\ConfigPersistenceException;
use Friendica\Core\Config\Util\ConfigFileManager;
@ -61,8 +62,17 @@ class Config implements IManageConfigValues
return $this->configCache;
}
/** {@inheritDoc} */
public function save()
/** {@inheritDoc} */
public function transactional(): ISetConfigValuesTransactional
{
return new TransactionalConfig($this);
}
/**
* Saves the current Configuration back into the data config.
* @see ConfigFileManager::CONFIG_DATA_FILE
*/
protected function save()
{
try {
$this->configFileManager->saveData($this->configCache);
@ -84,6 +94,13 @@ class Config implements IManageConfigValues
$this->configCache = $configCache;
}
/** {@inheritDoc} */
public function load(Cache $cache)
{
$this->configCache = $cache;
$this->save();
}
/** {@inheritDoc} */
public function get(string $cat, string $key, $default_value = null)
{
@ -91,26 +108,24 @@ class Config implements IManageConfigValues
}
/** {@inheritDoc} */
public function set(string $cat, string $key, $value, bool $autosave = true): bool
public function set(string $cat, string $key, $value): bool
{
$stored = $this->configCache->set($cat, $key, $value, Cache::SOURCE_DATA);
if ($stored && $autosave) {
if ($this->configCache->set($cat, $key, $value, Cache::SOURCE_DATA)) {
$this->save();
return true;
} else {
return false;
}
return $stored;
}
/** {@inheritDoc} */
public function delete(string $cat, string $key, bool $autosave = true): bool
public function delete(string $cat, string $key): bool
{
$removed = $this->configCache->delete($cat, $key);
if ($removed && $autosave) {
if ($this->configCache->delete($cat, $key)) {
$this->save();
return true;
} else {
return false;
}
return $removed;
}
}