feat: Add Extension Settings to Settingspage

This commit is contained in:
IceToast 2023-03-04 19:59:59 +01:00 committed by IceToast
parent 3ea5b4e010
commit 1a8f883b63
2 changed files with 53 additions and 7 deletions

View file

@ -102,4 +102,34 @@ class ExtensionHelper
return $migrations;
}
/**
* Summary of getAllExtensionSettings
* @return array of all setting classes look like: App\Extensions\PaymentGateways\PayPal\PayPalSettings
*/
public static function getAllExtensionSettings()
{
$extensions = ExtensionHelper::getAllExtensions();
$settings = [];
foreach ($extensions as $extension) {
$extensionName = basename($extension);
$settingFile = $extension . '/' . $extensionName . 'Settings.php';
if (file_exists($settingFile)) {
// remove the base path from the setting file path to get the namespace
$settingFile = str_replace(app_path() . '/', '', $settingFile);
$settingFile = str_replace('.php', '', $settingFile);
$settingFile = str_replace('/', '\\', $settingFile);
$settingFile = 'App\\' . $settingFile;
$settings[] = $settingFile;
}
}
error_log(print_r($settings, true));
return $settings;
}
}

View file

@ -2,6 +2,7 @@
namespace App\Http\Controllers\Admin;
use App\Helpers\ExtensionHelper;
use App\Http\Controllers\Controller;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\View\Factory;
@ -25,18 +26,33 @@ class SettingsController extends Controller
// get all other settings in app/Settings directory
// group items by file name like $categories
$settings = collect();
foreach (scandir(app_path('Settings')) as $file) {
if (in_array($file, ['.', '..'])) {
continue;
}
$className = 'App\\Settings\\' . str_replace('.php', '', $file);
$options = (new $className())->toArray();
$settings_classes = [];
// get all app settings
$app_settings = scandir(app_path('Settings'));
$app_settings = array_diff($app_settings, ['.', '..']);
// append App\Settings to class name
foreach ($app_settings as $app_setting) {
$settings_classes[] = 'App\\Settings\\' . str_replace('.php', '', $app_setting);
}
// get all extension settings
$settings_files = array_merge($settings_classes, ExtensionHelper::getAllExtensionSettings());
foreach ($settings_files as $file) {
$className = $file;
// instantiate the class and call toArray method to get all options
$options = (new $className())->toArray();
error_log(print_r($className, true));
// call getOptionInputData method to get all options
if (method_exists($className, 'getOptionInputData')) {
$optionInputData = $className::getOptionInputData();
} else {
$optionInputData = [];
}
error_log(print_r($optionInputData, true));
// collect all option input data
$optionsData = [];
@ -55,7 +71,7 @@ class SettingsController extends Controller
$optionsData['category_icon'] = $optionInputData['category_icon'];
}
$settings[str_replace('Settings.php', '', $file)] = $optionsData;
$settings[str_replace('Settings', '', class_basename($className))] = $optionsData;
}
$settings->sort();