3 namespace BookStack\Settings;
5 use BookStack\Users\Models\User;
9 * The settings are a simple key-value database store.
10 * For non-authenticated users, user settings are stored via the session instead.
11 * A local array-based cache is used to for setting accesses across a request.
15 protected array $localCache = [];
18 * Gets a setting from the database,
19 * If not found, Returns default, Which is false by default.
21 public function get(string $key, $default = null): mixed
23 if (is_null($default)) {
24 $default = config('setting-defaults.' . $key, false);
27 $value = $this->getValueFromStore($key) ?? $default;
28 return $this->formatValue($value, $default);
32 * Get a value from the session instead of the main store option.
34 protected function getFromSession(string $key, $default = false)
36 $value = session()->get($key, $default);
38 return $this->formatValue($value, $default);
42 * Get a user-specific setting from the database or cache.
44 public function getUser(User $user, string $key, $default = null)
46 if (is_null($default)) {
47 $default = config('setting-defaults.user.' . $key, false);
50 if ($user->isDefault()) {
51 return $this->getFromSession($key, $default);
54 return $this->get($this->userKey($user->id, $key), $default);
58 * Get a value for the current logged-in user.
60 public function getForCurrentUser(string $key, $default = null)
62 return $this->getUser(user(), $key, $default);
66 * Gets a setting value from the local cache.
67 * Will load the local cache if not previously loaded.
69 protected function getValueFromStore(string $key): mixed
71 $cacheCategory = $this->localCacheCategory($key);
72 if (!isset($this->localCache[$cacheCategory])) {
73 $this->loadToLocalCache($cacheCategory);
76 return $this->localCache[$cacheCategory][$key] ?? null;
80 * Put the given value into the local cached under the given key.
82 protected function putValueIntoLocalCache(string $key, mixed $value): void
84 $cacheCategory = $this->localCacheCategory($key);
85 if (!isset($this->localCache[$cacheCategory])) {
86 $this->loadToLocalCache($cacheCategory);
89 $this->localCache[$cacheCategory][$key] = $value;
93 * Get the category for the given setting key.
94 * Will return 'app' for a general app setting otherwise 'user:<user_id>' for a user setting.
96 protected function localCacheCategory(string $key): string
98 if (str_starts_with($key, 'user:')) {
99 return implode(':', array_slice(explode(':', $key), 0, 2));
106 * For the given category, load the relevant settings from the database into the local cache.
108 protected function loadToLocalCache(string $cacheCategory): void
110 $query = Setting::query();
112 if ($cacheCategory === 'app') {
113 $query->where('setting_key', 'not like', 'user:%');
115 $query->where('setting_key', 'like', $cacheCategory . ':%');
117 $settings = $query->toBase()->get();
119 if (!isset($this->localCache[$cacheCategory])) {
120 $this->localCache[$cacheCategory] = [];
123 foreach ($settings as $setting) {
124 $value = $setting->value;
126 if ($setting->type === 'array') {
127 $value = json_decode($value, true) ?? [];
130 $this->localCache[$cacheCategory][$setting->setting_key] = $value;
135 * Format a settings value.
137 protected function formatValue(mixed $value, mixed $default): mixed
139 // Change string booleans to actual booleans
140 if ($value === 'true') {
142 } elseif ($value === 'false') {
146 // Set to default if empty
155 * Checks if a setting exists.
157 public function has(string $key): bool
159 $setting = $this->getSettingObjectByKey($key);
161 return $setting !== null;
165 * Add a setting to the database.
166 * Values can be an array or a string.
168 public function put(string $key, mixed $value): bool
170 $setting = Setting::query()->firstOrNew([
171 'setting_key' => $key,
174 $setting->type = 'string';
175 $setting->value = $value;
177 if (is_array($value)) {
178 $setting->type = 'array';
179 $setting->value = $this->formatArrayValue($value);
183 $this->putValueIntoLocalCache($key, $value);
189 * Format an array to be stored as a setting.
190 * Array setting types are expected to be a flat array of child key=>value array items.
191 * This filters out any child items that are empty.
193 protected function formatArrayValue(array $value): string
195 $values = collect($value)->values()->filter(function (array $item) {
196 return count(array_filter($item)) > 0;
199 return json_encode($values);
203 * Put a user-specific setting into the database.
204 * Can only take string value types since this may use
205 * the session which is less flexible to data types.
207 public function putUser(User $user, string $key, string $value): bool
209 if ($user->isDefault()) {
210 session()->put($key, $value);
215 return $this->put($this->userKey($user->id, $key), $value);
219 * Put a user-specific setting into the database for the current access user.
220 * Can only take string value types since this may use
221 * the session which is less flexible to data types.
223 public function putForCurrentUser(string $key, string $value): bool
225 return $this->putUser(user(), $key, $value);
229 * Convert a setting key into a user-specific key.
231 protected function userKey(string $userId, string $key = ''): string
233 return 'user:' . $userId . ':' . $key;
237 * Removes a setting from the database.
239 public function remove(string $key): void
241 $setting = $this->getSettingObjectByKey($key);
246 $cacheCategory = $this->localCacheCategory($key);
247 if (isset($this->localCache[$cacheCategory])) {
248 unset($this->localCache[$cacheCategory][$key]);
253 * Delete settings for a given user id.
255 public function deleteUserSettings(string $userId): void
258 ->where('setting_key', 'like', $this->userKey($userId) . '%')
263 * Gets a setting model from the database for the given key.
265 protected function getSettingObjectByKey(string $key): ?Setting
267 return Setting::query()
268 ->where('setting_key', '=', $key)
273 * Empty the local setting value cache used by this service.
275 public function flushCache(): void
277 $this->localCache = [];