AppServiceProvider.php 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. // 通过代理访问时强制 https(检测 X-Forwarded-Proto 头)
  37. // 外部用户通过 https 代理访问 → 生成 https 链接
  38. // 内部容器通信 → 保持 http 链接
  39. if (request()->header('X-Forwarded-Proto') === 'https') {
  40. URL::forceScheme('https');
  41. }
  42. // 注册年级格式化 Blade 组件
  43. Blade::directive('formatGrade', function ($expression) {
  44. return "<?php echo format_grade($expression); ?>";
  45. });
  46. MarkdownImport::updated(function (MarkdownImport $import): void {
  47. try {
  48. Redis::publish('markdown-imports', json_encode([
  49. 'type' => 'markdown-imports',
  50. 'id' => $import->id,
  51. 'status' => $import->status,
  52. 'progress_stage' => $import->progress_stage,
  53. 'progress_message' => $import->progress_message,
  54. 'parsed_count' => $import->parsed_count ?? null,
  55. 'accepted_count' => $import->accepted_count ?? null,
  56. 'import_id' => $import->id,
  57. ], JSON_UNESCAPED_UNICODE));
  58. } catch (\Throwable $e) {
  59. // SSE publish failed, ignore to avoid affecting business flow.
  60. }
  61. });
  62. PreQuestionCandidate::updated(function (PreQuestionCandidate $candidate): void {
  63. try {
  64. Redis::publish('pre-question-candidates', json_encode([
  65. 'type' => 'pre-question-candidates',
  66. 'id' => $candidate->id,
  67. 'import_id' => $candidate->import_id,
  68. 'status' => $candidate->status,
  69. 'is_question_candidate' => $candidate->is_question_candidate,
  70. ], JSON_UNESCAPED_UNICODE));
  71. } catch (\Throwable $e) {
  72. // SSE publish failed, ignore to avoid affecting business flow.
  73. }
  74. });
  75. }
  76. }