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 * @brief Management of user configuration storage
14 * Please do not store booleans - convert to 0/1 integer values
15 * The PConfig::get() functions return boolean false for keys that are unset,
16 * and this could lead to subtle bugs.
21 * @var Config\IPConfigAdapter
23 private static $adapter;
26 * @var Config\IPConfigCache
28 private static $cache;
31 * Initialize the config with only the cache
33 * @param Config\IPConfigCache $cache The configuration cache
35 public static function init(Config\IPConfigCache $cache)
37 self::$cache = $cache;
41 * Add the adapter for DB-backend
43 * @param Config\IPConfigAdapter $adapter
45 public static function setAdapter(Config\IPConfigAdapter $adapter)
47 self::$adapter = $adapter;
51 * @brief Loads all configuration values of a user's config family into a cached storage.
53 * All configuration values of the given user are stored with the $uid in
54 * the cache ( @see IPConfigCache )
56 * @param string $uid The user_id
57 * @param string $family The category of the configuration value
61 public static function load($uid, $family)
63 if (!isset(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 with the $uid from a cached storage either from the self::$adapter
76 * (@see IConfigAdapter ) or from the static::$cache (@see IConfigCache ).
78 * @param string $uid The user_id
79 * @param string $family The category of the configuration value
80 * @param string $key The configuration key to query
81 * @param mixed $default_value optional, The value to return if key is not set (default: null)
82 * @param boolean $refresh optional, If true the config is loaded from the db and not from the cache (default: false)
84 * @return mixed Stored value or null if it does not exist
86 public static function get($uid, $family, $key, $default_value = null, $refresh = false)
88 if (!isset(self::$adapter)) {
89 return self::$cache->getP($uid, $family, $key, $default_value);
92 return self::$adapter->get($uid, $family, $key, $default_value, $refresh);
96 * @brief Sets a configuration value for a user
98 * Stores a config value ($value) in the category ($family) under the key ($key)
99 * for the user_id $uid.
101 * @note Please do not store booleans - convert to 0/1 integer values!
103 * @param string $uid The user_id
104 * @param string $family The category of the configuration value
105 * @param string $key The configuration key to set
106 * @param mixed $value The value to store
108 * @return bool Operation success
110 public static function set($uid, $family, $key, $value)
112 if (!isset(self::$adapter)) {
113 return self::$cache->setP($uid, $family, $key, $value);
116 return self::$adapter->set($uid, $family, $key, $value);
120 * @brief Deletes the given key from the users's configuration.
122 * Removes the configured value from the stored cache in self::$config
123 * (@see ConfigCache ) and removes it from the database (@see IConfigAdapter )
124 * with the given $uid.
126 * @param string $uid The user_id
127 * @param string $family The category of the configuration value
128 * @param string $key The configuration key to delete
132 public static function delete($uid, $family, $key)
134 if (!isset(self::$adapter)) {
135 return self::$cache->deleteP($uid, $family, $key);
138 return self::$adapter->delete($uid, $family, $key);