1 <?php namespace BookStack\Services;
5 use Illuminate\Contracts\Cache\Repository as Cache;
10 * The settings are a simple key-value database store.
12 * @package BookStack\Services
19 protected $localCache = [];
21 protected $cachePrefix = 'setting-';
24 * SettingService constructor.
25 * @param Setting $setting
28 public function __construct(Setting $setting, Cache $cache)
30 $this->setting = $setting;
31 $this->cache = $cache;
35 * Gets a setting from the database,
36 * If not found, Returns default, Which is false by default.
38 * @param string|bool $default
41 public function get($key, $default = false)
43 if ($default === false) $default = config('setting-defaults.' . $key, false);
44 if (isset($this->localCache[$key])) return $this->localCache[$key];
46 $value = $this->getValueFromStore($key, $default);
47 $formatted = $this->formatValue($value, $default);
48 $this->localCache[$key] = $formatted;
53 * Get a user-specific setting from the database or cache.
56 * @param bool $default
59 public function getUser($user, $key, $default = false)
61 return $this->get($this->userKey($user->id, $key), $default);
65 * Gets a setting value from the cache or database.
66 * Looks at the system defaults if not cached or in database.
71 protected function getValueFromStore($key, $default)
73 // Check for an overriding value
74 $overrideValue = $this->getOverrideValue($key);
75 if ($overrideValue !== null) return $overrideValue;
78 $cacheKey = $this->cachePrefix . $key;
79 $cacheVal = $this->cache->get($cacheKey, null);
80 if ($cacheVal !== null) return $cacheVal;
83 $settingObject = $this->getSettingObjectByKey($key);
84 if ($settingObject !== null) {
85 $value = $settingObject->value;
86 $this->cache->forever($cacheKey, $value);
94 * Clear an item from the cache completely.
97 protected function clearFromCache($key)
99 $cacheKey = $this->cachePrefix . $key;
100 $this->cache->forget($cacheKey);
101 if (isset($this->localCache[$key])) {
102 unset($this->localCache[$key]);
107 * Format a settings value
112 protected function formatValue($value, $default)
114 // Change string booleans to actual booleans
115 if ($value === 'true') $value = true;
116 if ($value === 'false') $value = false;
118 // Set to default if empty
119 if ($value === '') $value = $default;
124 * Checks if a setting exists.
128 public function has($key)
130 $setting = $this->getSettingObjectByKey($key);
131 return $setting !== null;
135 * Check if a user setting is in the database.
139 public function hasUser($key)
141 return $this->has($this->userKey($key));
145 * Add a setting to the database.
150 public function put($key, $value)
152 $setting = $this->setting->firstOrNew([
153 'setting_key' => $key
155 $setting->value = $value;
157 $this->clearFromCache($key);
162 * Put a user-specific setting into the database.
168 public function putUser($user, $key, $value)
170 return $this->put($this->userKey($user->id, $key), $value);
174 * Convert a setting key into a user-specific key.
178 protected function userKey($userId, $key = '')
180 return 'user:' . $userId . ':' . $key;
184 * Removes a setting from the database.
188 public function remove($key)
190 $setting = $this->getSettingObjectByKey($key);
194 $this->clearFromCache($key);
199 * Delete settings for a given user id.
203 public function deleteUserSettings($userId)
205 return $this->setting->where('setting_key', 'like', $this->userKey($userId) . '%')->delete();
209 * Gets a setting model from the database for the given key.
213 protected function getSettingObjectByKey($key)
215 return $this->setting->where('setting_key', '=', $key)->first();
220 * Returns an override value for a setting based on certain app conditions.
221 * Used where certain configuration options overrule others.
222 * Returns null if no override value is available.
226 protected function getOverrideValue($key)
228 if ($key === 'registration-enabled' && config('auth.method') === 'ldap') return false;