]> git.mxchange.org Git - friendica.git/blobdiff - src/Core/PConfig.php
Rework App modes
[friendica.git] / src / Core / PConfig.php
index ad658f8f8ad77ebcdab8ae1ad61cba9b3bf3b07d..aa5f75abe737b2350e199ae9083e899e6a6bce0e 100644 (file)
@@ -1,20 +1,17 @@
 <?php
 /**
- * @file src/Core/PConfig.php
+ * User Configuration Class
+ *
+ * @file include/Core/PConfig.php
+ *
+ * @brief Contains the class with methods for user configuration
  */
 namespace Friendica\Core;
 
-use Friendica\Database\DBM;
-use dba;
+use Friendica\BaseObject;
 
 require_once 'include/dba.php';
 
-/**
- * @file include/Core/PConfig.php
- * @brief contains the class with methods for the management
- * of the user configuration
- */
-
 /**
  * @brief Management of user configuration storage
  * Note:
@@ -22,9 +19,26 @@ require_once 'include/dba.php';
  * The PConfig::get() functions return boolean false for keys that are unset,
  * and this could lead to subtle bugs.
  */
-class PConfig
+class PConfig extends BaseObject
 {
-       private static $in_db;
+       /**
+        * @var Friendica\Core\Config\IPConfigAdapter
+        */
+       private static $adapter = null;
+
+       public static function init($uid)
+       {
+               // Database isn't ready or populated yet
+               if (!(self::getApp()->mode & \Friendica\App::MODE_DBCONFIGAVAILABLE)) {
+                       return;
+               }
+
+               if (self::getApp()->getConfigValue('system', 'config_adapter') == 'preload') {
+                       self::$adapter = new Config\PreloadPConfigAdapter($uid);
+               } else {
+                       self::$adapter = new Config\JITPConfigAdapter($uid);
+               }
+       }
 
        /**
         * @brief Loads all configuration values of a user's config family into a cached storage.
@@ -39,20 +53,16 @@ class PConfig
         */
        public static function load($uid, $family)
        {
-               $a = get_app();
-
-               $r = dba::select('pconfig', array('v', 'k'), array('cat' => $family, 'uid' => $uid));
-               if (DBM::is_result($r)) {
-                       while ($rr = dba::fetch($r)) {
-                               $k = $rr['k'];
-                               $a->config[$uid][$family][$k] = $rr['v'];
-                               self::$in_db[$uid][$family][$k] = true;
-                       }
-               } else if ($family != 'config') {
-                       // Negative caching
-                       $a->config[$uid][$family] = "!<unset>!";
+               // Database isn't ready or populated yet
+               if (!(self::getApp()->mode & \Friendica\App::MODE_DBCONFIGAVAILABLE)) {
+                       return;
+               }
+
+               if (empty(self::$adapter)) {
+                       self::init($uid);
                }
-               dba::close($r);
+
+               self::$adapter->load($uid, $family);
        }
 
        /**
@@ -72,37 +82,16 @@ class PConfig
         */
        public static function get($uid, $family, $key, $default_value = null, $refresh = false)
        {
-               $a = get_app();
-
-               if (!$refresh) {
-                       // Looking if the whole family isn't set
-                       if (isset($a->config[$uid][$family])) {
-                               if ($a->config[$uid][$family] === '!<unset>!') {
-                                       return $default_value;
-                               }
-                       }
-
-                       if (isset($a->config[$uid][$family][$key])) {
-                               if ($a->config[$uid][$family][$key] === '!<unset>!') {
-                                       return $default_value;
-                               }
-                               return $a->config[$uid][$family][$key];
-                       }
+               // Database isn't ready or populated yet
+               if (!(self::getApp()->mode & \Friendica\App::MODE_DBCONFIGAVAILABLE)) {
+                       return;
                }
 
-               $pconfig = dba::selectFirst('pconfig', ['v'], ['uid' => $uid, 'cat' => $family, 'k' => $key]);
-               if (DBM::is_result($pconfig)) {
-                       $val = (preg_match("|^a:[0-9]+:{.*}$|s", $pconfig['v']) ? unserialize($pconfig['v']) : $pconfig['v']);
-                       $a->config[$uid][$family][$key] = $val;
-                       self::$in_db[$uid][$family][$key] = true;
-
-                       return $val;
-               } else {
-                       $a->config[$uid][$family][$key] = '!<unset>!';
-                       self::$in_db[$uid][$family][$key] = false;
-
-                       return $default_value;
+               if (empty(self::$adapter)) {
+                       self::init($uid);
                }
+
+               return self::$adapter->get($uid, $family, $key, $default_value, $refresh);
        }
 
        /**
@@ -122,31 +111,16 @@ class PConfig
         */
        public static function set($uid, $family, $key, $value)
        {
-               $a = get_app();
-
-               // We store our setting values in a string variable.
-               // So we have to do the conversion here so that the compare below works.
-               // The exception are array values.
-               $dbvalue = (!is_array($value) ? (string)$value : $value);
-
-               $stored = self::get($uid, $family, $key, null, true);
-
-               if (($stored === $dbvalue) && self::$in_db[$uid][$family][$key]) {
-                       return true;
+               // Database isn't ready or populated yet
+               if (!(self::getApp()->mode & \Friendica\App::MODE_DBCONFIGAVAILABLE)) {
+                       return false;
                }
 
-               $a->config[$uid][$family][$key] = $dbvalue;
-
-               // manage array value
-               $dbvalue = (is_array($value) ? serialize($value) : $dbvalue);
-
-               $ret = dba::update('pconfig', array('v' => $dbvalue), array('uid' => $uid, 'cat' => $family, 'k' => $key), true);
-
-               if ($ret) {
-                       self::$in_db[$uid][$family][$key] = true;
-                       return $value;
+               if (empty(self::$adapter)) {
+                       self::init($uid);
                }
-               return $ret;
+
+               return self::$adapter->set($uid, $family, $key, $value);
        }
 
        /**
@@ -163,15 +137,15 @@ class PConfig
         */
        public static function delete($uid, $family, $key)
        {
-               $a = get_app();
-
-               if (x($a->config[$uid][$family], $key)) {
-                       unset($a->config[$uid][$family][$key]);
-                       unset(self::$in_db[$uid][$family][$key]);
+               // Database isn't ready or populated yet
+               if (!(self::getApp()->mode & \Friendica\App::MODE_DBCONFIGAVAILABLE)) {
+                       return false;
                }
 
-               $ret = dba::delete('pconfig', array('uid' => $uid, 'cat' => $family, 'k' => $key));
+               if (empty(self::$adapter)) {
+                       self::init($uid);
+               }
 
-               return $ret;
+               return self::$adapter->delete($uid, $family, $key);
        }
 }