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.
17 protected $localCache = [];
19 protected $cachePrefix = 'setting-';
22 * SettingService constructor.
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 public function get(string $key, $default = null)
36 if (is_null($default)) {
37 $default = config('setting-defaults.' . $key, false);
40 if (isset($this->localCache[$key])) {
41 return $this->localCache[$key];
44 $value = $this->getValueFromStore($key) ?? $default;
45 $formatted = $this->formatValue($value, $default);
46 $this->localCache[$key] = $formatted;
52 * Get a value from the session instead of the main store option.
54 protected function getFromSession(string $key, $default = false)
56 $value = session()->get($key, $default);
58 return $this->formatValue($value, $default);
62 * Get a user-specific setting from the database or cache.
64 public function getUser(User $user, string $key, $default = null)
66 if (is_null($default)) {
67 $default = config('setting-defaults.user.' . $key, false);
70 if ($user->isDefault()) {
71 return $this->getFromSession($key, $default);
74 return $this->get($this->userKey($user->id, $key), $default);
78 * Get a value for the current logged-in user.
80 public function getForCurrentUser(string $key, $default = null)
82 return $this->getUser(user(), $key, $default);
86 * Gets a setting value from the cache or database.
87 * Looks at the system defaults if not cached or in database.
88 * Returns null if nothing is found.
90 protected function getValueFromStore(string $key)
93 $cacheKey = $this->cachePrefix . $key;
94 $cacheVal = $this->cache->get($cacheKey, null);
95 if ($cacheVal !== null) {
100 $settingObject = $this->getSettingObjectByKey($key);
101 if ($settingObject !== null) {
102 $value = $settingObject->value;
104 if ($settingObject->type === 'array') {
105 $value = json_decode($value, true) ?? [];
108 $this->cache->forever($cacheKey, $value);
117 * Clear an item from the cache completely.
119 protected function clearFromCache(string $key)
121 $cacheKey = $this->cachePrefix . $key;
122 $this->cache->forget($cacheKey);
123 if (isset($this->localCache[$key])) {
124 unset($this->localCache[$key]);
129 * Format a settings value.
131 protected function formatValue($value, $default)
133 // Change string booleans to actual booleans
134 if ($value === 'true') {
136 } elseif ($value === 'false') {
140 // Set to default if empty
149 * Checks if a setting exists.
151 public function has(string $key): bool
153 $setting = $this->getSettingObjectByKey($key);
155 return $setting !== null;
159 * Add a setting to the database.
160 * Values can be an array or a string.
162 public function put(string $key, $value): bool
164 $setting = $this->setting->newQuery()->firstOrNew([
165 'setting_key' => $key,
167 $setting->type = 'string';
169 if (is_array($value)) {
170 $setting->type = 'array';
171 $value = $this->formatArrayValue($value);
174 $setting->value = $value;
176 $this->clearFromCache($key);
182 * Format an array to be stored as a setting.
183 * Array setting types are expected to be a flat array of child key=>value array items.
184 * This filters out any child items that are empty.
186 protected function formatArrayValue(array $value): string
188 $values = collect($value)->values()->filter(function (array $item) {
189 return count(array_filter($item)) > 0;
192 return json_encode($values);
196 * Put a user-specific setting into the database.
198 public function putUser(User $user, string $key, string $value): bool
200 if ($user->isDefault()) {
201 session()->put($key, $value);
206 return $this->put($this->userKey($user->id, $key), $value);
210 * Convert a setting key into a user-specific key.
212 protected function userKey(string $userId, string $key = ''): string
214 return 'user:' . $userId . ':' . $key;
218 * Removes a setting from the database.
220 public function remove(string $key): void
222 $setting = $this->getSettingObjectByKey($key);
226 $this->clearFromCache($key);
230 * Delete settings for a given user id.
232 public function deleteUserSettings(string $userId)
234 return $this->setting->newQuery()
235 ->where('setting_key', 'like', $this->userKey($userId) . '%')
240 * Gets a setting model from the database for the given key.
242 protected function getSettingObjectByKey(string $key): ?Setting
244 return $this->setting->newQuery()
245 ->where('setting_key', '=', $key)->first();