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