FundServiceProvider.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. namespace App\Module\Fund\Providers;
  3. use App\Module\AppGame\Events\LoginSuccessEvent;
  4. use App\Module\Fund\Events\FundChangedEvent;
  5. use App\Module\Fund\Events\FundStatusChangedEvent;
  6. use App\Module\Fund\Events\FundTransferEvent;
  7. use App\Module\Fund\Listeners\FundEventListener;
  8. use App\Module\Fund\Listeners\LoginSuccessListener;
  9. use Illuminate\Support\ServiceProvider;
  10. /**
  11. * 资金模块服务提供者
  12. */
  13. class FundServiceProvider extends ServiceProvider
  14. {
  15. /**
  16. * 事件到监听器的映射
  17. *
  18. * @var array
  19. */
  20. protected $listen = [
  21. FundChangedEvent::class => [
  22. FundEventListener::class . '@handleFundChanged',
  23. ],
  24. FundTransferEvent::class => [
  25. FundEventListener::class . '@handleFundTransfer',
  26. ],
  27. FundStatusChangedEvent::class => [
  28. FundEventListener::class . '@handleFundStatusChanged',
  29. ],
  30. LoginSuccessEvent::class => [
  31. LoginSuccessListener::class . '@handle',
  32. ],
  33. ];
  34. /**
  35. * 需要注册的订阅者
  36. *
  37. * @var array
  38. */
  39. protected $subscribe = [
  40. FundEventListener::class,
  41. ];
  42. /**
  43. * 注册服务
  44. *
  45. * @return void
  46. */
  47. public function register(): void
  48. {
  49. // 注册服务...
  50. $this->app->singleton('fund.account.service', function ($app) {
  51. return new \App\Module\Fund\Services\AccountService();
  52. });
  53. $this->app->singleton('fund.log.service', function ($app) {
  54. return new \App\Module\Fund\Services\LogService();
  55. });
  56. }
  57. /**
  58. * 启动服务
  59. *
  60. * @return void
  61. */
  62. public function boot(): void
  63. {
  64. // 注册事件监听器
  65. $this->registerEvents();
  66. // 注册命令
  67. if ($this->app->runningInConsole()) {
  68. $this->commands([
  69. \App\Module\Fund\Commands\CheckUserLogsCommand::class,
  70. \App\Module\Fund\Commands\TestLoginAccountCreationCommand::class,
  71. ]);
  72. }
  73. }
  74. /**
  75. * 注册事件和监听器
  76. *
  77. * @return void
  78. */
  79. protected function registerEvents(): void
  80. {
  81. $events = $this->app['events'];
  82. foreach ($this->listen as $event => $listeners) {
  83. foreach ($listeners as $listener) {
  84. $events->listen($event, $listener);
  85. }
  86. }
  87. foreach ($this->subscribe as $subscriber) {
  88. $events->subscribe($subscriber);
  89. }
  90. }
  91. }