update: custom theme

This commit is contained in:
tokumeikoi 2022-05-03 14:28:00 +08:00
parent a0ebcb948b
commit 20e365e771
4 changed files with 47 additions and 12 deletions

View File

@ -43,5 +43,6 @@ class Test extends Command
*/ */
public function handle() public function handle()
{ {
abort(500, 123);
} }
} }

View File

@ -4,44 +4,75 @@ namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller; use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\File; use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Http\Request; use Illuminate\Http\Request;
class ThemeController extends Controller class ThemeController extends Controller
{ {
public function getThemes() private $themes;
private $path;
public function __construct()
{ {
$path = public_path('theme/'); $this->path = $path = public_path('theme/');
$files = array_map(function ($item) use ($path) { $this->themes = array_map(function ($item) use ($path) {
return str_replace($path, '', $item); return str_replace($path, '', $item);
}, glob($path . '*')); }, glob($path . '*'));
}
public function getThemes()
{
$themeConfigs = []; $themeConfigs = [];
foreach ($files as $file) { foreach ($this->themes as $theme) {
$themeConfigFile = $path . "{$file}/config.php"; $themeConfigFile = $this->path . "{$theme}/config.php";
if (!File::exists($themeConfigFile)) continue; if (!File::exists($themeConfigFile)) continue;
$themeConfig = include($themeConfigFile); $themeConfig = include($themeConfigFile);
if (!isset($themeConfig['configs']) || !is_array($themeConfig)) continue; if (!isset($themeConfig['configs']) || !is_array($themeConfig)) continue;
$themeConfigs[$file] = $themeConfig; $themeConfigs[$this->themes] = $themeConfig;
} }
return response([ return response([
'data' => $themeConfigs 'data' => $themeConfigs
]); ]);
} }
public function getThemeConfig(Request $request)
{
return response([
'data' => config('theme.v2board')
]);
}
public function saveThemeConfig(Request $request) 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([ $payload = $request->validate([
'name' => 'required|in:' . join(',', $files), 'name' => 'required|in:' . join(',', $this->themes),
'configs' => 'required|array' 'configs' => 'required|array'
]); ]);
$themeConfigFile = public_path("theme/{$payload['name']}/config.php"); $themeConfigFile = public_path("theme/{$payload['name']}/config.php");
if (!File::exists($themeConfigFile)) abort(500, '主题不存在'); if (!File::exists($themeConfigFile)) abort(500, '主题不存在');
$themeConfig = include($themeConfigFile); $themeConfig = include($themeConfigFile);
$validateFields = array_column($themeConfig['configs'], 'field_name'); $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", "<?php\n return $data ;")) {
abort(500, '修改失败');
}
try {
Artisan::call('config:cache');
} catch (\Exception $e) {
abort(500, '保存失败');
}
return response([
'data' => config('theme.v2board')
]);
} }
} }

View File

@ -117,6 +117,7 @@ class AdminRoute
// Theme // Theme
$router->get ('/theme/getThemes', 'Admin\\ThemeController@getThemes'); $router->get ('/theme/getThemes', 'Admin\\ThemeController@getThemes');
$router->post('/theme/saveThemeConfig', 'Admin\\ThemeController@saveThemeConfig'); $router->post('/theme/saveThemeConfig', 'Admin\\ThemeController@saveThemeConfig');
$router->get ('/theme/getThemeConfig', 'Admin\\ThemeController@getThemeConfig');
}); });
} }
} }

2
config/theme/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
*.php
!.gitignore