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);
45 if (isset($this->localCache[$key])) {
46 return $this->localCache[$key];
49 $value = $this->getValueFromStore($key, $default);
50 $formatted = $this->formatValue($value, $default);
51 $this->localCache[$key] = $formatted;
56 * Get a user-specific setting from the database or cache.
57 * @param \BookStack\Auth\User $user
59 * @param bool $default
62 public function getUser($user, $key, $default = false)
64 return $this->get($this->userKey($user->id, $key), $default);
68 * Gets a setting value from the cache or database.
69 * Looks at the system defaults if not cached or in database.
74 protected function getValueFromStore($key, $default)
76 // Check for an overriding value
77 $overrideValue = $this->getOverrideValue($key);
78 if ($overrideValue !== null) {
79 return $overrideValue;
83 $cacheKey = $this->cachePrefix . $key;
84 $cacheVal = $this->cache->get($cacheKey, null);
85 if ($cacheVal !== null) {
90 $settingObject = $this->getSettingObjectByKey($key);
91 if ($settingObject !== null) {
92 $value = $settingObject->value;
93 $this->cache->forever($cacheKey, $value);
101 * Clear an item from the cache completely.
104 protected function clearFromCache($key)
106 $cacheKey = $this->cachePrefix . $key;
107 $this->cache->forget($cacheKey);
108 if (isset($this->localCache[$key])) {
109 unset($this->localCache[$key]);
114 * Format a settings value
119 protected function formatValue($value, $default)
121 // Change string booleans to actual booleans
122 if ($value === 'true') {
125 if ($value === 'false') {
129 // Set to default if empty
137 * Checks if a setting exists.
141 public function has($key)
143 $setting = $this->getSettingObjectByKey($key);
144 return $setting !== null;
148 * Check if a user setting is in the database.
152 public function hasUser($key)
154 return $this->has($this->userKey($key));
158 * Add a setting to the database.
163 public function put($key, $value)
165 $setting = $this->setting->firstOrNew([
166 'setting_key' => $key
168 $setting->value = $value;
170 $this->clearFromCache($key);
175 * Put a user-specific setting into the database.
176 * @param \BookStack\Auth\User $user
181 public function putUser($user, $key, $value)
183 return $this->put($this->userKey($user->id, $key), $value);
187 * Convert a setting key into a user-specific key.
191 protected function userKey($userId, $key = '')
193 return 'user:' . $userId . ':' . $key;
197 * Removes a setting from the database.
201 public function remove($key)
203 $setting = $this->getSettingObjectByKey($key);
207 $this->clearFromCache($key);
212 * Delete settings for a given user id.
216 public function deleteUserSettings($userId)
218 return $this->setting->where('setting_key', 'like', $this->userKey($userId) . '%')->delete();
222 * Gets a setting model from the database for the given key.
226 protected function getSettingObjectByKey($key)
228 return $this->setting->where('setting_key', '=', $key)->first();
233 * Returns an override value for a setting based on certain app conditions.
234 * Used where certain configuration options overrule others.
235 * Returns null if no override value is available.
239 protected function getOverrideValue($key)
241 if ($key === 'registration-enabled' && config('auth.method') === 'ldap') {