]> git.mxchange.org Git - friendica.git/blob - src/Core/Config.php
Fix PHPDoc comments project-wide
[friendica.git] / src / Core / Config.php
1 <?php
2 /**
3  * System Configuration Class
4  *
5  * @file include/Core/Config.php
6  *
7  * @brief Contains the class with methods for system configuration
8  */
9 namespace Friendica\Core;
10
11 use Friendica\App;
12 use Friendica\BaseObject;
13 use Friendica\Core\Config;
14
15 /**
16  * @brief Arbitrary system configuration storage
17  *
18  * Note:
19  * If we ever would decide to return exactly the variable type as entered,
20  * we will have fun with the additional features. :-)
21  */
22 class Config extends BaseObject
23 {
24         /**
25          * @var \Friendica\Core\Config\IConfigAdapter
26          */
27         private static $adapter = null;
28
29         public static function init()
30         {
31                 // Database isn't ready or populated yet
32                 if (!self::getApp()->getMode()->has(App\Mode::DBCONFIGAVAILABLE)) {
33                         return;
34                 }
35
36                 if (self::getApp()->getConfigValue('system', 'config_adapter') == 'preload') {
37                         self::$adapter = new Config\PreloadConfigAdapter();
38                 } else {
39                         self::$adapter = new Config\JITConfigAdapter();
40                 }
41         }
42
43         /**
44          * @brief Loads all configuration values of family into a cached storage.
45          *
46          * All configuration values of the system are stored in global cache
47          * which is available under the global variable $a->config
48          *
49          * @param string $family The category of the configuration value
50          *
51          * @return void
52          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
53          */
54         public static function load($family = "config")
55         {
56                 // Database isn't ready or populated yet
57                 if (!self::getApp()->getMode()->has(App\Mode::DBCONFIGAVAILABLE)) {
58                         return;
59                 }
60
61                 if (empty(self::$adapter)) {
62                         self::init();
63                 }
64
65                 self::$adapter->load($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 config value from the given category ($family)
73          * and the $key from a cached storage in $a->config[$uid].
74          * $instore is only used by the set_config function
75          * to determine if the key already exists in the DB
76          * If a key is found in the DB but doesn't exist in
77          * local config cache, pull it into the cache so we don't have
78          * to hit the DB again for this item.
79          *
80          * @param string  $family        The category of the configuration value
81          * @param string  $key           The configuration key to query
82          * @param mixed   $default_value optional, The value to return if key is not set (default: null)
83          * @param boolean $refresh       optional, If true the config is loaded from the db and not from the cache (default: false)
84          *
85          * @return mixed Stored value or null if it does not exist
86          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
87          */
88         public static function get($family, $key, $default_value = null, $refresh = false)
89         {
90                 // Database isn't ready or populated yet, fallback to file config
91                 if (!self::getApp()->getMode()->has(App\Mode::DBCONFIGAVAILABLE)) {
92                         return self::getApp()->getConfigValue($family, $key, $default_value);
93                 }
94
95                 if (empty(self::$adapter)) {
96                         self::init();
97                 }
98
99                 return self::$adapter->get($family, $key, $default_value, $refresh);
100         }
101
102         /**
103          * @brief Sets a configuration value for system config
104          *
105          * Stores a config value ($value) in the category ($family) under the key ($key)
106          * for the user_id $uid.
107          *
108          * Note: Please do not store booleans - convert to 0/1 integer values!
109          *
110          * @param string $family The category of the configuration value
111          * @param string $key    The configuration key to set
112          * @param mixed  $value  The value to store
113          *
114          * @return bool Operation success
115          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
116          */
117         public static function set($family, $key, $value)
118         {
119                 // Database isn't ready or populated yet
120                 if (!self::getApp()->getMode()->has(App\Mode::DBCONFIGAVAILABLE)) {
121                         return false;
122                 }
123
124                 if (empty(self::$adapter)) {
125                         self::init();
126                 }
127
128                 return self::$adapter->set($family, $key, $value);
129         }
130
131         /**
132          * @brief Deletes the given key from the system configuration.
133          *
134          * Removes the configured value from the stored cache in $a->config
135          * and removes it from the database.
136          *
137          * @param string $family The category of the configuration value
138          * @param string $key    The configuration key to delete
139          *
140          * @return mixed
141          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
142          */
143         public static function delete($family, $key)
144         {
145                 // Database isn't ready or populated yet
146                 if (!self::getApp()->getMode()->has(App\Mode::DBCONFIGAVAILABLE)) {
147                         return false;
148                 }
149
150                 if (empty(self::$adapter)) {
151                         self::init();
152                 }
153
154                 return self::$adapter->delete($family, $key);
155         }
156 }