namespace Friendica\Core\Config\Model;
use Friendica\Core\Config\Capability\IManageConfigValues;
+use Friendica\Core\Config\Capability\ISetConfigValuesTransactionally;
+use Friendica\Core\Config\Exception\ConfigFileException;
+use Friendica\Core\Config\Exception\ConfigPersistenceException;
use Friendica\Core\Config\Util\ConfigFileManager;
use Friendica\Core\Config\ValueObject\Cache;
*/
class Config implements IManageConfigValues
{
- /**
- * @var Cache
- */
+ /** @var Cache */
protected $configCache;
/** @var ConfigFileManager */
/**
* @param ConfigFileManager $configFileManager The configuration file manager to save back configs
- * @param Cache $configCache The configuration cache (based on the config-files)
+ * @param Cache $configCache The configuration cache (based on the config-files)
*/
public function __construct(ConfigFileManager $configFileManager, Cache $configCache)
{
$this->configCache = $configCache;
}
+ /**
+ * Load all configuration values from a given cache and saves it back in the configuration node store
+ * @see ConfigFileManager::CONFIG_DATA_FILE
+ *
+ * All configuration values of the system are stored in the cache.
+ *
+ * @param Cache $cache a new cache
+ *
+ * @return void
+ *
+ * @throws ConfigPersistenceException In case the persistence layer throws errors
+ */
+ public function setCacheAndSave(Cache $cache)
+ {
+ $this->configCache = $cache;
+ $this->save();
+ }
+
/**
* {@inheritDoc}
*/
return $this->configCache;
}
- public function save()
+ /** {@inheritDoc} */
+ public function beginTransaction(): ISetConfigValuesTransactionally
+ {
+ return new ConfigTransaction($this);
+ }
+
+ /**
+ * Saves the current Configuration back into the data config.
+ * @see ConfigFileManager::CONFIG_DATA_FILE
+ */
+ protected function save()
{
- $this->configFileManager->saveData($this->configCache);
+ try {
+ $this->configFileManager->saveData($this->configCache);
+ // reload after the save to possible reload default values of lower source-priorities again
+ $this->reload();
+ } catch (ConfigFileException $e) {
+ throw new ConfigPersistenceException('Cannot save config', $e);
+ }
}
- public function load(string $cat = 'config')
+ /** {@inheritDoc} */
+ public function reload()
{
$configCache = new Cache();
- $this->configFileManager->setupCache($configCache, $_SERVER);
+ try {
+ $this->configFileManager->setupCache($configCache);
+ } catch (ConfigFileException $e) {
+ throw new ConfigPersistenceException('Cannot reload config', $e);
+ }
$this->configCache = $configCache;
}
- public function get(string $cat, string $key, $default_value = null, bool $refresh = false)
+ /** {@inheritDoc} */
+ public function get(string $cat, string $key = null, $default_value = null)
{
return $this->configCache->get($cat, $key) ?? $default_value;
}
- public function set(string $cat, string $key, $value, bool $autosave = true): bool
+ /** {@inheritDoc} */
+ 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;
}
- public function delete(string $cat, string $key, bool $autosave = true): bool
+ /** {@inheritDoc} */
+ 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;
}
}