1 <?php namespace BookStack\Settings;
3 use Illuminate\Contracts\Cache\Repository as Cache;
8 * The settings are a simple key-value database store.
10 * @package BookStack\Services
17 protected $localCache = [];
19 protected $cachePrefix = 'setting-';
22 * SettingService constructor.
23 * @param Setting $setting
26 public function __construct(Setting $setting, Cache $cache)
28 $this->setting = $setting;
29 $this->cache = $cache;
33 * Gets a setting from the database,
34 * If not found, Returns default, Which is false by default.
36 * @param string|bool $default
39 public function get($key, $default = false)
41 if ($default === false) {
42 $default = config('setting-defaults.' . $key, false);
44 if (isset($this->localCache[$key])) {
45 return $this->localCache[$key];
48 $value = $this->getValueFromStore($key, $default);
49 $formatted = $this->formatValue($value, $default);
50 $this->localCache[$key] = $formatted;
55 * Get a user-specific setting from the database or cache.
56 * @param \BookStack\Auth\User $user
58 * @param bool $default
61 public function getUser($user, $key, $default = false)
63 if ($user->isDefault()) {
64 return session()->get($key, $default);
66 return $this->get($this->userKey($user->id, $key), $default);
70 * Gets a setting value from the cache or database.
71 * Looks at the system defaults if not cached or in database.
76 protected function getValueFromStore($key, $default)
78 // Check for an overriding value
79 $overrideValue = $this->getOverrideValue($key);
80 if ($overrideValue !== null) {
81 return $overrideValue;
85 $cacheKey = $this->cachePrefix . $key;
86 $cacheVal = $this->cache->get($cacheKey, null);
87 if ($cacheVal !== null) {
92 $settingObject = $this->getSettingObjectByKey($key);
93 if ($settingObject !== null) {
94 $value = $settingObject->value;
95 $this->cache->forever($cacheKey, $value);
103 * Clear an item from the cache completely.
106 protected function clearFromCache($key)
108 $cacheKey = $this->cachePrefix . $key;
109 $this->cache->forget($cacheKey);
110 if (isset($this->localCache[$key])) {
111 unset($this->localCache[$key]);
116 * Format a settings value
121 protected function formatValue($value, $default)
123 // Change string booleans to actual booleans
124 if ($value === 'true') {
127 if ($value === 'false') {
131 // Set to default if empty
139 * Checks if a setting exists.
143 public function has($key)
145 $setting = $this->getSettingObjectByKey($key);
146 return $setting !== null;
150 * Check if a user setting is in the database.
154 public function hasUser($key)
156 return $this->has($this->userKey($key));
160 * Add a setting to the database.
165 public function put($key, $value)
167 $setting = $this->setting->firstOrNew([
168 'setting_key' => $key
170 $setting->value = $value;
172 $this->clearFromCache($key);
177 * Put a user-specific setting into the database.
178 * @param \BookStack\Auth\User $user
183 public function putUser($user, $key, $value)
185 if ($user->isDefault()) {
186 return session()->put($key, $value);
188 return $this->put($this->userKey($user->id, $key), $value);
192 * Convert a setting key into a user-specific key.
196 protected function userKey($userId, $key = '')
198 return 'user:' . $userId . ':' . $key;
202 * Removes a setting from the database.
206 public function remove($key)
208 $setting = $this->getSettingObjectByKey($key);
212 $this->clearFromCache($key);
217 * Delete settings for a given user id.
221 public function deleteUserSettings($userId)
223 return $this->setting->where('setting_key', 'like', $this->userKey($userId) . '%')->delete();
227 * Gets a setting model from the database for the given key.
231 protected function getSettingObjectByKey($key)
233 return $this->setting->where('setting_key', '=', $key)->first();
238 * Returns an override value for a setting based on certain app conditions.
239 * Used where certain configuration options overrule others.
240 * Returns null if no override value is available.
244 protected function getOverrideValue($key)
246 if ($key === 'registration-enabled' && config('auth.method') === 'ldap') {