3 namespace Friendica\Model\Config;
7 * The Config model backend, which is using the general DB-model backend for configs
9 class Config extends DbaConfig
12 * Loads all configuration values and returns the loaded category as an array.
14 * @param string|null $cat The category of the configuration values to load
16 * @return array The config array
18 * @throws \Exception In case DB calls are invalid
20 public function load(string $cat = null)
25 $configs = $this->dba->select('config', ['cat', 'v', 'k']);
27 $configs = $this->dba->select('config', ['cat', 'v', 'k'], ['cat' => $cat]);
30 while ($config = $this->dba->fetch($configs)) {
33 $value = $this->toConfigValue($config['v']);
35 // just save it in case it is set
37 $return[$config['cat']][$key] = $value;
40 $this->dba->close($configs);
46 * Get a particular, system-wide config variable out of the DB with the
47 * given category name ($cat) and a key ($key).
49 * Note: Boolean variables are defined as 0/1 in the database
51 * @param string $cat The category of the configuration value
52 * @param string $key The configuration key to query
54 * @return array|string|null Stored value or null if it does not exist
56 * @throws \Exception In case DB calls are invalid
58 public function get(string $cat, string $key)
60 if (!$this->isConnected()) {
64 $config = $this->dba->selectFirst('config', ['v'], ['cat' => $cat, 'k' => $key]);
65 if ($this->dba->isResult($config)) {
66 $value = $this->toConfigValue($config['v']);
68 // just return it in case it is set
78 * Stores a config value ($value) in the category ($cat) under the key ($key).
80 * Note: Please do not store booleans - convert to 0/1 integer values!
82 * @param string $cat The category of the configuration value
83 * @param string $key The configuration key to set
84 * @param mixed $value The value to store
86 * @return bool Operation success
88 * @throws \Exception In case DB calls are invalid
90 public function set(string $cat, string $key, $value)
92 if (!$this->isConnected()) {
96 // We store our setting values in a string variable.
97 // So we have to do the conversion here so that the compare below works.
98 // The exception are array values.
99 $compare_value = (!is_array($value) ? (string)$value : $value);
100 $stored_value = $this->get($cat, $key);
102 if (isset($stored_value) && ($stored_value === $compare_value)) {
106 $dbvalue = $this->toDbValue($value);
108 $result = $this->dba->update('config', ['v' => $dbvalue], ['cat' => $cat, 'k' => $key], true);
114 * Removes the configured value from the database.
116 * @param string $cat The category of the configuration value
117 * @param string $key The configuration key to delete
119 * @return bool Operation success
121 * @throws \Exception In case DB calls are invalid
123 public function delete(string $cat, string $key)
125 if (!$this->isConnected()) {
129 return $this->dba->delete('config', ['cat' => $cat, 'k' => $key]);