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

92 lines
3.1 KiB
PHP
Raw Permalink 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.json";
2022-05-03 03:16:16 +08:00
if (!File::exists($themeConfigFile)) continue;
$themeConfig = json_decode(File::get($themeConfigFile), true);
2022-05-03 03:16:16 +08:00
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-12-17 23:00:23 +08:00
$themeConfigFile = public_path("theme/{$payload['name']}/config.json");
2022-05-03 03:16:16 +08:00
if (!File::exists($themeConfigFile)) abort(500, '主题不存在');
2022-12-17 21:42:27 +08:00
$themeConfig = json_decode(File::get($themeConfigFile), true);
if (!isset($themeConfig['configs']) || !is_array($themeConfig)) abort(500, '主题配置文件有误');
2022-05-03 03:16:16 +08:00
$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
}
}