1 <?php namespace BookStack\Settings;
3 use Illuminate\Contracts\Cache\Repository as Cache;
7 * The settings are a simple key-value database store.
8 * For non-authenticated users, user settings are stored via the session instead.
15 protected $localCache = [];
17 protected $cachePrefix = 'setting-';
20 * SettingService constructor.
21 * @param Setting $setting
24 public function __construct(Setting $setting, Cache $cache)
26 $this->setting = $setting;
27 $this->cache = $cache;
31 * Gets a setting from the database,
32 * If not found, Returns default, Which is false by default.
34 * @param string|bool $default
37 public function get($key, $default = false)
39 if ($default === false) {
40 $default = config('setting-defaults.' . $key, false);
43 if (isset($this->localCache[$key])) {
44 return $this->localCache[$key];
47 $value = $this->getValueFromStore($key, $default);
48 $formatted = $this->formatValue($value, $default);
49 $this->localCache[$key] = $formatted;
54 * Get a value from the session instead of the main store option.
56 * @param bool $default
59 protected function getFromSession($key, $default = false)
61 $value = session()->get($key, $default);
62 $formatted = $this->formatValue($value, $default);
67 * Get a user-specific setting from the database or cache.
68 * @param \BookStack\Auth\User $user
70 * @param bool $default
73 public function getUser($user, $key, $default = false)
75 if ($user->isDefault()) {
76 return $this->getFromSession($key, $default);
78 return $this->get($this->userKey($user->id, $key), $default);
82 * Get a value for the current logged-in user.
84 * @param bool $default
87 public function getForCurrentUser($key, $default = false)
89 return $this->getUser(user(), $key, $default);
93 * Gets a setting value from the cache or database.
94 * Looks at the system defaults if not cached or in database.
99 protected function getValueFromStore($key, $default)
102 $cacheKey = $this->cachePrefix . $key;
103 $cacheVal = $this->cache->get($cacheKey, null);
104 if ($cacheVal !== null) {
108 // Check the database
109 $settingObject = $this->getSettingObjectByKey($key);
110 if ($settingObject !== null) {
111 $value = $settingObject->value;
112 $this->cache->forever($cacheKey, $value);
120 * Clear an item from the cache completely.
123 protected function clearFromCache($key)
125 $cacheKey = $this->cachePrefix . $key;
126 $this->cache->forget($cacheKey);
127 if (isset($this->localCache[$key])) {
128 unset($this->localCache[$key]);
133 * Format a settings value
138 protected function formatValue($value, $default)
140 // Change string booleans to actual booleans
141 if ($value === 'true') {
144 if ($value === 'false') {
148 // Set to default if empty
156 * Checks if a setting exists.
160 public function has($key)
162 $setting = $this->getSettingObjectByKey($key);
163 return $setting !== null;
167 * Check if a user setting is in the database.
171 public function hasUser($key)
173 return $this->has($this->userKey($key));
177 * Add a setting to the database.
182 public function put($key, $value)
184 $setting = $this->setting->firstOrNew([
185 'setting_key' => $key
187 $setting->value = $value;
189 $this->clearFromCache($key);
194 * Put a user-specific setting into the database.
195 * @param \BookStack\Auth\User $user
200 public function putUser($user, $key, $value)
202 if ($user->isDefault()) {
203 return session()->put($key, $value);
205 return $this->put($this->userKey($user->id, $key), $value);
209 * Convert a setting key into a user-specific key.
213 protected function userKey($userId, $key = '')
215 return 'user:' . $userId . ':' . $key;
219 * Removes a setting from the database.
223 public function remove($key)
225 $setting = $this->getSettingObjectByKey($key);
229 $this->clearFromCache($key);
234 * Delete settings for a given user id.
238 public function deleteUserSettings($userId)
240 return $this->setting->where('setting_key', 'like', $this->userKey($userId) . '%')->delete();
244 * Gets a setting model from the database for the given key.
248 protected function getSettingObjectByKey($key)
250 return $this->setting->where('setting_key', '=', $key)->first();