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 return $this->get($this->userKey($user->id, $key), $default);
67 * Gets a setting value from the cache or database.
68 * Looks at the system defaults if not cached or in database.
73 protected function getValueFromStore($key, $default)
75 // Check for an overriding value
76 $overrideValue = $this->getOverrideValue($key);
77 if ($overrideValue !== null) {
78 return $overrideValue;
82 $cacheKey = $this->cachePrefix . $key;
83 $cacheVal = $this->cache->get($cacheKey, null);
84 if ($cacheVal !== null) {
89 $settingObject = $this->getSettingObjectByKey($key);
90 if ($settingObject !== null) {
91 $value = $settingObject->value;
92 $this->cache->forever($cacheKey, $value);
100 * Clear an item from the cache completely.
103 protected function clearFromCache($key)
105 $cacheKey = $this->cachePrefix . $key;
106 $this->cache->forget($cacheKey);
107 if (isset($this->localCache[$key])) {
108 unset($this->localCache[$key]);
113 * Format a settings value
118 protected function formatValue($value, $default)
120 // Change string booleans to actual booleans
121 if ($value === 'true') {
124 if ($value === 'false') {
128 // Set to default if empty
136 * Checks if a setting exists.
140 public function has($key)
142 $setting = $this->getSettingObjectByKey($key);
143 return $setting !== null;
147 * Check if a user setting is in the database.
151 public function hasUser($key)
153 return $this->has($this->userKey($key));
157 * Add a setting to the database.
162 public function put($key, $value)
164 $setting = $this->setting->firstOrNew([
165 'setting_key' => $key
167 $setting->value = $value;
169 $this->clearFromCache($key);
174 * Put a user-specific setting into the database.
175 * @param \BookStack\Auth\User $user
180 public function putUser($user, $key, $value)
182 return $this->put($this->userKey($user->id, $key), $value);
186 * Convert a setting key into a user-specific key.
190 protected function userKey($userId, $key = '')
192 return 'user:' . $userId . ':' . $key;
196 * Removes a setting from the database.
200 public function remove($key)
202 $setting = $this->getSettingObjectByKey($key);
206 $this->clearFromCache($key);
211 * Delete settings for a given user id.
215 public function deleteUserSettings($userId)
217 return $this->setting->where('setting_key', 'like', $this->userKey($userId) . '%')->delete();
221 * Gets a setting model from the database for the given key.
225 protected function getSettingObjectByKey($key)
227 return $this->setting->where('setting_key', '=', $key)->first();
232 * Returns an override value for a setting based on certain app conditions.
233 * Used where certain configuration options overrule others.
234 * Returns null if no override value is available.
238 protected function getOverrideValue($key)
240 if ($key === 'registration-enabled' && config('auth.method') === 'ldap') {