]> git.mxchange.org Git - friendica.git/blob - src/Core/Config.php
Rework App modes
[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\BaseObject;
12 use Friendica\Core\Config;
13
14 require_once 'include/dba.php';
15
16 /**
17  * @brief Arbitrary system configuration storage
18  *
19  * Note:
20  * If we ever would decide to return exactly the variable type as entered,
21  * we will have fun with the additional features. :-)
22  */
23 class Config extends BaseObject
24 {
25         /**
26          * @var Friendica\Core\Config\IConfigAdapter
27          */
28         private static $adapter = null;
29
30         public static function init()
31         {
32                 // Database isn't ready or populated yet
33                 if (!(self::getApp()->mode & \Friendica\App::MODE_DBCONFIGAVAILABLE)) {
34                         return;
35                 }
36
37                 if (self::getApp()->getConfigValue('system', 'config_adapter') == 'preload') {
38                         self::$adapter = new Config\PreloadConfigAdapter();
39                 } else {
40                         self::$adapter = new Config\JITConfigAdapter();
41                 }
42         }
43
44         /**
45          * @brief Loads all configuration values of family into a cached storage.
46          *
47          * All configuration values of the system are stored in global cache
48          * which is available under the global variable $a->config
49          *
50          * @param string $family The category of the configuration value
51          *
52          * @return void
53          */
54         public static function load($family = "config")
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();
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          */
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()->mode & \Friendica\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 mixed Stored $value or false if the database update failed
114          */
115         public static function set($family, $key, $value)
116         {
117                 // Database isn't ready or populated yet
118                 if (!(self::getApp()->mode & \Friendica\App::MODE_DBCONFIGAVAILABLE)) {
119                         return false;
120                 }
121
122                 if (empty(self::$adapter)) {
123                         self::init();
124                 }
125
126                 return self::$adapter->set($family, $key, $value);
127         }
128
129         /**
130          * @brief Deletes the given key from the system configuration.
131          *
132          * Removes the configured value from the stored cache in $a->config
133          * and removes it from the database.
134          *
135          * @param string $family The category of the configuration value
136          * @param string $key    The configuration key to delete
137          *
138          * @return mixed
139          */
140         public static function delete($family, $key)
141         {
142                 // Database isn't ready or populated yet
143                 if (!(self::getApp()->mode & \Friendica\App::MODE_DBCONFIGAVAILABLE)) {
144                         return false;
145                 }
146
147                 if (empty(self::$adapter)) {
148                         self::init();
149                 }
150
151                 return self::$adapter->delete($family, $key);
152         }
153 }