ThemeController.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Http\Controllers\Controller;
  4. use Illuminate\Support\Facades\File;
  5. use Illuminate\Http\Request;
  6. class ThemeController extends Controller
  7. {
  8. public function getThemes()
  9. {
  10. $path = public_path('theme/');
  11. $files = array_map(function ($item) use ($path) {
  12. return str_replace($path, '', $item);
  13. }, glob($path . '*'));
  14. $themeConfigs = [];
  15. foreach ($files as $file) {
  16. $themeConfigFile = $path . "{$file}/config.php";
  17. if (!File::exists($themeConfigFile)) continue;
  18. $themeConfig = include($themeConfigFile);
  19. if (!isset($themeConfig['configs']) || !is_array($themeConfig)) continue;
  20. $themeConfigs[$file] = $themeConfig;
  21. }
  22. return response([
  23. 'data' => $themeConfigs
  24. ]);
  25. }
  26. public function saveThemeConfig(Request $request)
  27. {
  28. $path = public_path('theme/');
  29. $files = array_map(function ($item) use ($path) {
  30. return str_replace($path, '', $item);
  31. }, glob($path . '*'));
  32. $payload = $request->validate([
  33. 'name' => 'required|in:' . join(',', $files),
  34. 'configs' => 'required|array'
  35. ]);
  36. $themeConfigFile = public_path("theme/{$payload['name']}/config.php");
  37. if (!File::exists($themeConfigFile)) abort(500, '主题不存在');
  38. $themeConfig = include($themeConfigFile);
  39. $validateFields = array_column($themeConfig['configs'], 'field_name');
  40. dd($validateFields);
  41. }
  42. }