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