1 <?php namespace BookStack\Services;
5 use Illuminate\Contracts\Cache\Repository as Cache;
10 * The settings are a simple key-value database store.
12 * @package BookStack\Services
19 protected $localCache = [];
21 protected $cachePrefix = 'setting-';
24 * SettingService constructor.
25 * @param Setting $setting
28 public function __construct(Setting $setting, Cache $cache)
30 $this->setting = $setting;
31 $this->cache = $cache;
35 * Gets a setting from the database,
36 * If not found, Returns default, Which is false by default.
38 * @param string|bool $default
41 public function get($key, $default = false)
43 if ($default === false) {
44 $default = config('setting-defaults.' . $key, false);
46 if (isset($this->localCache[$key])) {
47 return $this->localCache[$key];
50 $value = $this->getValueFromStore($key, $default);
51 $formatted = $this->formatValue($value, $default);
52 $this->localCache[$key] = $formatted;
57 * Get a user-specific setting from the database or cache.
60 * @param bool $default
63 public function getUser($user, $key, $default = false)
65 return $this->get($this->userKey($user->id, $key), $default);
69 * Gets a setting value from the cache or database.
70 * Looks at the system defaults if not cached or in database.
75 protected function getValueFromStore($key, $default)
77 // Check for an overriding value
78 $overrideValue = $this->getOverrideValue($key);
79 if ($overrideValue !== null) {
80 return $overrideValue;
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;
94 $this->cache->forever($cacheKey, $value);
102 * Clear an item from the cache completely.
105 protected function clearFromCache($key)
107 $cacheKey = $this->cachePrefix . $key;
108 $this->cache->forget($cacheKey);
109 if (isset($this->localCache[$key])) {
110 unset($this->localCache[$key]);
115 * Format a settings value
120 protected function formatValue($value, $default)
122 // Change string booleans to actual booleans
123 if ($value === 'true') {
126 if ($value === 'false') {
130 // Set to default if empty
138 * Checks if a setting exists.
142 public function has($key)
144 $setting = $this->getSettingObjectByKey($key);
145 return $setting !== null;
149 * Check if a user setting is in the database.
153 public function hasUser($key)
155 return $this->has($this->userKey($key));
159 * Add a setting to the database.
164 public function put($key, $value)
166 $setting = $this->setting->firstOrNew([
167 'setting_key' => $key
169 $setting->value = $value;
171 $this->clearFromCache($key);
176 * Put a user-specific setting into the database.
182 public function putUser($user, $key, $value)
184 return $this->put($this->userKey($user->id, $key), $value);
188 * Convert a setting key into a user-specific key.
192 protected function userKey($userId, $key = '')
194 return 'user:' . $userId . ':' . $key;
198 * Removes a setting from the database.
202 public function remove($key)
204 $setting = $this->getSettingObjectByKey($key);
208 $this->clearFromCache($key);
213 * Delete settings for a given user id.
217 public function deleteUserSettings($userId)
219 return $this->setting->where('setting_key', 'like', $this->userKey($userId) . '%')->delete();
223 * Gets a setting model from the database for the given key.
227 protected function getSettingObjectByKey($key)
229 return $this->setting->where('setting_key', '=', $key)->first();
234 * Returns an override value for a setting based on certain app conditions.
235 * Used where certain configuration options overrule others.
236 * Returns null if no override value is available.
240 protected function getOverrideValue($key)
242 if ($key === 'registration-enabled' && config('auth.method') === 'ldap') {