DynamicMenuServiceProvider.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. namespace Longyi\DynamicMenu\Providers;
  3. use Illuminate\Support\ServiceProvider;
  4. use Illuminate\Support\Facades\Cache;
  5. use Longyi\DynamicMenu\Models\MenuItem;
  6. class DynamicMenuServiceProvider extends ServiceProvider
  7. {
  8. public function boot()
  9. {
  10. // 加载迁移
  11. $this->loadMigrationsFrom(__DIR__ . '/../Database/Migrations');
  12. // 加载路由
  13. $this->loadRoutesFrom(__DIR__ . '/../Routes/admin-routes.php');
  14. // 加载视图
  15. $this->loadViewsFrom(__DIR__ . '/../Resources/views', 'dynamicmenu');
  16. // 加载语言文件
  17. $this->loadTranslationsFrom(__DIR__ . '/../Resources/lang', 'dynamicmenu');
  18. // 发布资源
  19. $this->publishes([
  20. __DIR__ . '/../../publishable/assets' => public_path('vendor/dynamicmenu'),
  21. ], 'public');
  22. }
  23. public function register()
  24. {
  25. // 延迟合并配置,等到服务提供者注册完成后
  26. $this->app->booted(function () {
  27. $this->mergeDynamicMenuConfig();
  28. });
  29. if ($this->app->runningInConsole()) {
  30. $this->commands([
  31. \Longyi\DynamicMenu\Console\Commands\InitializeSettings::class,
  32. ]);
  33. }
  34. }
  35. /**
  36. * 合并动态菜单配置
  37. */
  38. protected function mergeDynamicMenuConfig()
  39. {
  40. try {
  41. // 从数据库获取菜单配置
  42. $menuConfig = $this->getMenuConfigFromDatabase();
  43. // 获取现有配置
  44. $existingConfig = $this->app['config']->get('menu.admin', []);
  45. // 合并配置
  46. $mergedConfig = array_merge($existingConfig, $menuConfig);
  47. // 设置配置
  48. $this->app['config']->set('menu.admin', $mergedConfig);
  49. } catch (\Exception $e) {
  50. \Log::error('合并动态菜单配置失败: ' . $e->getMessage());
  51. }
  52. }
  53. /**
  54. * 从数据库获取菜单配置
  55. */
  56. protected function getMenuConfigFromDatabase($useCache = true)
  57. {
  58. if (!$useCache) {
  59. // 不使用缓存,直接查询数据库
  60. $menuItems = MenuItem::with('children')
  61. ->where('status', 1)
  62. ->orderBy('sort_order')
  63. ->get();
  64. return $this->buildMenuConfig($menuItems);
  65. }
  66. // 使用缓存
  67. return Cache::remember('dynamic_menu_config', 3600, function () {
  68. try {
  69. $menuItems = MenuItem::with('children')
  70. ->where('status', 1)
  71. ->orderBy('sort_order')
  72. ->get();
  73. return $this->buildMenuConfig($menuItems);
  74. } catch (\Exception $e) {
  75. \Log::warning('无法从数据库获取菜单:' . $e->getMessage());
  76. return [];
  77. }
  78. });
  79. }
  80. // ... existing code ...
  81. /**
  82. * 构建菜单配置数组
  83. */
  84. protected function buildMenuConfig($menuItems)
  85. {
  86. $config = [];
  87. foreach ($menuItems as $item) {
  88. $menuItem = [
  89. 'key' => $item->key,
  90. 'name' => $item->name,
  91. 'route' => $item->route ?: 'admin.dynamicmenu.index',
  92. 'sort' => (int) $item->sort_order,
  93. 'icon' => $item->icon ?: 'icon-menu',
  94. ];
  95. $config[] = $menuItem;
  96. }
  97. return $config;
  98. }
  99. }