-<?php\r
-\r
-namespace Friendica\Core\Config;\r
-\r
-use dba;\r
-use Exception;\r
-use Friendica\App;\r
-use Friendica\BaseObject;\r
-use Friendica\Database\DBM;\r
-\r
-require_once 'include/dba.php';\r
-\r
-/**\r
- * Preload ConfigAdapter\r
- *\r
- * Minimize the number of database queries to retrieve configuration values at the cost of memory.\r
- *\r
- * @author Hypolite Petovan <mrpetovan@gmail.com>\r
- */\r
-class PreloadConfigAdapter extends BaseObject implements IConfigAdapter\r
-{\r
- private $config_loaded = false;\r
-\r
- public function __construct()\r
- {\r
- $this->load();\r
- }\r
-\r
- public function load($family = 'config')\r
- {\r
- if ($this->config_loaded) {\r
- return;\r
- }\r
-\r
- $configs = dba::select('config', ['cat', 'v', 'k']);\r
- while ($config = dba::fetch($configs)) {\r
- $this->setPreloadedValue($config['cat'], $config['k'], $config['v']);\r
- }\r
- dba::close($configs);\r
-\r
- $this->config_loaded = true;\r
- }\r
-\r
- public function get($cat, $k, $default_value = null, $refresh = false)\r
- {\r
- if ($refresh) {\r
- $config = dba::selectFirst('config', ['v'], ['cat' => $cat, 'k' => $k]);\r
- if (DBM::is_result($config)) {\r
- $this->setPreloadedValue($cat, $k, $config['v']);\r
- }\r
- }\r
-\r
- $return = $this->getPreloadedValue($cat, $k, $default_value);\r
-\r
- return $return;\r
- }\r
-\r
- public function set($cat, $k, $value)\r
- {\r
- // We store our setting values as strings.\r
- // So we have to do the conversion here so that the compare below works.\r
- // The exception are array values.\r
- $compare_value = !is_array($value) ? (string)$value : $value;\r
-\r
- if ($this->getPreloadedValue($cat, $k) === $compare_value) {\r
- return true;\r
- }\r
-\r
- $this->setPreloadedValue($cat, $k, $value);\r
-\r
- // manage array value\r
- $dbvalue = is_array($value) ? serialize($value) : $value;\r
-\r
- $result = dba::update('config', ['v' => $dbvalue], ['cat' => $cat, 'k' => $k], true);\r
- if (!$result) {\r
- throw new Exception('Unable to store config value in [' . $cat . '][' . $k . ']');\r
- }\r
-\r
- return true;\r
- }\r
-\r
- public function delete($cat, $k)\r
- {\r
- $this->deletePreloadedValue($cat, $k);\r
-\r
- $result = dba::delete('config', ['cat' => $cat, 'k' => $k]);\r
-\r
- return $result;\r
- }\r
-\r
- /**\r
- * Retrieves a preloaded value from the App config cache\r
- *\r
- * @param string $cat\r
- * @param string $k\r
- * @param mixed $default\r
- */\r
- private function getPreloadedValue($cat, $k, $default = null)\r
- {\r
- $a = self::getApp();\r
-\r
- $return = $default;\r
-\r
- if ($cat === 'config') {\r
- if (isset($a->config[$k])) {\r
- $return = $a->config[$k];\r
- }\r
- } else {\r
- if (isset($a->config[$cat][$k])) {\r
- $return = $a->config[$cat][$k];\r
- }\r
- }\r
-\r
- return $return;\r
- }\r
-\r
- /**\r
- * Sets a preloaded value in the App config cache\r
- *\r
- * Accepts raw output from the config table\r
- *\r
- * @param string $cat\r
- * @param string $k\r
- * @param mixed $v\r
- */\r
- private function setPreloadedValue($cat, $k, $v)\r
- {\r
- $a = self::getApp();\r
-\r
- // Only arrays are serialized in database, so we have to unserialize sparingly\r
- $value = is_string($v) && preg_match("|^a:[0-9]+:{.*}$|s", $v) ? unserialize($v) : $v;\r
-\r
- if ($cat === 'config') {\r
- $a->config[$k] = $value;\r
- } else {\r
- $a->config[$cat][$k] = $value;\r
- }\r
- }\r
-\r
- /**\r
- * Deletes a preloaded value from the App config cache\r
- *\r
- * @param string $cat\r
- * @param string $k\r
- */\r
- private function deletePreloadedValue($cat, $k)\r
- {\r
- $a = self::getApp();\r
-\r
- if ($cat === 'config') {\r
- if (isset($a->config[$k])) {\r
- unset($a->config[$k]);\r
- }\r
- } else {\r
- if (isset($a->config[$cat][$k])) {\r
- unset($a->config[$cat][$k]);\r
- }\r
- }\r
- }\r
-}\r
+<?php
+
+namespace Friendica\Core\Config;
+
+use Exception;
+use Friendica\BaseObject;
+use Friendica\Database\DBA;
+
+require_once 'include/dba.php';
+
+/**
+ * Preload Configuration Adapter
+ *
+ * Minimizes the number of database queries to retrieve configuration values at the cost of memory.
+ *
+ * @author Hypolite Petovan <hypolite@mrpetovan.com>
+ */
+class PreloadConfigAdapter extends BaseObject implements IConfigAdapter
+{
+ private $config_loaded = false;
+
+ public function __construct()
+ {
+ $this->load();
+ }
+
+ public function load($family = 'config')
+ {
+ 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']);
+ }
+ DBA::close($configs);
+
+ $this->config_loaded = true;
+ }
+
+ public function get($cat, $k, $default_value = null, $refresh = false)
+ {
+ if ($refresh) {
+ $config = DBA::selectFirst('config', ['v'], ['cat' => $cat, 'k' => $k]);
+ if (DBA::isResult($config)) {
+ self::getApp()->setConfigValue($cat, $k, $config['v']);
+ }
+ }
+
+ $return = self::getApp()->getConfigValue($cat, $k, $default_value);
+
+ return $return;
+ }
+
+ public function set($cat, $k, $value)
+ {
+ // 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) {
+ return true;
+ }
+
+ self::getApp()->setConfigValue($cat, $k, $value);
+
+ // manage array value
+ $dbvalue = is_array($value) ? serialize($value) : $value;
+
+ $result = DBA::update('config', ['v' => $dbvalue], ['cat' => $cat, 'k' => $k], true);
+ if (!$result) {
+ throw new Exception('Unable to store config value in [' . $cat . '][' . $k . ']');
+ }
+
+ return true;
+ }
+
+ public function delete($cat, $k)
+ {
+ self::getApp()->deleteConfigValue($cat, $k);
+
+ $result = DBA::delete('config', ['cat' => $cat, 'k' => $k]);
+
+ return $result;
+ }
+}