]> git.mxchange.org Git - friendica.git/blobdiff - src/Core/Config/PreloadConfigAdapter.php
Avoid memory issue in exception
[friendica.git] / src / Core / Config / PreloadConfigAdapter.php
index ac59d945580ca6d6e55ffebda4fdff8a042a5c4a..96331e7a2cd6dd978b1e69cd6dfd078edaf21628 100644 (file)
@@ -3,7 +3,6 @@
 namespace Friendica\Core\Config;
 
 use Exception;
-use Friendica\BaseObject;
 use Friendica\Database\DBA;
 
 /**
@@ -13,56 +12,87 @@ use Friendica\Database\DBA;
  *
  * @author Hypolite Petovan <hypolite@mrpetovan.com>
  */
-class PreloadConfigAdapter extends BaseObject implements IConfigAdapter
+class PreloadConfigAdapter extends AbstractDbaConfigAdapter implements IConfigAdapter
 {
        private $config_loaded = false;
 
-       public function __construct()
+       /**
+        * @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();
                $this->load();
        }
 
+       /**
+        * {@inheritdoc}
+        */
        public function load($family = 'config')
        {
+               if (!$this->isConnected()) {
+                       return;
+               }
+
                if ($this->config_loaded) {
                        return;
                }
 
                $configs = DBA::select('config', ['cat', 'v', 'k']);
                while ($config = DBA::fetch($configs)) {
-                       self::getApp()->setConfigValue($config['cat'], $config['k'], $config['v']);
+                       $this->configCache->set($config['cat'], $config['k'], $config['v']);
                }
                DBA::close($configs);
 
                $this->config_loaded = true;
        }
 
+       /**
+        * {@inheritdoc}
+        */
        public function get($cat, $k, $default_value = null, $refresh = false)
        {
+               if (!$this->isConnected()) {
+                       return $default_value;
+               }
+
                if ($refresh) {
                        $config = DBA::selectFirst('config', ['v'], ['cat' => $cat, 'k' => $k]);
                        if (DBA::isResult($config)) {
-                               self::getApp()->setConfigValue($cat, $k, $config['v']);
+                               $this->configCache->set($cat, $k, $config['v']);
                        }
                }
 
-               $return = self::getApp()->getConfigValue($cat, $k, $default_value);
+               $return = $this->configCache->get($cat, $k, $default_value);
 
                return $return;
        }
 
+       /**
+        * {@inheritdoc}
+        */
        public function set($cat, $k, $value)
        {
+               if (!$this->isConnected()) {
+                       return false;
+               }
+
                // We store our setting values as strings.
                // So we have to do the conversion here so that the compare below works.
                // The exception are array values.
                $compare_value = !is_array($value) ? (string)$value : $value;
 
-               if (self::getApp()->getConfigValue($cat, $k) === $compare_value) {
+               if ($this->configCache->get($cat, $k) === $compare_value) {
                        return true;
                }
 
-               self::getApp()->setConfigValue($cat, $k, $value);
+               $this->configCache->set($cat, $k, $value);
 
                // manage array value
                $dbvalue = is_array($value) ? serialize($value) : $value;
@@ -75,9 +105,16 @@ class PreloadConfigAdapter extends BaseObject implements IConfigAdapter
                return true;
        }
 
+       /**
+        * {@inheritdoc}
+        */
        public function delete($cat, $k)
        {
-               self::getApp()->deleteConfigValue($cat, $k);
+               if (!$this->isConnected()) {
+                       return false;
+               }
+
+               $this->configCache->delete($cat, $k);
 
                $result = DBA::delete('config', ['cat' => $cat, 'k' => $k]);