ThemeService.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. namespace App\Services;
  3. use Illuminate\Support\Facades\Artisan;
  4. use Illuminate\Support\Facades\File;
  5. class ThemeService
  6. {
  7. private $path;
  8. private $theme;
  9. public function __construct($theme)
  10. {
  11. $this->theme = $theme;
  12. $this->path = $path = public_path('theme/');
  13. }
  14. public function init()
  15. {
  16. $themeConfigFile = $this->path . "{$this->theme}/config.php";
  17. if (!File::exists($themeConfigFile)) return;
  18. $themeConfig = include($themeConfigFile);
  19. $configs = $themeConfig['configs'];
  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. try {
  26. if (!File::put(base_path() . "/config/theme/{$this->theme}.php", "<?php\n return $data ;")) {
  27. abort(500, "{$this->theme}初始化失败");
  28. }
  29. } catch (\Exception $e) {
  30. abort(500, '请检查V2Board目录权限');
  31. }
  32. try {
  33. Artisan::call('config:cache');
  34. while (true) {
  35. if (config("theme.{$this->theme}")) break;
  36. }
  37. } catch (\Exception $e) {
  38. abort(500, "{$this->theme}初始化失败");
  39. }
  40. }
  41. }