GenerateJudgeCardTemplateCommand.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Support\JudgeCardTemplateBuilder;
  4. use Illuminate\Console\Command;
  5. use Illuminate\Support\Facades\File;
  6. class GenerateJudgeCardTemplateCommand extends Command
  7. {
  8. protected $signature = 'exam:generate-judge-card-template {--path= : 输出文件路径,默认项目根目录 judge_card.template.json}';
  9. protected $description = '根据判卷卡排版参数生成 Python 识别模板 JSON';
  10. public function handle(JudgeCardTemplateBuilder $builder): int
  11. {
  12. $outputPath = (string) ($this->option('path') ?: base_path('judge_card.template.json'));
  13. $directory = dirname($outputPath);
  14. if (! is_dir($directory)) {
  15. File::ensureDirectoryExists($directory);
  16. }
  17. $template = $builder->build();
  18. $json = json_encode($template, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
  19. if ($json === false) {
  20. $this->error('生成 judge_card.template.json 失败:JSON 编码错误');
  21. return self::FAILURE;
  22. }
  23. File::put($outputPath, $json.PHP_EOL);
  24. $this->info("判卷卡模板已生成:{$outputPath}");
  25. return self::SUCCESS;
  26. }
  27. }