AppServiceProvider.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. // 只有当 APP_URL 是 https 时才强制 https(容器内部通信用 http)
  37. if (config('app.env') === 'production' && str_starts_with(config('app.url'), 'https://')) {
  38. URL::forceScheme('https');
  39. }
  40. // 注册年级格式化 Blade 组件
  41. Blade::directive('formatGrade', function ($expression) {
  42. return "<?php echo format_grade($expression); ?>";
  43. });
  44. MarkdownImport::updated(function (MarkdownImport $import): void {
  45. try {
  46. Redis::publish('markdown-imports', json_encode([
  47. 'type' => 'markdown-imports',
  48. 'id' => $import->id,
  49. 'status' => $import->status,
  50. 'progress_stage' => $import->progress_stage,
  51. 'progress_message' => $import->progress_message,
  52. 'parsed_count' => $import->parsed_count ?? null,
  53. 'accepted_count' => $import->accepted_count ?? null,
  54. 'import_id' => $import->id,
  55. ], JSON_UNESCAPED_UNICODE));
  56. } catch (\Throwable $e) {
  57. // SSE publish failed, ignore to avoid affecting business flow.
  58. }
  59. });
  60. PreQuestionCandidate::updated(function (PreQuestionCandidate $candidate): void {
  61. try {
  62. Redis::publish('pre-question-candidates', json_encode([
  63. 'type' => 'pre-question-candidates',
  64. 'id' => $candidate->id,
  65. 'import_id' => $candidate->import_id,
  66. 'status' => $candidate->status,
  67. 'is_question_candidate' => $candidate->is_question_candidate,
  68. ], JSON_UNESCAPED_UNICODE));
  69. } catch (\Throwable $e) {
  70. // SSE publish failed, ignore to avoid affecting business flow.
  71. }
  72. });
  73. }
  74. }