| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- <?php
- namespace App\Console\Commands;
- use App\Module\System\Models\AdminMenu;
- use Illuminate\Console\Command;
- use Illuminate\Support\Str;
- class CheckAllAdminMenus extends Command
- {
- /**
- * The name and signature of the console command.
- *
- * @var string
- */
- protected $signature = 'admin:check-all-menus';
- /**
- * The console command description.
- *
- * @var string
- */
- protected $description = '检查所有模块的后台控制器是否都有对应的菜单项';
- /**
- * Execute the console command.
- *
- * @return int
- */
- public function handle()
- {
- $this->info("开始检查所有模块的后台控制器...");
-
- $modules = [
- 'Activity', 'Article', 'Farm', 'File', 'Fund', 'Game', 'GameItems',
- 'OAuth', 'Pet', 'Sms', 'System', 'Task', 'Team', 'User'
- ];
-
- $missingMenus = [];
- $totalControllers = 0;
- $totalMenus = 0;
-
- foreach ($modules as $module) {
- $this->info("\n检查{$module}模块的后台控制器...");
-
- $controllerDir = app_path("Module/{$module}/AdminControllers");
-
- if (!is_dir($controllerDir)) {
- $this->warn(" - 未找到AdminControllers目录");
- continue;
- }
-
- $controllers = array_filter(scandir($controllerDir), function($file) {
- return !in_array($file, ['.', '..']) &&
- pathinfo($file, PATHINFO_EXTENSION) === 'php' &&
- !Str::contains($file, 'Helper');
- });
-
- $moduleControllers = 0;
- $moduleMenus = 0;
-
- foreach ($controllers as $controller) {
- $controllerName = pathinfo($controller, PATHINFO_FILENAME);
- $uri = $this->getControllerUri($controllerName);
-
- $totalControllers++;
- $moduleControllers++;
-
- // 检查菜单项是否存在
- $menuExists = AdminMenu::where('uri', $uri)->exists();
-
- if ($menuExists) {
- $totalMenus++;
- $moduleMenus++;
- $this->info(" - {$controllerName} => {$uri} [✓]");
- } else {
- $missingMenus[] = [
- 'module' => $module,
- 'controller' => $controllerName,
- 'uri' => $uri
- ];
- $this->error(" - {$controllerName} => {$uri} [✗]");
- }
- }
-
- $this->info(" {$module}模块: {$moduleMenus}/{$moduleControllers} 个控制器有对应的菜单项");
- }
-
- $this->info("\n总计: {$totalMenus}/{$totalControllers} 个控制器有对应的菜单项");
-
- if (count($missingMenus) > 0) {
- $this->info("\n缺失的菜单项:");
- foreach ($missingMenus as $menu) {
- $this->warn(" - {$menu['module']}模块: {$menu['controller']} => {$menu['uri']}");
- }
-
- if ($this->confirm('是否要修复这些缺失的菜单项?')) {
- foreach ($modules as $module) {
- $this->call('admin:fix-menus', ['module' => $module]);
- }
- }
- } else {
- $this->info("\n所有控制器都有对应的菜单项!");
- }
-
- return 0;
- }
-
- /**
- * 获取控制器的URI
- *
- * @param string $controllerName
- * @return string
- */
- protected function getControllerUri(string $controllerName): string
- {
- // 移除Controller后缀
- $name = str_replace('Controller', '', $controllerName);
-
- // 转换为kebab-case
- $uri = Str::kebab($name);
-
- return $uri;
- }
- }
|