SocialFarmServiceProvider.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. namespace App\Module\SocialFarm\Providers;
  3. use Illuminate\Support\ServiceProvider;
  4. use Illuminate\Support\Facades\Event;
  5. /**
  6. * SocialFarm模块服务提供者
  7. */
  8. class SocialFarmServiceProvider extends ServiceProvider
  9. {
  10. /**
  11. * 注册服务
  12. */
  13. public function register(): void
  14. {
  15. // 注册配置文件
  16. $this->mergeConfigFrom(
  17. __DIR__ . '/../config/socialfarm.php',
  18. 'socialfarm'
  19. );
  20. }
  21. /**
  22. * 启动服务
  23. */
  24. public function boot(): void
  25. {
  26. // 发布配置文件
  27. if ($this->app->runningInConsole()) {
  28. $this->publishes([
  29. __DIR__ . '/../config/socialfarm.php' => config_path('socialfarm.php'),
  30. ], 'socialfarm-config');
  31. }
  32. // 注册事件监听器
  33. $this->registerEventListeners();
  34. // 注册命令
  35. $this->registerCommands();
  36. }
  37. /**
  38. * 注册事件监听器
  39. */
  40. protected function registerEventListeners(): void
  41. {
  42. // 偷菜成功事件
  43. Event::listen(
  44. \App\Module\SocialFarm\Events\CropStolenEvent::class,
  45. \App\Module\SocialFarm\Listeners\SendStealNotificationListener::class
  46. );
  47. Event::listen(
  48. \App\Module\SocialFarm\Events\CropStolenEvent::class,
  49. \App\Module\SocialFarm\Listeners\UpdateSocialStatsListener::class
  50. );
  51. // 农场访问事件
  52. Event::listen(
  53. \App\Module\SocialFarm\Events\FarmVisitedEvent::class,
  54. \App\Module\SocialFarm\Listeners\UpdateSocialStatsListener::class
  55. );
  56. // 互助行为事件
  57. Event::listen(
  58. \App\Module\SocialFarm\Events\HelpActionEvent::class,
  59. \App\Module\SocialFarm\Listeners\UpdateSocialStatsListener::class
  60. );
  61. }
  62. /**
  63. * 注册命令
  64. */
  65. protected function registerCommands(): void
  66. {
  67. if ($this->app->runningInConsole()) {
  68. $this->commands([
  69. \App\Module\SocialFarm\Commands\CleanExpiredLogsCommand::class,
  70. \App\Module\SocialFarm\Commands\SocialFarmStatsCommand::class,
  71. ]);
  72. }
  73. }
  74. /**
  75. * 获取提供的服务
  76. */
  77. public function provides(): array
  78. {
  79. return [];
  80. }
  81. }