]> git.mxchange.org Git - friendica.git/blobdiff - src/Core/Config/JITConfigAdapter.php
Merge pull request #6612 from MrPetovan/bug/6605-cache-config-adapter-connection
[friendica.git] / src / Core / Config / JITConfigAdapter.php
index 7b0c6b4173db3e02264cb6c9ca8fd2bfe0e3294b..ecd88bb3d3d5d9f609dbb706d64a5f7eccfd1db3 100644 (file)
@@ -1,26 +1,43 @@
 <?php
 namespace Friendica\Core\Config;
 
-use Friendica\BaseObject;
 use Friendica\Database\DBA;
-use Friendica\Database\DBM;
-
-require_once 'include/dba.php';
 
 /**
  * JustInTime Configuration Adapter
  *
  * Default Config Adapter. Provides the best performance for pages loading few configuration variables.
  *
- * @author Hypolite Petovan <mrpetovan@gmail.com>
+ * @author Hypolite Petovan <hypolite@mrpetovan.com>
  */
-class JITConfigAdapter extends BaseObject implements IConfigAdapter
+class JITConfigAdapter extends AbstractDbaConfigAdapter implements IConfigAdapter
 {
        private $cache;
        private $in_db;
 
+       /**
+        * @var IConfigCache The config cache of this driver
+        */
+       private $configCache;
+
+       /**
+        * @param IConfigCache $configCache The config cache of this driver
+        */
+       public function __construct(IConfigCache $configCache)
+       {
+               $this->configCache = $configCache;
+               $this->connected = DBA::connected();
+       }
+
+       /**
+        * {@inheritdoc}
+        */
        public function load($cat = "config")
        {
+               if (!$this->isConnected()) {
+                       return;
+               }
+
                // We don't preload "system" anymore.
                // This reduces the number of database reads a lot.
                if ($cat === 'system') {
@@ -31,7 +48,7 @@ class JITConfigAdapter extends BaseObject implements IConfigAdapter
                while ($config = DBA::fetch($configs)) {
                        $k = $config['k'];
 
-                       self::getApp()->setConfigValue($cat, $k, $config['v']);
+                       $this->configCache->set($cat, $k, $config['v']);
 
                        if ($cat !== 'config') {
                                $this->cache[$cat][$k] = $config['v'];
@@ -41,9 +58,14 @@ class JITConfigAdapter extends BaseObject implements IConfigAdapter
                DBA::close($configs);
        }
 
+       /**
+        * {@inheritdoc}
+        */
        public function get($cat, $k, $default_value = null, $refresh = false)
        {
-               $a = self::getApp();
+               if (!$this->isConnected()) {
+                       return $default_value;
+               }
 
                if (!$refresh) {
                        // Do we have the cached value? Then return it
@@ -57,7 +79,7 @@ class JITConfigAdapter extends BaseObject implements IConfigAdapter
                }
 
                $config = DBA::selectFirst('config', ['v'], ['cat' => $cat, 'k' => $k]);
-               if (DBM::is_result($config)) {
+               if (DBA::isResult($config)) {
                        // manage array value
                        $value = (preg_match("|^a:[0-9]+:{.*}$|s", $config['v']) ? unserialize($config['v']) : $config['v']);
 
@@ -65,18 +87,18 @@ class JITConfigAdapter extends BaseObject implements IConfigAdapter
                        $this->cache[$cat][$k] = $value;
                        $this->in_db[$cat][$k] = true;
                        return $value;
-               } elseif (isset($a->config[$cat][$k])) {
-                       // Assign the value (mostly) from config/local.ini.php file to the cache
-                       $this->cache[$cat][$k] = $a->config[$cat][$k];
+               } elseif ($this->configCache->get($cat, $k) !== null) {
+                       // Assign the value (mostly) from config/local.config.php file to the cache
+                       $this->cache[$cat][$k] = $this->configCache->get($cat, $k);
                        $this->in_db[$cat][$k] = false;
 
-                       return $a->config[$cat][$k];
-               } elseif (isset($a->config[$k])) {
-                       // Assign the value (mostly) from config/local.ini.php file to the cache
-                       $this->cache[$k] = $a->config[$k];
+                       return $this->configCache->get($cat, $k);
+               } elseif ($this->configCache->get('config', $k) !== null) {
+                       // Assign the value (mostly) from config/local.config.php file to the cache
+                       $this->cache[$k] = $this->configCache->get('config', $k);
                        $this->in_db[$k] = false;
 
-                       return $a->config[$k];
+                       return $this->configCache->get('config', $k);
                }
 
                $this->cache[$cat][$k] = '!<unset>!';
@@ -85,9 +107,14 @@ class JITConfigAdapter extends BaseObject implements IConfigAdapter
                return $default_value;
        }
 
+       /**
+        * {@inheritdoc}
+        */
        public function set($cat, $k, $value)
        {
-               $a = self::getApp();
+               if (!$this->isConnected()) {
+                       return false;
+               }
 
                // We store our setting values in a string variable.
                // So we have to do the conversion here so that the compare below works.
@@ -107,7 +134,7 @@ class JITConfigAdapter extends BaseObject implements IConfigAdapter
                        return true;
                }
 
-               self::getApp()->setConfigValue($cat, $k, $value);
+               $this->configCache->set($cat, $k, $value);
 
                // Assign the just added value to the cache
                $this->cache[$cat][$k] = $dbvalue;
@@ -124,8 +151,15 @@ class JITConfigAdapter extends BaseObject implements IConfigAdapter
                return $result;
        }
 
+       /**
+        * {@inheritdoc}
+        */
        public function delete($cat, $k)
        {
+               if (!$this->isConnected()) {
+                       return false;
+               }
+
                if (isset($this->cache[$cat][$k])) {
                        unset($this->cache[$cat][$k]);
                        unset($this->in_db[$cat][$k]);