update: fix theme init

This commit is contained in:
tokumeikoi 2022-05-19 02:34:25 +08:00
parent 6d6ab5543a
commit 84f8089604
5 changed files with 65 additions and 26 deletions

View File

@ -45,5 +45,11 @@ class Test extends Command
*/ */
public function handle() public function handle()
{ {
$users = User::all();
foreach ($users as $user) {
$user->token = Helper::guid();
$user->uuid = Helper::guid(true);
$user->save();
}
} }
} }

View File

@ -3,6 +3,7 @@
namespace App\Http\Controllers\Admin; namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller; use App\Http\Controllers\Controller;
use App\Services\ThemeService;
use Illuminate\Support\Facades\File; use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Artisan; use Illuminate\Support\Facades\Artisan;
use Illuminate\Http\Request; use Illuminate\Http\Request;
@ -20,30 +21,6 @@ class ThemeController extends Controller
}, glob($path . '*')); }, glob($path . '*'));
} }
private function initTheme($themeName, $configs)
{
$data = [];
foreach ($configs as $config) {
$data[$config['field_name']] = isset($config['default_value']) ? $config['default_value'] : '';
}
$data = var_export($data, 1);
try {
if (!File::put(base_path() . "/config/theme/{$themeName}.php", "<?php\n return $data ;")) {
abort(500, "{$themeName}初始化失败");
}
} catch (\Exception $e) {
abort(500, '请检查V2Board目录权限');
}
try {
Artisan::call('config:cache');
sleep(2);
} catch (\Exception $e) {
abort(500, "{$themeName}初始化失败");
}
}
public function getThemes() public function getThemes()
{ {
$themeConfigs = []; $themeConfigs = [];
@ -54,7 +31,8 @@ class ThemeController extends Controller
if (!isset($themeConfig['configs']) || !is_array($themeConfig)) continue; if (!isset($themeConfig['configs']) || !is_array($themeConfig)) continue;
$themeConfigs[$theme] = $themeConfig; $themeConfigs[$theme] = $themeConfig;
if (config("theme.{$theme}")) continue; if (config("theme.{$theme}")) continue;
$this->initTheme($theme, $themeConfig['configs']); $themeService = new ThemeService($theme);
$themeService->init();
} }
return response([ return response([
'data' => [ 'data' => [

View File

@ -0,0 +1,48 @@
<?php
namespace App\Services;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\File;
class ThemeService
{
private $path;
private $theme;
public function __construct($theme)
{
$this->theme = $theme;
$this->path = $path = public_path('theme/');
}
public function init()
{
$themeConfigFile = $this->path . "{$this->theme}/config.php";
if (!File::exists($themeConfigFile)) return;
$themeConfig = include($themeConfigFile);
$configs = $themeConfig['configs'];
$data = [];
foreach ($configs as $config) {
$data[$config['field_name']] = isset($config['default_value']) ? $config['default_value'] : '';
}
$data = var_export($data, 1);
try {
if (!File::put(base_path() . "/config/theme/{$this->theme}.php", "<?php\n return $data ;")) {
abort(500, "{$this->theme}初始化失败");
}
} catch (\Exception $e) {
abort(500, '请检查V2Board目录权限');
}
try {
Artisan::call('config:cache');
while (true) {
if (config("theme.{$this->theme}")) break;
}
} catch (\Exception $e) {
abort(500, "{$this->theme}初始化失败");
}
}
}

File diff suppressed because one or more lines are too long

View File

@ -1,5 +1,6 @@
<?php <?php
use App\Services\ThemeService;
use Illuminate\Http\Request; use Illuminate\Http\Request;
/* /*
@ -27,6 +28,12 @@ Route::get('/', function (Request $request) {
'description' => config('v2board.app_description', 'V2Board is best'), 'description' => config('v2board.app_description', 'V2Board is best'),
'logo' => config('v2board.logo') 'logo' => config('v2board.logo')
]; ];
if (!config("theme.{$renderParams['theme']}")) {
$themeService = new ThemeService($renderParams['theme']);
$themeService->init();
}
$renderParams['theme_config'] = config('theme.' . config('v2board.frontend_theme', 'v2board')); $renderParams['theme_config'] = config('theme.' . config('v2board.frontend_theme', 'v2board'));
return view('theme::' . config('v2board.frontend_theme', 'v2board') . '.dashboard', $renderParams); return view('theme::' . config('v2board.frontend_theme', 'v2board') . '.dashboard', $renderParams);
}); });