ScanModelsCommand.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. namespace App\Module\Cleanup\Commands;
  3. use App\Module\Cleanup\Logics\ModelScannerLogic;
  4. use Illuminate\Console\Command;
  5. /**
  6. * 扫描Model类命令
  7. *
  8. * 用于扫描系统中的Model类并生成清理配置
  9. */
  10. class ScanModelsCommand extends Command
  11. {
  12. /**
  13. * 命令签名
  14. */
  15. protected $signature = 'cleanup:scan-models
  16. {--force : 强制刷新所有Model配置}
  17. {--show-details : 显示详细信息}';
  18. /**
  19. * 命令描述
  20. */
  21. protected $description = '扫描系统中的Model类并生成清理配置';
  22. /**
  23. * 执行命令
  24. */
  25. public function handle(): int
  26. {
  27. $this->info('开始扫描Model类...');
  28. $this->newLine();
  29. $forceRefresh = $this->option('force');
  30. $showDetails = $this->option('show-details');
  31. if ($forceRefresh) {
  32. $this->warn('强制刷新模式:将重新生成所有Model的配置');
  33. }
  34. try {
  35. // 执行扫描
  36. $result = ModelScannerLogic::scanAllModels($forceRefresh);
  37. // 显示扫描结果
  38. $this->displayScanResults($result, $showDetails);
  39. $this->newLine();
  40. $this->info('✅ Model类扫描完成!');
  41. return Command::SUCCESS;
  42. } catch (\Exception $e) {
  43. $this->error('❌ 扫描失败: ' . $e->getMessage());
  44. $this->error('详细错误: ' . $e->getTraceAsString());
  45. return Command::FAILURE;
  46. }
  47. }
  48. /**
  49. * 显示扫描结果
  50. */
  51. private function displayScanResults(array $result, bool $showDetails): void
  52. {
  53. // 显示统计信息
  54. $this->table(
  55. ['项目', '数量'],
  56. [
  57. ['总Model数', $result['total_models']],
  58. ['已扫描', $result['scanned_models']],
  59. ['新增配置', $result['new_models']],
  60. ['更新配置', $result['updated_models']],
  61. ['扫描耗时', $result['scan_time'] . 's'],
  62. ]
  63. );
  64. if ($showDetails && !empty($result['models'])) {
  65. $this->newLine();
  66. $this->info('📋 详细信息:');
  67. $tableData = [];
  68. foreach ($result['models'] as $model) {
  69. $status = $model['is_new'] ? '新增' : ($model['is_updated'] ? '更新' : '跳过');
  70. $tableData[] = [
  71. $model['model_class'],
  72. $model['table_name'],
  73. $model['module_name'],
  74. $model['data_category']->getDescription(),
  75. $status,
  76. ];
  77. }
  78. $this->table(
  79. ['Model类', '表名', '模块', '数据分类', '状态'],
  80. $tableData
  81. );
  82. }
  83. // 显示模块统计
  84. if (!empty($result['models'])) {
  85. $moduleStats = [];
  86. foreach ($result['models'] as $model) {
  87. $module = $model['module_name'];
  88. if (!isset($moduleStats[$module])) {
  89. $moduleStats[$module] = 0;
  90. }
  91. $moduleStats[$module]++;
  92. }
  93. $this->newLine();
  94. $this->info('📊 模块统计:');
  95. $moduleTableData = [];
  96. foreach ($moduleStats as $module => $count) {
  97. $moduleTableData[] = [$module, $count];
  98. }
  99. $this->table(['模块名', 'Model数量'], $moduleTableData);
  100. }
  101. }
  102. }