2022-05-03 03:16:16 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
|
|
|
|
use App\Http\Controllers\Controller;
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-05-09 00:26:38 +08:00
|
|
|
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);
|
2022-05-15 01:10:27 +08:00
|
|
|
try {
|
|
|
|
if (!File::put(base_path() . "/config/theme/{$themeName}.php", "<?php\n return $data ;")) {
|
|
|
|
abort(500, "{$themeName}初始化失败");
|
|
|
|
}
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
abort(500, '请检查V2Board目录权限');
|
2022-05-09 00:26:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
Artisan::call('config:cache');
|
2022-05-09 00:30:44 +08:00
|
|
|
sleep(2);
|
2022-05-09 00:26:38 +08:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
abort(500, "{$themeName}初始化失败");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
$this->initTheme($theme, $themeConfig['configs']);
|
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-09 00:26:38 +08:00
|
|
|
'config' => 'required|array'
|
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
|
|
|
}
|
|
|
|
}
|