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