From 20e365e771d86f008ce93041f081287643d7b9df Mon Sep 17 00:00:00 2001 From: tokumeikoi Date: Tue, 3 May 2022 14:28:00 +0800 Subject: [PATCH] update: custom theme --- app/Console/Commands/Test.php | 1 + .../Controllers/Admin/ThemeController.php | 55 +++++++++++++++---- app/Http/Routes/AdminRoute.php | 1 + config/theme/.gitignore | 2 + 4 files changed, 47 insertions(+), 12 deletions(-) create mode 100644 config/theme/.gitignore diff --git a/app/Console/Commands/Test.php b/app/Console/Commands/Test.php index 14b5932e..7ef4c535 100644 --- a/app/Console/Commands/Test.php +++ b/app/Console/Commands/Test.php @@ -43,5 +43,6 @@ class Test extends Command */ public function handle() { + abort(500, 123); } } diff --git a/app/Http/Controllers/Admin/ThemeController.php b/app/Http/Controllers/Admin/ThemeController.php index b78b9e67..dcf2dc9d 100644 --- a/app/Http/Controllers/Admin/ThemeController.php +++ b/app/Http/Controllers/Admin/ThemeController.php @@ -4,44 +4,75 @@ namespace App\Http\Controllers\Admin; use App\Http\Controllers\Controller; use Illuminate\Support\Facades\File; +use Illuminate\Support\Facades\Artisan; use Illuminate\Http\Request; class ThemeController extends Controller { - public function getThemes() + private $themes; + private $path; + + public function __construct() { - $path = public_path('theme/'); - $files = array_map(function ($item) use ($path) { + $this->path = $path = public_path('theme/'); + $this->themes = array_map(function ($item) use ($path) { return str_replace($path, '', $item); }, glob($path . '*')); + } + + public function getThemes() + { $themeConfigs = []; - foreach ($files as $file) { - $themeConfigFile = $path . "{$file}/config.php"; + foreach ($this->themes as $theme) { + $themeConfigFile = $this->path . "{$theme}/config.php"; if (!File::exists($themeConfigFile)) continue; $themeConfig = include($themeConfigFile); if (!isset($themeConfig['configs']) || !is_array($themeConfig)) continue; - $themeConfigs[$file] = $themeConfig; + $themeConfigs[$this->themes] = $themeConfig; } return response([ 'data' => $themeConfigs ]); } + public function getThemeConfig(Request $request) + { + return response([ + 'data' => config('theme.v2board') + ]); + } + public function saveThemeConfig(Request $request) { - $path = public_path('theme/'); - $files = array_map(function ($item) use ($path) { - return str_replace($path, '', $item); - }, glob($path . '*')); $payload = $request->validate([ - 'name' => 'required|in:' . join(',', $files), + 'name' => 'required|in:' . join(',', $this->themes), 'configs' => 'required|array' ]); $themeConfigFile = public_path("theme/{$payload['name']}/config.php"); if (!File::exists($themeConfigFile)) abort(500, '主题不存在'); $themeConfig = include($themeConfigFile); $validateFields = array_column($themeConfig['configs'], 'field_name'); - dd($validateFields); + $config = []; + foreach ($validateFields as $validateField) { + if (!isset($payload['configs'][$validateField])) continue; + $config[$validateField] = $payload['configs'][$validateField]; + } + File::ensureDirectoryExists(base_path() . '/config/theme/'); + + $data = var_export($config, 1); + if (!File::put(base_path() . "/config/theme/{$payload['name']}.php", " config('theme.v2board') + ]); } } diff --git a/app/Http/Routes/AdminRoute.php b/app/Http/Routes/AdminRoute.php index 1adb549e..fd5dbf51 100644 --- a/app/Http/Routes/AdminRoute.php +++ b/app/Http/Routes/AdminRoute.php @@ -117,6 +117,7 @@ class AdminRoute // Theme $router->get ('/theme/getThemes', 'Admin\\ThemeController@getThemes'); $router->post('/theme/saveThemeConfig', 'Admin\\ThemeController@saveThemeConfig'); + $router->get ('/theme/getThemeConfig', 'Admin\\ThemeController@getThemeConfig'); }); } } diff --git a/config/theme/.gitignore b/config/theme/.gitignore new file mode 100644 index 00000000..9b8775cf --- /dev/null +++ b/config/theme/.gitignore @@ -0,0 +1,2 @@ +*.php +!.gitignore