1 <?php namespace BookStack\Settings;
3 use BookStack\Auth\User;
4 use Illuminate\Contracts\Cache\Repository as Cache;
8 * The settings are a simple key-value database store.
9 * For non-authenticated users, user settings are stored via the session instead.
15 protected $localCache = [];
17 protected $cachePrefix = 'setting-';
20 * SettingService constructor.
22 public function __construct(Setting $setting, Cache $cache)
24 $this->setting = $setting;
25 $this->cache = $cache;
29 * Gets a setting from the database,
30 * If not found, Returns default, Which is false by default.
32 public function get(string $key, $default = false)
34 if ($default === false) {
35 $default = config('setting-defaults.' . $key, false);
38 if (isset($this->localCache[$key])) {
39 return $this->localCache[$key];
42 $value = $this->getValueFromStore($key) ?? $default;
43 $formatted = $this->formatValue($value, $default);
44 $this->localCache[$key] = $formatted;
49 * Get a value from the session instead of the main store option.
51 protected function getFromSession(string $key, $default = false)
53 $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 = false)
62 if ($user->isDefault()) {
63 return $this->getFromSession($key, $default);
65 return $this->get($this->userKey($user->id, $key), $default);
69 * Get a value for the current logged-in user.
71 public function getForCurrentUser(string $key, $default = false)
73 return $this->getUser(user(), $key, $default);
77 * Gets a setting value from the cache or database.
78 * Looks at the system defaults if not cached or in database.
79 * Returns null if nothing is found.
81 protected function getValueFromStore(string $key)
84 $cacheKey = $this->cachePrefix . $key;
85 $cacheVal = $this->cache->get($cacheKey, null);
86 if ($cacheVal !== null) {
91 $settingObject = $this->getSettingObjectByKey($key);
92 if ($settingObject !== null) {
93 $value = $settingObject->value;
95 if ($settingObject->type === 'array') {
96 $value = json_decode($value, true) ?? [];
99 $this->cache->forever($cacheKey, $value);
107 * Clear an item from the cache completely.
109 protected function clearFromCache(string $key)
111 $cacheKey = $this->cachePrefix . $key;
112 $this->cache->forget($cacheKey);
113 if (isset($this->localCache[$key])) {
114 unset($this->localCache[$key]);
119 * Format a settings value
121 protected function formatValue($value, $default)
123 // Change string booleans to actual booleans
124 if ($value === 'true') {
126 } else if ($value === 'false') {
130 // Set to default if empty
138 * Checks if a setting exists.
140 public function has(string $key): bool
142 $setting = $this->getSettingObjectByKey($key);
143 return $setting !== null;
147 * Add a setting to the database.
148 * Values can be an array or a string.
150 public function put(string $key, $value): bool
152 $setting = $this->setting->newQuery()->firstOrNew([
153 'setting_key' => $key
155 $setting->type = 'string';
157 if (is_array($value)) {
158 $setting->type = 'array';
159 $value = $this->formatArrayValue($value);
162 $setting->value = $value;
164 $this->clearFromCache($key);
169 * Format an array to be stored as a setting.
170 * Array setting types are expected to be a flat array of child key=>value array items.
171 * This filters out any child items that are empty.
173 protected function formatArrayValue(array $value): string
175 $values = collect($value)->values()->filter(function(array $item) {
176 return count(array_filter($item)) > 0;
178 return json_encode($values);
182 * Put a user-specific setting into the database.
184 public function putUser(User $user, string $key, string $value): bool
186 if ($user->isDefault()) {
187 session()->put($key, $value);
191 return $this->put($this->userKey($user->id, $key), $value);
195 * Convert a setting key into a user-specific key.
197 protected function userKey(string $userId, string $key = ''): string
199 return 'user:' . $userId . ':' . $key;
203 * Removes a setting from the database.
205 public function remove(string $key): void
207 $setting = $this->getSettingObjectByKey($key);
211 $this->clearFromCache($key);
215 * Delete settings for a given user id.
217 public function deleteUserSettings(string $userId)
219 return $this->setting->newQuery()
220 ->where('setting_key', 'like', $this->userKey($userId) . '%')
225 * Gets a setting model from the database for the given key.
227 protected function getSettingObjectByKey(string $key): ?Setting
229 return $this->setting->newQuery()
230 ->where('setting_key', '=', $key)->first();