3 namespace BookStack\Settings;
5 use BookStack\Auth\User;
6 use Illuminate\Contracts\Cache\Repository as Cache;
10 * The settings are a simple key-value database store.
11 * For non-authenticated users, user settings are stored via the session instead.
15 protected Setting $setting;
16 protected Cache $cache;
17 protected array $localCache = [];
18 protected string $cachePrefix = 'setting-';
20 public function __construct(Setting $setting, Cache $cache)
22 $this->setting = $setting;
23 $this->cache = $cache;
27 * Gets a setting from the database,
28 * If not found, Returns default, Which is false by default.
30 public function get(string $key, $default = null)
32 if (is_null($default)) {
33 $default = config('setting-defaults.' . $key, false);
36 if (isset($this->localCache[$key])) {
37 return $this->localCache[$key];
40 $value = $this->getValueFromStore($key) ?? $default;
41 $formatted = $this->formatValue($value, $default);
42 $this->localCache[$key] = $formatted;
48 * Get a value from the session instead of the main store option.
50 protected function getFromSession(string $key, $default = false)
52 $value = session()->get($key, $default);
54 return $this->formatValue($value, $default);
58 * Get a user-specific setting from the database or cache.
60 public function getUser(User $user, string $key, $default = null)
62 if (is_null($default)) {
63 $default = config('setting-defaults.user.' . $key, false);
66 if ($user->isDefault()) {
67 return $this->getFromSession($key, $default);
70 return $this->get($this->userKey($user->id, $key), $default);
74 * Get a value for the current logged-in user.
76 public function getForCurrentUser(string $key, $default = null)
78 return $this->getUser(user(), $key, $default);
82 * Gets a setting value from the cache or database.
83 * Looks at the system defaults if not cached or in database.
84 * Returns null if nothing is found.
86 protected function getValueFromStore(string $key)
89 $cacheKey = $this->cachePrefix . $key;
90 $cacheVal = $this->cache->get($cacheKey, null);
91 if ($cacheVal !== null) {
96 $settingObject = $this->getSettingObjectByKey($key);
97 if ($settingObject !== null) {
98 $value = $settingObject->value;
100 if ($settingObject->type === 'array') {
101 $value = json_decode($value, true) ?? [];
104 $this->cache->forever($cacheKey, $value);
113 * Clear an item from the cache completely.
115 protected function clearFromCache(string $key)
117 $cacheKey = $this->cachePrefix . $key;
118 $this->cache->forget($cacheKey);
119 if (isset($this->localCache[$key])) {
120 unset($this->localCache[$key]);
125 * Format a settings value.
127 protected function formatValue($value, $default)
129 // Change string booleans to actual booleans
130 if ($value === 'true') {
132 } elseif ($value === 'false') {
136 // Set to default if empty
145 * Checks if a setting exists.
147 public function has(string $key): bool
149 $setting = $this->getSettingObjectByKey($key);
151 return $setting !== null;
155 * Add a setting to the database.
156 * Values can be an array or a string.
158 public function put(string $key, $value): bool
160 $setting = $this->setting->newQuery()->firstOrNew([
161 'setting_key' => $key,
163 $setting->type = 'string';
165 if (is_array($value)) {
166 $setting->type = 'array';
167 $value = $this->formatArrayValue($value);
170 $setting->value = $value;
172 $this->clearFromCache($key);
178 * Format an array to be stored as a setting.
179 * Array setting types are expected to be a flat array of child key=>value array items.
180 * This filters out any child items that are empty.
182 protected function formatArrayValue(array $value): string
184 $values = collect($value)->values()->filter(function (array $item) {
185 return count(array_filter($item)) > 0;
188 return json_encode($values);
192 * Put a user-specific setting into the database.
193 * Can only take string value types since this may use
194 * the session which is less flexible to data types.
196 public function putUser(User $user, string $key, string $value): bool
198 if ($user->isDefault()) {
199 session()->put($key, $value);
204 return $this->put($this->userKey($user->id, $key), $value);
208 * Put a user-specific setting into the database for the current access user.
209 * Can only take string value types since this may use
210 * the session which is less flexible to data types.
212 public function putForCurrentUser(string $key, string $value)
214 return $this->putUser(user(), $key, $value);
218 * Convert a setting key into a user-specific key.
220 protected function userKey(string $userId, string $key = ''): string
222 return 'user:' . $userId . ':' . $key;
226 * Removes a setting from the database.
228 public function remove(string $key): void
230 $setting = $this->getSettingObjectByKey($key);
234 $this->clearFromCache($key);
238 * Delete settings for a given user id.
240 public function deleteUserSettings(string $userId)
242 return $this->setting->newQuery()
243 ->where('setting_key', 'like', $this->userKey($userId) . '%')
248 * Gets a setting model from the database for the given key.
250 protected function getSettingObjectByKey(string $key): ?Setting
252 return $this->setting->newQuery()
253 ->where('setting_key', '=', $key)->first();