v2board/app/Http/Controllers/Admin/ThemeController.php

91 lines
2.9 KiB
PHP
Raw Normal View History

2022-05-03 03:16:16 +08:00
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
2022-05-19 02:34:25 +08:00
use App\Services\ThemeService;
2022-05-03 03:16:16 +08:00
use Illuminate\Support\Facades\File;
2022-05-03 14:28:00 +08:00
use Illuminate\Support\Facades\Artisan;
2022-05-03 03:16:16 +08:00
use Illuminate\Http\Request;
class ThemeController extends Controller
{
2022-05-03 14:28:00 +08:00
private $themes;
private $path;
public function __construct()
2022-05-03 03:16:16 +08:00
{
2022-05-03 14:28:00 +08:00
$this->path = $path = public_path('theme/');
$this->themes = array_map(function ($item) use ($path) {
2022-05-03 03:16:16 +08:00
return str_replace($path, '', $item);
}, glob($path . '*'));
2022-05-03 14:28:00 +08:00
}
public function getThemes()
{
2022-05-03 03:16:16 +08:00
$themeConfigs = [];
2022-05-03 14:28:00 +08:00
foreach ($this->themes as $theme) {
$themeConfigFile = $this->path . "{$theme}/config.php";
2022-05-03 03:16:16 +08:00
if (!File::exists($themeConfigFile)) continue;
$themeConfig = include($themeConfigFile);
if (!isset($themeConfig['configs']) || !is_array($themeConfig)) continue;
2022-05-09 00:26:38 +08:00
$themeConfigs[$theme] = $themeConfig;
if (config("theme.{$theme}")) continue;
2022-05-19 02:34:25 +08:00
$themeService = new ThemeService($theme);
$themeService->init();
2022-05-03 03:16:16 +08:00
}
return response([
2022-05-06 13:53:31 +08:00
'data' => [
'themes' => $themeConfigs,
2022-05-09 00:26:38 +08:00
'active' => config('v2board.frontend_theme', 'v2board')
2022-05-06 13:53:31 +08:00
]
2022-05-03 03:16:16 +08:00
]);
}
2022-05-03 14:28:00 +08:00
public function getThemeConfig(Request $request)
{
2022-05-06 13:53:31 +08:00
$payload = $request->validate([
'name' => 'required|in:' . join(',', $this->themes)
]);
2022-05-03 14:28:00 +08:00
return response([
2022-05-06 13:53:31 +08:00
'data' => config("theme.{$payload['name']}")
2022-05-03 14:28:00 +08:00
]);
}
2022-05-03 03:16:16 +08:00
public function saveThemeConfig(Request $request)
{
$payload = $request->validate([
2022-05-03 14:28:00 +08:00
'name' => 'required|in:' . join(',', $this->themes),
2022-05-16 17:31:37 +08:00
'config' => 'required'
2022-05-03 03:16:16 +08:00
]);
2022-05-16 17:31:37 +08:00
$payload['config'] = json_decode(base64_decode($payload['config']), true);
if (!$payload['config'] || !is_array($payload['config'])) abort(500, '参数有误');
2022-05-03 03:16:16 +08:00
$themeConfigFile = public_path("theme/{$payload['name']}/config.php");
if (!File::exists($themeConfigFile)) abort(500, '主题不存在');
$themeConfig = include($themeConfigFile);
$validateFields = array_column($themeConfig['configs'], 'field_name');
2022-05-03 14:28:00 +08:00
$config = [];
foreach ($validateFields as $validateField) {
2022-05-09 00:26:38 +08:00
$config[$validateField] = isset($payload['config'][$validateField]) ? $payload['config'][$validateField] : '';
2022-05-03 14:28:00 +08:00
}
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');
2022-05-09 01:45:33 +08:00
// sleep(2);
2022-05-03 14:28:00 +08:00
} catch (\Exception $e) {
abort(500, '保存失败');
}
2022-05-03 03:16:16 +08:00
2022-05-03 14:28:00 +08:00
return response([
2022-05-09 00:26:38 +08:00
'data' => $config
2022-05-03 14:28:00 +08:00
]);
2022-05-03 03:16:16 +08:00
}
}