ThemeController.php 3.3 KB

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