]> BookStack Code Mirror - bookstack/blob - app/Services/SettingService.php
Added Swedish locale to config
[bookstack] / app / Services / SettingService.php
1 <?php namespace BookStack\Services;
2
3 use BookStack\Setting;
4 use BookStack\User;
5 use Illuminate\Contracts\Cache\Repository as Cache;
6
7 /**
8  * Class SettingService
9  *
10  * The settings are a simple key-value database store.
11  *
12  * @package BookStack\Services
13  */
14 class SettingService
15 {
16
17     protected $setting;
18     protected $cache;
19     protected $localCache = [];
20
21     protected $cachePrefix = 'setting-';
22
23     /**
24      * SettingService constructor.
25      * @param Setting $setting
26      * @param Cache   $cache
27      */
28     public function __construct(Setting $setting, Cache $cache)
29     {
30         $this->setting = $setting;
31         $this->cache = $cache;
32     }
33
34     /**
35      * Gets a setting from the database,
36      * If not found, Returns default, Which is false by default.
37      * @param             $key
38      * @param string|bool $default
39      * @return bool|string
40      */
41     public function get($key, $default = false)
42     {
43         if ($default === false) $default = config('setting-defaults.' . $key, false);
44         if (isset($this->localCache[$key])) return $this->localCache[$key];
45
46         $value = $this->getValueFromStore($key, $default);
47         $formatted = $this->formatValue($value, $default);
48         $this->localCache[$key] = $formatted;
49         return $formatted;
50     }
51
52     /**
53      * Get a user-specific setting from the database or cache.
54      * @param User $user
55      * @param $key
56      * @param bool $default
57      * @return bool|string
58      */
59     public function getUser($user, $key, $default = false)
60     {
61         return $this->get($this->userKey($user->id, $key), $default);
62     }
63
64     /**
65      * Gets a setting value from the cache or database.
66      * Looks at the system defaults if not cached or in database.
67      * @param $key
68      * @param $default
69      * @return mixed
70      */
71     protected function getValueFromStore($key, $default)
72     {
73         // Check for an overriding value
74         $overrideValue = $this->getOverrideValue($key);
75         if ($overrideValue !== null) return $overrideValue;
76
77         // Check the cache
78         $cacheKey = $this->cachePrefix . $key;
79         $cacheVal = $this->cache->get($cacheKey, null);
80         if ($cacheVal !== null) return $cacheVal;
81
82         // Check the database
83         $settingObject = $this->getSettingObjectByKey($key);
84         if ($settingObject !== null) {
85             $value = $settingObject->value;
86             $this->cache->forever($cacheKey, $value);
87             return $value;
88         }
89
90         return $default;
91     }
92
93     /**
94      * Clear an item from the cache completely.
95      * @param $key
96      */
97     protected function clearFromCache($key)
98     {
99         $cacheKey = $this->cachePrefix . $key;
100         $this->cache->forget($cacheKey);
101         if (isset($this->localCache[$key])) {
102             unset($this->localCache[$key]);
103         }
104     }
105
106     /**
107      * Format a settings value
108      * @param $value
109      * @param $default
110      * @return mixed
111      */
112     protected function formatValue($value, $default)
113     {
114         // Change string booleans to actual booleans
115         if ($value === 'true') $value = true;
116         if ($value === 'false') $value = false;
117
118         // Set to default if empty
119         if ($value === '') $value = $default;
120         return $value;
121     }
122
123     /**
124      * Checks if a setting exists.
125      * @param $key
126      * @return bool
127      */
128     public function has($key)
129     {
130         $setting = $this->getSettingObjectByKey($key);
131         return $setting !== null;
132     }
133
134     /**
135      * Check if a user setting is in the database.
136      * @param $key
137      * @return bool
138      */
139     public function hasUser($key)
140     {
141         return $this->has($this->userKey($key));
142     }
143
144     /**
145      * Add a setting to the database.
146      * @param $key
147      * @param $value
148      * @return bool
149      */
150     public function put($key, $value)
151     {
152         $setting = $this->setting->firstOrNew([
153             'setting_key' => $key
154         ]);
155         $setting->value = $value;
156         $setting->save();
157         $this->clearFromCache($key);
158         return true;
159     }
160
161     /**
162      * Put a user-specific setting into the database.
163      * @param User $user
164      * @param $key
165      * @param $value
166      * @return bool
167      */
168     public function putUser($user, $key, $value)
169     {
170         return $this->put($this->userKey($user->id, $key), $value);
171     }
172
173     /**
174      * Convert a setting key into a user-specific key.
175      * @param $key
176      * @return string
177      */
178     protected function userKey($userId, $key = '')
179     {
180         return 'user:' . $userId . ':' . $key;
181     }
182
183     /**
184      * Removes a setting from the database.
185      * @param $key
186      * @return bool
187      */
188     public function remove($key)
189     {
190         $setting = $this->getSettingObjectByKey($key);
191         if ($setting) {
192             $setting->delete();
193         }
194         $this->clearFromCache($key);
195         return true;
196     }
197
198     /**
199      * Delete settings for a given user id.
200      * @param $userId
201      * @return mixed
202      */
203     public function deleteUserSettings($userId)
204     {
205         return $this->setting->where('setting_key', 'like', $this->userKey($userId) . '%')->delete();
206     }
207
208     /**
209      * Gets a setting model from the database for the given key.
210      * @param $key
211      * @return mixed
212      */
213     protected function getSettingObjectByKey($key)
214     {
215         return $this->setting->where('setting_key', '=', $key)->first();
216     }
217
218
219     /**
220      * Returns an override value for a setting based on certain app conditions.
221      * Used where certain configuration options overrule others.
222      * Returns null if no override value is available.
223      * @param $key
224      * @return bool|null
225      */
226     protected function getOverrideValue($key)
227     {
228         if ($key === 'registration-enabled' && config('auth.method') === 'ldap') return false;
229         return null;
230     }
231
232 }