AppServiceProvider.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. namespace App\Providers;
  3. use Illuminate\Support\ServiceProvider;
  4. use Illuminate\Support\Facades\URL;
  5. use Illuminate\Support\Facades\Redis;
  6. use Illuminate\Support\Facades\Blade;
  7. use App\Models\MarkdownImport;
  8. use App\Models\PreQuestionCandidate;
  9. class AppServiceProvider extends ServiceProvider
  10. {
  11. /**
  12. * Register any application services.
  13. */
  14. public function register(): void
  15. {
  16. // 注册 MathRecSys 服务
  17. $this->app->singleton(\App\Services\MathRecSysService::class, function () {
  18. return new \App\Services\MathRecSysService();
  19. });
  20. // 注册知识图谱服务
  21. $this->app->singleton(\App\Services\KnowledgeGraphService::class, function ($app) {
  22. return new \App\Services\KnowledgeGraphService(
  23. $app->make(\App\Services\MathRecSysService::class)
  24. );
  25. });
  26. // 注册PDF合并工具
  27. $this->app->singleton(\App\Services\PdfMerger::class, function () {
  28. return new \App\Services\PdfMerger();
  29. });
  30. }
  31. /**
  32. * Bootstrap any application services.
  33. */
  34. public function boot(): void
  35. {
  36. if (config('app.env') === 'production') {
  37. URL::forceScheme('https');
  38. }
  39. // 注册年级格式化 Blade 组件
  40. Blade::directive('formatGrade', function ($expression) {
  41. return "<?php echo format_grade($expression); ?>";
  42. });
  43. MarkdownImport::updated(function (MarkdownImport $import): void {
  44. try {
  45. Redis::publish('markdown-imports', json_encode([
  46. 'type' => 'markdown-imports',
  47. 'id' => $import->id,
  48. 'status' => $import->status,
  49. 'progress_stage' => $import->progress_stage,
  50. 'progress_message' => $import->progress_message,
  51. 'parsed_count' => $import->parsed_count ?? null,
  52. 'accepted_count' => $import->accepted_count ?? null,
  53. 'import_id' => $import->id,
  54. ], JSON_UNESCAPED_UNICODE));
  55. } catch (\Throwable $e) {
  56. // SSE publish failed, ignore to avoid affecting business flow.
  57. }
  58. });
  59. PreQuestionCandidate::updated(function (PreQuestionCandidate $candidate): void {
  60. try {
  61. Redis::publish('pre-question-candidates', json_encode([
  62. 'type' => 'pre-question-candidates',
  63. 'id' => $candidate->id,
  64. 'import_id' => $candidate->import_id,
  65. 'status' => $candidate->status,
  66. 'is_question_candidate' => $candidate->is_question_candidate,
  67. ], JSON_UNESCAPED_UNICODE));
  68. } catch (\Throwable $e) {
  69. // SSE publish failed, ignore to avoid affecting business flow.
  70. }
  71. });
  72. }
  73. }