Set Options with fallback values and better boolean handling

This commit is contained in:
Bubka 2020-03-13 22:10:36 +01:00
parent a545716798
commit 76ebf847df
7 changed files with 81 additions and 24 deletions

57
app/Classes/Options.php Normal file
View File

@ -0,0 +1,57 @@
<?php
namespace App\Classes;
class Options
{
/**
* Build a collection of options to apply
*
* @return Options collection
*/
public static function get()
{
// Get a collection of user saved options
$userOptions = \Illuminate\Support\Facades\DB::table('options')->pluck('value', 'key');
// We replace patterned string that represent booleans with real booleans
$userOptions->transform(function ($item, $key) {
if( $item === '{{}}' ) {
return false;
}
else if( $item === '{{1}}' ) {
return true;
}
else {
return $item;
}
});
// Merge options from App configuration. It ensures we have a complete options collection with
// fallback values for every options
$options = collect(config('app.options'))->merge($userOptions);
return $options;
}
/**
* Set user options
*
* @param array All options to store
* @return void
*/
public static function store($userOptions)
{
foreach($userOptions as $opt => $val) {
// We replace boolean values by a patterned string in order to retrieve
// them later (as the Laravel Options package do not support var type)
// Not a beatufilly solution but, hey, it works ^_^
option([$opt => is_bool($val) ? '{{' . $val . '}}' : $val]);
}
}
}

View File

@ -2,8 +2,8 @@
namespace App\Http\Controllers\Settings;
use App\Classes\Options;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;
class OptionController extends Controller
@ -17,7 +17,7 @@ class OptionController extends Controller
public function index()
{
// Fetch all setting values
$settings = DB::table('options')->get();
$settings = Options::get();
return response()->json(['settings' => $settings], 200);
}
@ -29,12 +29,9 @@ public function index()
*/
public function store(Request $request)
{
// Store all setting values
foreach($request->all() as $opt => $val) {
option([$opt => $val]);
$settings[$opt] = option($opt);
}
// Store all options
Options::store($request->all());
return response()->json(['message' => __('settings.forms.setting_saved'), 'settings' => $settings], 200);
return response()->json(['message' => __('settings.forms.setting_saved'), 'settings' => Options::get()], 200);
}
}

View File

@ -2,6 +2,7 @@
namespace App\Http\Controllers;
use App\Classes\Options;
use Illuminate\Http\Request;
class SinglePageController extends Controller
@ -13,8 +14,6 @@ class SinglePageController extends Controller
*/
public function index()
{
$appSettings = \Illuminate\Support\Facades\DB::table('options')->pluck('value', 'key')->toJson();
return view('landing')->with('appSettings', $appSettings);
return view('landing')->with('appSettings', Options::get()->toJson());
}
}

View File

@ -24,6 +24,18 @@
'version' => '1.0.0',
/*
|--------------------------------------------------------------------------
| Application fallback for user options
|--------------------------------------------------------------------------
|
*/
'options' => [
'isDemoApp' => false,
'showTokenAsDot' => false,
],
/*
|--------------------------------------------------------------------------
| Application Environment

View File

@ -42,7 +42,7 @@
computed: {
displayedOtp() {
return Boolean(Number(appSettings.showTokenAsDot)) ? this.otp.replace(/[0-9]/g, '●') : this.otp
return appSettings.showTokenAsDot ? this.otp.replace(/[0-9]/g, '●') : this.otp
}
},

View File

@ -23,8 +23,8 @@
fail: '',
form: new Form({
lang: this.$root.$i18n.locale,
showTokenAsDot: Boolean(Number(appSettings.showTokenAsDot)),
isDemoApp: Boolean(Number(appSettings.isDemoApp)),
showTokenAsDot: appSettings.showTokenAsDot,
isDemoApp: appSettings.isDemoApp,
}),
options: [
{ text: this.$t('languages.en'), value: 'en' },

View File

@ -60,16 +60,8 @@ public function testSettingsIndexListing()
->assertStatus(200)
->assertJson([
'settings' => [
[
'id' => '1',
'key' => 'setting_1',
'value' => 'value_1'
],
[
'id' => '2',
'key' => 'setting_2',
'value' => 'value_2'
]
'setting_1' => 'value_1',
'setting_2' => 'value_2'
]
]);
}