| 12345678910111213141516171819202122232425262728293031323334353637383940414243 |
- <?php
- namespace App\Console\Commands;
- use App\Services\KnowledgePointQuestionStatsService;
- use Illuminate\Console\Command;
- use Illuminate\Support\Facades\File;
- class KnowledgePointQuestionStatsMarkdownCommand extends Command
- {
- protected $signature = 'stats:kp-questions-md
- {--output= : 写入文件路径(绝对路径、或项目根下 docs/xxx、或相对于 storage/app)}';
- protected $description = '输出知识点题量统计 Markdown 表(questions / questions_tem 不重复)';
- public function handle(KnowledgePointQuestionStatsService $svc): int
- {
- $rows = $svc->buildRows();
- $md = $svc->toMarkdownTable($rows);
- $path = $this->option('output');
- if ($path) {
- $p = ltrim((string) $path, '/');
- if (str_starts_with($p, 'docs/')) {
- $full = base_path($p);
- } elseif (str_starts_with((string) $path, '/') || (strlen((string) $path) > 2 && preg_match('/^[A-Za-z]:\\\\/', (string) $path))) {
- $full = (string) $path;
- } else {
- $full = storage_path('app/'.$p);
- }
- $dir = dirname($full);
- if (! is_dir($dir)) {
- File::makeDirectory($dir, 0755, true);
- }
- file_put_contents($full, $md);
- $this->info('已写入:'.$full);
- }
- $this->line($md);
- return self::SUCCESS;
- }
- }
|