]> git.mxchange.org Git - friendica.git/blob - src/Core/Config/PConfiguration.php
cf7ef6adca005e6cc74123b696e703d402d2936e
[friendica.git] / src / Core / Config / PConfiguration.php
1 <?php
2
3 namespace Friendica\Core\Config;
4
5 /**
6  * This class is responsible for the user-specific configuration values in Friendica
7  * The values are set through the Config-DB-Table (per Config-DB-adapter @see Adapter\IPConfigAdapter )
8  *
9  * The configuration cache (@see Cache\IPConfigCache ) is used for temporary caching of database calls. This will
10  * increase the performance.
11  */
12 class PConfiguration
13 {
14         /**
15          * @var Cache\IPConfigCache
16          */
17         private $configCache;
18
19         /**
20          * @var Adapter\IPConfigAdapter
21          */
22         private $configAdapter;
23
24         /**
25          * @param Cache\IPConfigCache     $configCache   The configuration cache
26          * @param Adapter\IPConfigAdapter $configAdapter The configuration DB-backend
27          */
28         public function __construct(Cache\IPConfigCache $configCache, Adapter\IPConfigAdapter $configAdapter)
29         {
30                 $this->configCache = $configCache;
31                 $this->configAdapter = $configAdapter;
32         }
33
34         /**
35          * @brief Loads all configuration values of a user's config family into a cached storage.
36          *
37          * All configuration values of the given user are stored with the $uid in
38          * the cache ( @see IPConfigCache )
39          *
40          * @param string $uid The user_id
41          * @param string $cat The category of the configuration value
42          *
43          * @return void
44          */
45         public function load($uid, $cat = 'config')
46         {
47                 // If not connected, do nothing
48                 if (!$this->configAdapter->isConnected()) {
49                         return;
50                 }
51
52                 // load the whole category out of the DB into the cache
53                 $this->configCache->loadP($uid, $this->configAdapter->load($uid, $cat));
54         }
55
56         /**
57          * @brief Get a particular user's config variable given the category name
58          * ($cat) and a key.
59          *
60          * Get a particular user's config value from the given category ($cat)
61          * and the $key with the $uid from a cached storage either from the $this->configAdapter
62          * (@see IConfigAdapter ) or from the $this->configCache (@see IConfigCache ).
63          *
64          * @param string  $uid           The user_id
65          * @param string  $cat           The category of the configuration value
66          * @param string  $key           The configuration key to query
67          * @param mixed   $default_value optional, The value to return if key is not set (default: null)
68          * @param boolean $refresh       optional, If true the config is loaded from the db and not from the cache (default: false)
69          *
70          * @return mixed Stored value or null if it does not exist
71          */
72         public function get($uid, $cat, $key, $default_value = null, $refresh = false)
73         {
74                 // if the value isn't loaded or refresh is needed, load it to the cache
75                 if ($this->configAdapter->isConnected() &&
76                         (!$this->configAdapter->isLoaded($uid, $cat, $key) ||
77                                 $refresh)) {
78                         $dbValue = $this->configAdapter->get($uid, $cat, $key);
79
80                         if ($dbValue !== '!<unset>!') {
81                                 $this->configCache->setP($uid, $cat, $key, $dbValue);
82                         }
83                 }
84
85                 // use the config cache for return
86                 if ($this->configCache->hasP($uid, $cat, $key)) {
87                         return $this->configCache->getP($uid, $cat, $key);
88                 } else {
89                         return $default_value;
90                 }
91         }
92
93         /**
94          * @brief Sets a configuration value for a user
95          *
96          * Stores a config value ($value) in the category ($family) under the key ($key)
97          * for the user_id $uid.
98          *
99          * @note  Please do not store booleans - convert to 0/1 integer values!
100          *
101          * @param string $uid    The user_id
102          * @param string $cat    The category of the configuration value
103          * @param string $key    The configuration key to set
104          * @param mixed  $value  The value to store
105          *
106          * @return bool Operation success
107          */
108         public function set($uid, $cat, $key, $value)
109         {
110                 // set the cache first
111                 $cached = $this->configCache->setP($uid, $cat, $key, $value);
112
113                 // If there is no connected adapter, we're finished
114                 if (!$this->configAdapter->isConnected()) {
115                         return $cached;
116                 }
117
118                 $stored = $this->configAdapter->set($uid, $cat, $key, $value);
119
120                 return $cached && $stored;
121         }
122
123         /**
124          * @brief Deletes the given key from the users's configuration.
125          *
126          * Removes the configured value from the stored cache in $this->configCache
127          * (@see ConfigCache ) and removes it from the database (@see IConfigAdapter )
128          * with the given $uid.
129          *
130          * @param string $uid The user_id
131          * @param string $cat The category of the configuration value
132          * @param string $key The configuration key to delete
133          *
134          * @return bool
135          */
136         public function delete($uid, $cat, $key)
137         {
138                 $cacheRemoved = $this->configCache->deleteP($uid, $cat, $key);
139
140                 if (!$this->configAdapter->isConnected()) {
141                         return $cacheRemoved;
142                 }
143
144                 $storeRemoved = $this->configAdapter->delete($uid, $cat, $key);
145
146                 return $cacheRemoved || $storeRemoved;
147         }
148 }