KnowledgePointQuestionStatsMarkdownCommand.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Services\KnowledgePointQuestionStatsService;
  4. use Illuminate\Console\Command;
  5. use Illuminate\Support\Facades\File;
  6. class KnowledgePointQuestionStatsMarkdownCommand extends Command
  7. {
  8. protected $signature = 'stats:kp-questions-md
  9. {--output= : 写入文件路径(绝对路径、或项目根下 docs/xxx、或相对于 storage/app)}';
  10. protected $description = '输出知识点题量统计 Markdown 表(questions / questions_tem 不重复)';
  11. public function handle(KnowledgePointQuestionStatsService $svc): int
  12. {
  13. $rows = $svc->buildRows();
  14. $md = $svc->toMarkdownTable($rows);
  15. $path = $this->option('output');
  16. if ($path) {
  17. $p = ltrim((string) $path, '/');
  18. if (str_starts_with($p, 'docs/')) {
  19. $full = base_path($p);
  20. } elseif (str_starts_with((string) $path, '/') || (strlen((string) $path) > 2 && preg_match('/^[A-Za-z]:\\\\/', (string) $path))) {
  21. $full = (string) $path;
  22. } else {
  23. $full = storage_path('app/'.$p);
  24. }
  25. $dir = dirname($full);
  26. if (! is_dir($dir)) {
  27. File::makeDirectory($dir, 0755, true);
  28. }
  29. file_put_contents($full, $md);
  30. $this->info('已写入:'.$full);
  31. }
  32. $this->line($md);
  33. return self::SUCCESS;
  34. }
  35. }