3 namespace Friendica\Core\Config;
5 use Friendica\Core\Logger;
8 * This class is responsible for all system-wide configuration values in Friendica
9 * There are two types of storage
10 * - The Config-Files (loaded into the FileCache @see Cache\IConfigCache )
11 * - The Config-DB-Table (per Config-DB-adapter @see Adapter\IConfigAdapter )
16 * @var Cache\IConfigCache
21 * @var Adapter\IConfigAdapter
23 private $configAdapter;
26 * @param Cache\IConfigCache $configCache The configuration cache (based on the config-files)
27 * @param Adapter\IConfigAdapter $configAdapter The configuration DB-backend
29 public function __construct(Cache\IConfigCache $configCache, Adapter\IConfigAdapter $configAdapter)
31 $this->configCache = $configCache;
32 $this->configAdapter = $configAdapter;
38 * Returns the Config Cache
40 * @return Cache\IConfigCache
42 public function getCache()
44 return $this->configCache;
48 * @brief Loads all configuration values of family into a cached storage.
50 * All configuration values of the system are stored in the cache ( @see IConfigCache )
52 * @param string $cat The category of the configuration value
56 public function load($cat = 'config')
58 // If not connected, do nothing
59 if (!$this->configAdapter->isConnected()) {
63 // load the whole category out of the DB into the cache
64 $this->configCache->load($this->configAdapter->load($cat), true);
68 * @brief Get a particular user's config variable given the category name
71 * Get a particular config value from the given category ($cat)
72 * and the $key from a cached storage either from the $this->configAdapter
73 * (@see IConfigAdapter ) or from the $this->configCache (@see IConfigCache ).
75 * @param string $cat The category of the configuration value
76 * @param string $key The configuration key to query
77 * @param mixed $default_value optional, The value to return if key is not set (default: null)
78 * @param boolean $refresh optional, If true the config is loaded from the db and not from the cache (default: false)
80 * @return mixed Stored value or null if it does not exist
82 public function get($cat, $key, $default_value = null, $refresh = false)
84 // if the value isn't loaded or refresh is needed, load it to the cache
85 if ($this->configAdapter->isConnected() &&
86 (!$this->configAdapter->isLoaded($cat, $key) ||
88 $dbvalue = $this->configAdapter->get($cat, $key);
90 if ($dbvalue !== '!<unset>!') {
91 $this->configCache->set($cat, $key, $dbvalue);
96 // use the config cache for return
97 if ($this->configCache->has($cat, $key)) {
98 return $this->configCache->get($cat, $key);
100 return $default_value;
105 * @brief Sets a configuration value for system config
107 * Stores a config value ($value) in the category ($cat) under the key ($key)
109 * Note: Please do not store booleans - convert to 0/1 integer values!
111 * @param string $cat The category of the configuration value
112 * @param string $key The configuration key to set
113 * @param mixed $value The value to store
115 * @return bool Operation success
117 public function set($cat, $key, $value)
119 if ($key === 'last_worker_execution') {
120 Logger::alert('catchmeifyou', ['value' => $value]);
123 // set the cache first
124 $cached = $this->configCache->set($cat, $key, $value);
126 // If there is no connected adapter, we're finished
127 if (!$this->configAdapter->isConnected()) {
131 $stored = $this->configAdapter->set($cat, $key, $value);
133 return $cached && $stored;
137 * @brief Deletes the given key from the system configuration.
139 * Removes the configured value from the stored cache in $this->configCache
140 * (@see ConfigCache ) and removes it from the database (@see IConfigAdapter ).
142 * @param string $cat The category of the configuration value
143 * @param string $key The configuration key to delete
147 public function delete($cat, $key)
149 $cacheRemoved = $this->configCache->delete($cat, $key);
151 if (!$this->configAdapter->isConnected()) {
152 return $cacheRemoved;
155 $storeRemoved = $this->configAdapter->delete($cat, $key);
157 return $cacheRemoved || $storeRemoved;