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
|
|
|
}
|
|
|
|
|
|
|
|
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-03 14:28:00 +08:00
|
|
|
$themeConfigs[$this->themes] = $themeConfig;
|
2022-05-03 03:16:16 +08:00
|
|
|
}
|
|
|
|
return response([
|
|
|
|
'data' => $themeConfigs
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2022-05-03 14:28:00 +08:00
|
|
|
public function getThemeConfig(Request $request)
|
|
|
|
{
|
|
|
|
return response([
|
|
|
|
'data' => config('theme.v2board')
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
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-03 03:16:16 +08:00
|
|
|
'configs' => 'required|array'
|
|
|
|
]);
|
|
|
|
$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) {
|
|
|
|
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, '保存失败');
|
|
|
|
}
|
2022-05-03 03:16:16 +08:00
|
|
|
|
2022-05-03 14:28:00 +08:00
|
|
|
return response([
|
|
|
|
'data' => config('theme.v2board')
|
|
|
|
]);
|
2022-05-03 03:16:16 +08:00
|
|
|
}
|
|
|
|
}
|