3 * User Configuration Class
5 * @file include/Core/PConfig.php
7 * @brief Contains the class with methods for user configuration
9 namespace Friendica\Core;
12 use Friendica\BaseObject;
15 * @brief Management of user configuration storage
17 * Please do not store booleans - convert to 0/1 integer values
18 * The PConfig::get() functions return boolean false for keys that are unset,
19 * and this could lead to subtle bugs.
21 class PConfig extends BaseObject
24 * @var \Friendica\Core\Config\IPConfigAdapter
26 private static $adapter = null;
28 public static function init($uid)
32 // Database isn't ready or populated yet
33 if (!$a->getMode()->has(App\Mode::DBCONFIGAVAILABLE)) {
37 if ($a->getConfigValue('system', 'config_adapter') == 'preload') {
38 self::$adapter = new Config\PreloadPConfigAdapter($uid);
40 self::$adapter = new Config\JITPConfigAdapter();
45 * @brief Loads all configuration values of a user's config family into a cached storage.
47 * All configuration values of the given user are stored in global cache
48 * which is available under the global variable $a->config[$uid].
50 * @param string $uid The user_id
51 * @param string $family The category of the configuration value
54 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
56 public static function load($uid, $family)
58 // Database isn't ready or populated yet
59 if (!self::getApp()->getMode()->has(App\Mode::DBCONFIGAVAILABLE)) {
63 if (empty(self::$adapter)) {
67 self::$adapter->load($uid, $family);
71 * @brief Get a particular user's config variable given the category name
72 * ($family) and a key.
74 * Get a particular user's config value from the given category ($family)
75 * and the $key from a cached storage in $a->config[$uid].
77 * @param string $uid The user_id
78 * @param string $family The category of the configuration value
79 * @param string $key The configuration key to query
80 * @param mixed $default_value optional, The value to return if key is not set (default: null)
81 * @param boolean $refresh optional, If true the config is loaded from the db and not from the cache (default: false)
83 * @return mixed Stored value or null if it does not exist
84 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
86 public static function get($uid, $family, $key, $default_value = null, $refresh = false)
88 // Database isn't ready or populated yet
89 if (!self::getApp()->getMode()->has(App\Mode::DBCONFIGAVAILABLE)) {
93 if (empty(self::$adapter)) {
97 return self::$adapter->get($uid, $family, $key, $default_value, $refresh);
101 * @brief Sets a configuration value for a user
103 * Stores a config value ($value) in the category ($family) under the key ($key)
104 * for the user_id $uid.
106 * @note Please do not store booleans - convert to 0/1 integer values!
108 * @param string $uid The user_id
109 * @param string $family The category of the configuration value
110 * @param string $key The configuration key to set
111 * @param mixed $value The value to store
113 * @return bool Operation success
114 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
116 public static function set($uid, $family, $key, $value)
118 // Database isn't ready or populated yet
119 if (!self::getApp()->getMode()->has(App\Mode::DBCONFIGAVAILABLE)) {
123 if (empty(self::$adapter)) {
127 return self::$adapter->set($uid, $family, $key, $value);
131 * @brief Deletes the given key from the users's configuration.
133 * Removes the configured value from the stored cache in $a->config[$uid]
134 * and removes it from the database.
136 * @param string $uid The user_id
137 * @param string $family The category of the configuration value
138 * @param string $key The configuration key to delete
141 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
143 public static function delete($uid, $family, $key)
145 // Database isn't ready or populated yet
146 if (!self::getApp()->getMode()->has(App\Mode::DBCONFIGAVAILABLE)) {
150 if (empty(self::$adapter)) {
154 return self::$adapter->delete($uid, $family, $key);