]> git.mxchange.org Git - friendica.git/blob - src/Core/Config.php
Update Config class with adapter
[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                 $a = self::getApp();
33
34                 if (isset($a->config['system']['config_adapter']) && $a->config['system']['config_adapter'] == 'preload') {
35                         self::$adapter = new Config\PreloadConfigAdapter();
36                 } else {
37                         self::$adapter = new Config\JITConfigAdapter();
38                 }
39         }
40
41         /**
42          * @brief Loads all configuration values of family into a cached storage.
43          *
44          * All configuration values of the system are stored in global cache
45          * which is available under the global variable $a->config
46          *
47          * @param string $family The category of the configuration value
48          *
49          * @return void
50          */
51         public static function load($family = "config")
52         {
53                 if (empty(self::$adapter)) {
54                         self::init();
55                 }
56
57                 self::$adapter->load($family);
58         }
59
60         /**
61          * @brief Get a particular user's config variable given the category name
62          * ($family) and a key.
63          *
64          * Get a particular config value from the given category ($family)
65          * and the $key from a cached storage in $a->config[$uid].
66          * $instore is only used by the set_config function
67          * to determine if the key already exists in the DB
68          * If a key is found in the DB but doesn't exist in
69          * local config cache, pull it into the cache so we don't have
70          * to hit the DB again for this item.
71          *
72          * @param string  $family        The category of the configuration value
73          * @param string  $key           The configuration key to query
74          * @param mixed   $default_value optional, The value to return if key is not set (default: null)
75          * @param boolean $refresh       optional, If true the config is loaded from the db and not from the cache (default: false)
76          *
77          * @return mixed Stored value or null if it does not exist
78          */
79         public static function get($family, $key, $default_value = null, $refresh = false)
80         {
81                 if (empty(self::$adapter)) {
82                         self::init();
83                 }
84
85                 return self::$adapter->get($family, $key, $default_value, $refresh);
86         }
87
88         /**
89          * @brief Sets a configuration value for system config
90          *
91          * Stores a config value ($value) in the category ($family) under the key ($key)
92          * for the user_id $uid.
93          *
94          * Note: Please do not store booleans - convert to 0/1 integer values!
95          *
96          * @param string $family The category of the configuration value
97          * @param string $key    The configuration key to set
98          * @param mixed  $value  The value to store
99          *
100          * @return mixed Stored $value or false if the database update failed
101          */
102         public static function set($family, $key, $value)
103         {
104                 if (empty(self::$adapter)) {
105                         self::init();
106                 }
107
108                 return self::$adapter->set($family, $key, $value);
109         }
110
111         /**
112          * @brief Deletes the given key from the system configuration.
113          *
114          * Removes the configured value from the stored cache in $a->config
115          * and removes it from the database.
116          *
117          * @param string $family The category of the configuration value
118          * @param string $key    The configuration key to delete
119          *
120          * @return mixed
121          */
122         public static function delete($family, $key)
123         {
124                 if (empty(self::$adapter)) {
125                         self::init();
126                 }
127
128                 return self::$adapter->delete($family, $key);
129         }
130 }