]> git.mxchange.org Git - friendica.git/blobdiff - src/Core/Config/Model/Config.php
Merge pull request #12674 from nupplaphil/bug/config_typesafe
[friendica.git] / src / Core / Config / Model / Config.php
index 3af2f7e4b1e8c3f992ce09d6ddf05e28bb25b441..46d5643b344bc66e4538b693b17bc400bcba296f 100644 (file)
@@ -22,6 +22,9 @@
 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;
 
@@ -30,9 +33,7 @@ use Friendica\Core\Config\ValueObject\Cache;
  */
 class Config implements IManageConfigValues
 {
-       /**
-        * @var Cache
-        */
+       /** @var Cache */
        protected $configCache;
 
        /** @var ConfigFileManager */
@@ -40,7 +41,7 @@ class Config implements IManageConfigValues
 
        /**
         * @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)
        {
@@ -48,6 +49,24 @@ class Config implements IManageConfigValues
                $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}
         */
@@ -56,43 +75,65 @@ class Config implements IManageConfigValues
                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;
        }
 }