FundServiceProvider.php 2.2 KB

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