]> git.mxchange.org Git - friendica.git/blob - src/Core/Config.php
Updating type-hints and some naming conventions
[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 /**
12  * @brief Arbitrary system configuration storage
13  *
14  * Note:
15  * If we ever would decide to return exactly the variable type as entered,
16  * we will have fun with the additional features. :-)
17  */
18 class Config
19 {
20         /**
21          * @var Config\IConfigAdapter
22          */
23         private static $adapter;
24
25         /**
26          * @var Config\IConfigCache
27          */
28         private static $config;
29
30         /**
31          * Initialize the config with only the cache
32          *
33          * @param Config\IConfigCache $config  The configuration cache
34          */
35         public static function init(Config\IConfigCache $config)
36         {
37                 self::$config  = $config;
38         }
39
40         /**
41          * Add the adapter for DB-backend
42          *
43          * @param Config\IConfigAdapter $adapter
44          */
45         public static function setAdapter(Config\IConfigAdapter $adapter)
46         {
47                 self::$adapter = $adapter;
48         }
49
50         /**
51          * @brief Loads all configuration values of family into a cached storage.
52          *
53          * All configuration values of the system are stored in global cache
54          * which is available under the global variable self::$config
55          *
56          * @param string $family The category of the configuration value
57          *
58          * @return void
59          */
60         public static function load($family = "config")
61         {
62                 if (!isset(self::$adapter)) {
63                         return;
64                 }
65
66                 self::$adapter->load($family);
67         }
68
69         /**
70          * @brief Get a particular user's config variable given the category name
71          * ($family) and a key.
72          *
73          * Get a particular config value from the given category ($family)
74          * and the $key from a cached storage in static::config[$uid].
75          * $instore is only used by the set_config function
76          * to determine if the key already exists in the DB
77          * If a key is found in the DB but doesn't exist in
78          * local config cache, pull it into the cache so we don't have
79          * to hit the DB again for this item.
80          *
81          * @param string  $family        The category of the configuration value
82          * @param string  $key           The configuration key to query
83          * @param mixed   $default_value optional, The value to return if key is not set (default: null)
84          * @param boolean $refresh       optional, If true the config is loaded from the db and not from the cache (default: false)
85          *
86          * @return mixed Stored value or null if it does not exist
87          */
88         public static function get($family, $key, $default_value = null, $refresh = false)
89         {
90                 if (!isset(self::$adapter)) {
91                         return self::$config->get($family, $key, $default_value);
92                 }
93
94                 return self::$adapter->get($family, $key, $default_value, $refresh);
95         }
96
97         /**
98          * @brief Sets a configuration value for system config
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 $family The category of the configuration value
106          * @param string $key    The configuration key to set
107          * @param mixed  $value  The value to store
108          *
109          * @return bool Operation success
110          */
111         public static function set($family, $key, $value)
112         {
113                 if (!isset(self::$adapter)) {
114                         self::$config->set($family, $key, $value);
115                         return true;
116                 }
117
118                 return self::$adapter->set($family, $key, $value);
119         }
120
121         /**
122          * @brief Deletes the given key from the system configuration.
123          *
124          * Removes the configured value from the stored cache in Config::$config
125          * and removes it from the database.
126          *
127          * @param string $family The category of the configuration value
128          * @param string $key    The configuration key to delete
129          *
130          * @return mixed
131          */
132         public static function delete($family, $key)
133         {
134                 if (!isset(self::$adapter)) {
135                         self::$config->delete($family, $key);
136                 }
137
138                 return self::$adapter->delete($family, $key);
139         }
140 }