]> git.mxchange.org Git - friendica.git/blobdiff - src/Core/Config/PreloadPConfigAdapter.php
Merge pull request #6593 from tobiasd/20190206-can
[friendica.git] / src / Core / Config / PreloadPConfigAdapter.php
index 8ed3ee10e82b9b4c75291af78a3d7e0157af5c81..af97815adef38d159ab6ba44f27766d10ed7ca9d 100644 (file)
@@ -2,30 +2,41 @@
 
 namespace Friendica\Core\Config;
 
-use dba;
 use Exception;
-use Friendica\App;
-use Friendica\BaseObject;
-use Friendica\Database\DBM;
-
-require_once 'include/dba.php';
+use Friendica\Database\DBA;
 
 /**
  * Preload User Configuration Adapter
  *
  * Minimizes the number of database queries to retrieve configuration values at the cost of memory.
  *
- * @author Hypolite Petovan <mrpetovan@gmail.com>
+ * @author Hypolite Petovan <hypolite@mrpetovan.com>
  */
-class PreloadPConfigAdapter extends BaseObject implements IPConfigAdapter
+class PreloadPConfigAdapter implements IPConfigAdapter
 {
        private $config_loaded = false;
 
-       public function __construct($uid)
+       /**
+        * The config cache of this adapter
+        * @var IPConfigCache
+        */
+       private $configCache;
+
+       /**
+        * @param IPConfigCache $configCache The config cache of this adapter
+        * @param int           $uid    The UID of the current user
+        */
+       public function __construct(IPConfigCache $configCache, $uid = null)
        {
-               $this->load($uid, 'config');
+               $this->configCache = $configCache;
+               if (isset($uid)) {
+                       $this->load($uid, 'config');
+               }
        }
 
+       /**
+        * {@inheritdoc}
+        */
        public function load($uid, $family)
        {
                if ($this->config_loaded) {
@@ -36,15 +47,18 @@ class PreloadPConfigAdapter extends BaseObject implements IPConfigAdapter
                        return;
                }
 
-               $pconfigs = dba::select('pconfig', ['cat', 'v', 'k'], ['uid' => $uid]);
-               while ($pconfig = dba::fetch($pconfigs)) {
-                       self::getApp()->setPConfigValue($uid, $pconfig['cat'], $pconfig['k'], $pconfig['v']);
+               $pconfigs = DBA::select('pconfig', ['cat', 'v', 'k'], ['uid' => $uid]);
+               while ($pconfig = DBA::fetch($pconfigs)) {
+                       $this->configCache->setP($uid, $pconfig['cat'], $pconfig['k'], $pconfig['v']);
                }
-               dba::close($pconfigs);
+               DBA::close($pconfigs);
 
                $this->config_loaded = true;
        }
 
+       /**
+        * {@inheritdoc}
+        */
        public function get($uid, $cat, $k, $default_value = null, $refresh = false)
        {
                if (!$this->config_loaded) {
@@ -52,19 +66,20 @@ class PreloadPConfigAdapter extends BaseObject implements IPConfigAdapter
                }
 
                if ($refresh) {
-                       $config = dba::selectFirst('pconfig', ['v'], ['uid' => $uid, 'cat' => $cat, 'k' => $k]);
-                       if (DBM::is_result($config)) {
-                               self::getApp()->setPConfigValue($uid, $cat, $k, $config['v']);
+                       $config = DBA::selectFirst('pconfig', ['v'], ['uid' => $uid, 'cat' => $cat, 'k' => $k]);
+                       if (DBA::isResult($config)) {
+                               $this->configCache->setP($uid, $cat, $k, $config['v']);
                        } else {
-                               self::getApp()->deletePConfigValue($uid, $cat, $k);
+                               $this->configCache->deleteP($uid, $cat, $k);
                        }
                }
 
-               $return = self::getApp()->getPConfigValue($uid, $cat, $k, $default_value);
-
-               return $return;
+               return $this->configCache->getP($uid, $cat, $k, $default_value);;
        }
 
+       /**
+        * {@inheritdoc}
+        */
        public function set($uid, $cat, $k, $value)
        {
                if (!$this->config_loaded) {
@@ -75,16 +90,16 @@ class PreloadPConfigAdapter extends BaseObject implements IPConfigAdapter
                // The exception are array values.
                $compare_value = !is_array($value) ? (string)$value : $value;
 
-               if (self::getApp()->getPConfigValue($uid, $cat, $k) === $compare_value) {
+               if ($this->configCache->getP($uid, $cat, $k) === $compare_value) {
                        return true;
                }
 
-               self::getApp()->setPConfigValue($uid, $cat, $k, $value);
+               $this->configCache->setP($uid, $cat, $k, $value);
 
                // manage array value
                $dbvalue = is_array($value) ? serialize($value) : $value;
 
-               $result = dba::update('pconfig', ['v' => $dbvalue], ['uid' => $uid, 'cat' => $cat, 'k' => $k], true);
+               $result = DBA::update('pconfig', ['v' => $dbvalue], ['uid' => $uid, 'cat' => $cat, 'k' => $k], true);
                if (!$result) {
                        throw new Exception('Unable to store config value in [' . $uid . '][' . $cat . '][' . $k . ']');
                }
@@ -92,15 +107,18 @@ class PreloadPConfigAdapter extends BaseObject implements IPConfigAdapter
                return true;
        }
 
+       /**
+        * {@inheritdoc}
+        */
        public function delete($uid, $cat, $k)
        {
                if (!$this->config_loaded) {
                        $this->load($uid, $cat);
                }
 
-               self::getApp()->deletePConfigValue($uid, $cat, $k);
+               $this->configCache->deleteP($uid, $cat, $k);
 
-               $result = dba::delete('pconfig', ['uid' => $uid, 'cat' => $cat, 'k' => $k]);
+               $result = DBA::delete('pconfig', ['uid' => $uid, 'cat' => $cat, 'k' => $k]);
 
                return $result;
        }