]> git.mxchange.org Git - friendica.git/blob - src/Core/PConfig.php
Move *ConfigValue functions to App
[friendica.git] / src / Core / PConfig.php
1 <?php
2 /**
3  * User Configuration Class
4  *
5  * @file include/Core/PConfig.php
6  *
7  * @brief Contains the class with methods for user configuration
8  */
9 namespace Friendica\Core;
10
11 use Friendica\BaseObject;
12 use Friendica\Core\Config;
13
14 require_once 'include/dba.php';
15
16 /**
17  * @brief Management of user configuration storage
18  * Note:
19  * Please do not store booleans - convert to 0/1 integer values
20  * The PConfig::get() functions return boolean false for keys that are unset,
21  * and this could lead to subtle bugs.
22  */
23 class PConfig extends BaseObject
24 {
25         /**
26          * @var Friendica\Core\Config\IPConfigAdapter
27          */
28         private static $adapter = null;
29
30         public static function init($uid)
31         {
32                 $a = self::getApp();
33
34                 if (isset($a->config['system']['config_adapter']) && $a->config['system']['config_adapter'] == 'preload') {
35                         self::$adapter = new Config\PreloadPConfigAdapter($uid);
36                 } else {
37                         self::$adapter = new Config\JITPConfigAdapter($uid);
38                 }
39         }
40
41         /**
42          * @brief Loads all configuration values of a user's config family into a cached storage.
43          *
44          * All configuration values of the given user are stored in global cache
45          * which is available under the global variable $a->config[$uid].
46          *
47          * @param string $uid    The user_id
48          * @param string $family The category of the configuration value
49          *
50          * @return void
51          */
52         public static function load($uid, $family)
53         {
54                 if (empty(self::$adapter)) {
55                         self::init($uid);
56                 }
57
58                 self::$adapter->load($uid, $family);
59         }
60
61         /**
62          * @brief Get a particular user's config variable given the category name
63          * ($family) and a key.
64          *
65          * Get a particular user's config value from the given category ($family)
66          * and the $key from a cached storage in $a->config[$uid].
67          *
68          * @param string  $uid           The user_id
69          * @param string  $family        The category of the configuration value
70          * @param string  $key           The configuration key to query
71          * @param mixed   $default_value optional, The value to return if key is not set (default: null)
72          * @param boolean $refresh       optional, If true the config is loaded from the db and not from the cache (default: false)
73          *
74          * @return mixed Stored value or null if it does not exist
75          */
76         public static function get($uid, $family, $key, $default_value = null, $refresh = false)
77         {
78                 if (empty(self::$adapter)) {
79                         self::init($uid);
80                 }
81
82                 return self::$adapter->get($uid, $family, $key, $default_value, $refresh);
83         }
84
85         /**
86          * @brief Sets a configuration value for a user
87          *
88          * Stores a config value ($value) in the category ($family) under the key ($key)
89          * for the user_id $uid.
90          *
91          * @note Please do not store booleans - convert to 0/1 integer values!
92          *
93          * @param string $uid    The user_id
94          * @param string $family The category of the configuration value
95          * @param string $key    The configuration key to set
96          * @param string $value  The value to store
97          *
98          * @return mixed Stored $value or false
99          */
100         public static function set($uid, $family, $key, $value)
101         {
102                 if (empty(self::$adapter)) {
103                         self::init($uid);
104                 }
105
106                 return self::$adapter->set($uid, $family, $key, $value);
107         }
108
109         /**
110          * @brief Deletes the given key from the users's configuration.
111          *
112          * Removes the configured value from the stored cache in $a->config[$uid]
113          * and removes it from the database.
114          *
115          * @param string $uid    The user_id
116          * @param string $family The category of the configuration value
117          * @param string $key    The configuration key to delete
118          *
119          * @return mixed
120          */
121         public static function delete($uid, $family, $key)
122         {
123                 if (empty(self::$adapter)) {
124                         self::init($uid);
125                 }
126
127                 return self::$adapter->delete($uid, $family, $key);
128         }
129 }