. * */ namespace Friendica\Core\Config\Model; use Friendica\Core\Config\Capability\IManageConfigValues; use Friendica\Core\Config\Capability\ISetConfigValuesTransactionally; use Friendica\Core\Config\Exception\ConfigPersistenceException; use Friendica\Core\Config\ValueObject\Cache; /** * Transaction class for configurations, which sets values into a temporary buffer until "save()" is called */ class ConfigTransaction implements ISetConfigValuesTransactionally { /** @var IManageConfigValues */ protected $config; /** @var Cache */ protected $cache; /** @var bool field to check if something is to save */ protected $changedConfig = false; public function __construct(IManageConfigValues $config) { $this->config = $config; $this->cache = clone $config->getCache(); } /** {@inheritDoc} */ public function set(string $cat, string $key, $value): ISetConfigValuesTransactionally { $this->cache->set($cat, $key, $value, Cache::SOURCE_DATA); $this->changedConfig = true; return $this; } /** {@inheritDoc} */ public function delete(string $cat, string $key): ISetConfigValuesTransactionally { $this->cache->delete($cat, $key, Cache::SOURCE_DATA); $this->changedConfig = true; return $this; } /** {@inheritDoc} */ public function commit(): void { // If nothing changed, just do nothing :) if (!$this->changedConfig) { return; } try { $this->config->load($this->cache); $this->cache = clone $this->config->getCache(); } catch (\Exception $e) { throw new ConfigPersistenceException('Cannot save config', $e); } } }