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