ThemeController.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Http\Controllers\Controller;
  4. use App\Services\ThemeService;
  5. use Illuminate\Support\Facades\File;
  6. use Illuminate\Support\Facades\Artisan;
  7. use Illuminate\Http\Request;
  8. class ThemeController extends Controller
  9. {
  10. private $themes;
  11. private $path;
  12. public function __construct()
  13. {
  14. $this->path = $path = public_path('theme/');
  15. $this->themes = array_map(function ($item) use ($path) {
  16. return str_replace($path, '', $item);
  17. }, glob($path . '*'));
  18. }
  19. public function getThemes()
  20. {
  21. $themeConfigs = [];
  22. foreach ($this->themes as $theme) {
  23. $themeConfigFile = $this->path . "{$theme}/config.php";
  24. if (!File::exists($themeConfigFile)) continue;
  25. $themeConfig = include($themeConfigFile);
  26. if (!isset($themeConfig['configs']) || !is_array($themeConfig)) continue;
  27. $themeConfigs[$theme] = $themeConfig;
  28. if (config("theme.{$theme}")) continue;
  29. $themeService = new ThemeService($theme);
  30. $themeService->init();
  31. }
  32. return response([
  33. 'data' => [
  34. 'themes' => $themeConfigs,
  35. 'active' => config('v2board.frontend_theme', 'v2board')
  36. ]
  37. ]);
  38. }
  39. public function getThemeConfig(Request $request)
  40. {
  41. $payload = $request->validate([
  42. 'name' => 'required|in:' . join(',', $this->themes)
  43. ]);
  44. return response([
  45. 'data' => config("theme.{$payload['name']}")
  46. ]);
  47. }
  48. public function saveThemeConfig(Request $request)
  49. {
  50. $payload = $request->validate([
  51. 'name' => 'required|in:' . join(',', $this->themes),
  52. 'config' => 'required'
  53. ]);
  54. $payload['config'] = json_decode(base64_decode($payload['config']), true);
  55. if (!$payload['config'] || !is_array($payload['config'])) abort(500, '参数有误');
  56. $themeConfigFile = public_path("theme/{$payload['name']}/config.php");
  57. if (!File::exists($themeConfigFile)) abort(500, '主题不存在');
  58. $themeConfig = include($themeConfigFile);
  59. $validateFields = array_column($themeConfig['configs'], 'field_name');
  60. $config = [];
  61. foreach ($validateFields as $validateField) {
  62. $config[$validateField] = isset($payload['config'][$validateField]) ? $payload['config'][$validateField] : '';
  63. }
  64. File::ensureDirectoryExists(base_path() . '/config/theme/');
  65. $data = var_export($config, 1);
  66. if (!File::put(base_path() . "/config/theme/{$payload['name']}.php", "<?php\n return $data ;")) {
  67. abort(500, '修改失败');
  68. }
  69. try {
  70. Artisan::call('config:cache');
  71. // sleep(2);
  72. } catch (\Exception $e) {
  73. abort(500, '保存失败');
  74. }
  75. return response([
  76. 'data' => $config
  77. ]);
  78. }
  79. }