1 <?php namespace BookStack\Services;
4 use Illuminate\Contracts\Cache\Repository as Cache;
9 * The settings are a simple key-value database store.
11 * @package BookStack\Services
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 $value = $this->getValueFromStore($key, $default);
42 return $this->formatValue($value, $default);
46 * Gets a setting value from the cache or database.
51 protected function getValueFromStore($key, $default)
53 $overrideValue = $this->getOverrideValue($key);
54 if ($overrideValue !== null) return $overrideValue;
56 $cacheKey = $this->cachePrefix . $key;
57 if ($this->cache->has($cacheKey)) {
58 return $this->cache->get($cacheKey);
61 $settingObject = $this->getSettingObjectByKey($key);
63 if ($settingObject !== null) {
64 $value = $settingObject->value;
65 $this->cache->forever($cacheKey, $value);
73 * Clear an item from the cache completely.
76 protected function clearFromCache($key)
78 $cacheKey = $this->cachePrefix . $key;
79 $this->cache->forget($cacheKey);
83 * Format a settings value
88 protected function formatValue($value, $default)
90 // Change string booleans to actual booleans
91 if ($value === 'true') $value = true;
92 if ($value === 'false') $value = false;
94 // Set to default if empty
95 if ($value === '') $value = $default;
100 * Checks if a setting exists.
104 public function has($key)
106 $setting = $this->getSettingObjectByKey($key);
107 return $setting !== null;
111 * Add a setting to the database.
116 public function put($key, $value)
118 $setting = $this->setting->firstOrNew([
119 'setting_key' => $key
121 $setting->value = $value;
123 $this->clearFromCache($key);
128 * Removes a setting from the database.
132 public function remove($key)
134 $setting = $this->getSettingObjectByKey($key);
138 $this->clearFromCache($key);
143 * Gets a setting model from the database for the given key.
147 protected function getSettingObjectByKey($key)
149 return $this->setting->where('setting_key', '=', $key)->first();
154 * Returns an override value for a setting based on certain app conditions.
155 * Used where certain configuration options overrule others.
156 * Returns null if no override value is available.
160 protected function getOverrideValue($key)
162 if ($key === 'registration-enabled' && config('auth.method') === 'ldap') return false;