-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathAppSettings.php
339 lines (292 loc) · 8.5 KB
/
AppSettings.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
<?php
namespace QCod\AppSettings\Setting;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use QCod\Settings\Setting\SettingStorage;
class AppSettings
{
/**
* @var SettingStorage
*/
private $settingStorage;
/**
* AppSettings constructor.
*
* @param SettingStorage $settingStorage
*/
public function __construct(SettingStorage $settingStorage)
{
$this->settingStorage = $settingStorage;
}
/**
* Get al the settings from storage
*
* @param bool $fresh
* @return \Illuminate\Support\Collection
*/
public function all($fresh = false)
{
return $this->settingStorage->all($fresh);
}
/**
* Get a setting and cast the value
*
* @param $name
* @param null $default
* @return mixed
*/
public function get($name, $default = null)
{
$settingField = $this->getSettingField($name);
// get the setting and fallback to default value from config
$value = $this->settingStorage->get(
$name,
array_get($settingField, 'value', $default)
);
// cast the value
$outValue = $this->castValue(array_get($settingField, 'data_type'), $value, true);
// check for accessor to run
if ($accessor = array_get($settingField, 'accessor')) {
$outValue = $this->runCallback($accessor, $name, $value);
}
return $outValue;
}
/**
* Save a setting into storage
*
* @param $name string|array
* @param $value string|mixed
* @return mixed
*/
public function set($name, $value)
{
$settingField = $this->getSettingField($name);
$dataType = array_get($settingField, 'data_type');
$val = $this->castValue($dataType, $value);
// check for mutator to run
if ($mutator = array_get($settingField, 'mutator')) {
$val = $this->runCallback($mutator, $name, $value);
}
return $this->settingStorage->set($name, $val);
}
/**
* Remove a setting from storage
*
* @param $name
* @return mixed
*/
public function remove($name)
{
return $this->settingStorage->remove($name);
}
/**
* Save incoming settings
*
* @param $request \Illuminate\Http\Request
*/
public function save($request)
{
$settingsPage = preg_replace("/[^A-Za-z0-9 ]/", '', $request->page);
// get all defined settings from config
$allDefinedSettings = $this->getAllSettingFields($settingsPage);
// set all the fields with updated values
$allDefinedSettings->each(function ($setting) use ($request) {
$settingName = $setting['name'];
$type = $setting['type'];
// handle file upload
if (in_array($type, ['file', 'image']) && !isset($setting['mutator'])) {
$this->uploadFile($setting, $request);
// any other type of field
} elseif ($request->has($settingName) || $type == 'checkbox') {
$this->set($settingName, $request->get($settingName));
}
});
// clean up any abandoned settings
$this->cleanUpSettings($allDefinedSettings);
}
/**
* Load and resolve closers in config
*
* @param $config
* @return array
*/
public function loadConfig($config)
{
// resolve any callback for inputs
array_walk_recursive($config, function (&$value, $key) {
if (!in_array($key, ['mutator', 'accessor', 'rules']) && is_callable($value)) {
// skip any string which dont look like namesapce
if (is_string($value) && str_contains($value, '\\') == false) {
return;
}
$value = $this->runCallback($value, $key, '');
}
});
return $config;
}
/**
* Get the settings UI sections as collection
*
* @return \Illuminate\Support\Collection
*/
protected function getSettingUISections(string $settingsPage)
{
return collect(config('app_settings.sections.'.$settingsPage , []));
}
/**
* Get all the setting fields defined from all sections
* @param $settingsPage string : the name of the page of settings to use
* @return \Illuminate\Support\Collection
*/
public function getAllSettingFields(string $settingsPage = '' )
{
return $this->getSettingUISections($settingsPage)->flatMap(function ($field) {
return array_get($field, 'inputs', []);
});
}
/**
* Get a single setting field config
*
* @param $name
* @return array
*/
public function getSettingField($name)
{
return $this->getAllSettingFields()
->first(function ($field) use ($name) {
return array_get($field, 'name') == $name;
}, []);
}
/**
* Build validation rules for laravel validator
*
* @return array
*/
public function getValidationRules()
{
return $this->getAllSettingFields()
->pluck('rules', 'name')
->reject(function ($val) {
return is_null($val);
})
->toArray();
}
/**
* Cast a value into a type
*
* @param $dataType
* @param $value
* @param bool $out
* @return bool|int|mixed|string
*/
private function castValue($dataType, $value, $out = false)
{
switch ($dataType) {
case 'array':
return $this->castToArray($value, $out);
break;
case 'int':
case 'integer':
case 'number':
return intval($value);
break;
case 'boolean':
case 'bool':
return boolval($value);
break;
default:
return $value;
}
}
/**
* Run a callback to mutate and access the value
*
* @param $callback
* @param $name
* @param $value
* @return string|mixed
*/
protected function runCallback($callback, $name, $value)
{
if (is_callable($callback)) {
$value = $callback($value, $name);
}
// try callback handler class
if (is_string($callback)) {
$instance = app($callback);
$value = $instance->handle($value, $name);
}
return $value;
}
/**
* Remove abandoned settings
*
* @param $allDefinedSettings \Illuminate\Support\Collection
*/
protected function cleanUpSettings($allDefinedSettings)
{
if (!config('app_settings.remove_abandoned_settings')) {
return;
}
$this->settingStorage->all()->keys()
->diff($allDefinedSettings->pluck('name'))
->each(function ($field) {
$this->remove($field);
});
}
/**
* Cast value to array
*
* @param $value
* @param $out
* @return array|mixed|string
*/
private function castToArray($value, $out)
{
if ($out) {
return empty($value) ? [] : json_decode($value, true);
}
return json_encode($value);
}
/**
* Upload a file
*
* @param $setting array
* @param $request Request
* @return string|null
*/
private function uploadFile($setting, $request)
{
$settingName = array_get($setting, 'name');
// get the disk and path to upload
$disk = array_get($setting, 'disk', 'public');
$path = array_get($setting, 'path', '/');
$uploadedPath = null;
$oldFile = $this->get($settingName);
if ($request->hasFile($settingName)) {
$uploadedPath = $request->$settingName->store($path, $disk);
$this->set($settingName, $uploadedPath);
// delete old file
$this->deleteFile($oldFile, $disk);
return $uploadedPath;
}
// check for remove asked
if ($request->has('remove_file_' . $settingName)) {
$this->deleteFile($oldFile, $disk);
$this->set($settingName, null);
}
return $uploadedPath;
}
/**
* Delete a file
*
* @param $oldFile
* @param $disk
*/
private function deleteFile($oldFile, $disk): void
{
if ($oldFile && Storage::disk($disk)->exists($oldFile)) {
Storage::disk($disk)->delete($oldFile);
}
}
}