MexServiceProvider.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. namespace App\Module\Mex\Providers;
  3. use Illuminate\Support\ServiceProvider;
  4. use Illuminate\Console\Scheduling\Schedule;
  5. use Illuminate\Support\Facades\Event;
  6. /**
  7. * Mex模块服务提供者
  8. *
  9. * 负责注册Mex模块的服务、命令、计划任务等
  10. */
  11. class MexServiceProvider extends ServiceProvider
  12. {
  13. /**
  14. * 注册服务
  15. */
  16. public function register(): void
  17. {
  18. // 注册配置文件
  19. $this->mergeConfigFrom(
  20. __DIR__ . '/../Config/mex.php',
  21. 'mex'
  22. );
  23. // 注册单例服务 ;不注册服务
  24. // $this->registerServices();
  25. }
  26. /**
  27. * 启动服务
  28. */
  29. public function boot(): void
  30. {
  31. // 注册命令
  32. $this->registerCommands();
  33. // 注册计划任务;不注册计划任务,计划任务由项目组织
  34. // $this->registerSchedule();
  35. // 注册事件监听器
  36. $this->registerEventListeners();
  37. // 发布配置文件
  38. $this->publishes([
  39. __DIR__ . '/../Config/mex.php' => config_path('mex.php'),
  40. ], 'mex-config');
  41. }
  42. /**
  43. * 注册命令
  44. */
  45. protected function registerCommands(): void
  46. {
  47. if ($this->app->runningInConsole()) {
  48. $this->commands([
  49. \App\Module\Mex\Commands\MexUserBuyItemMatchCommand::class,
  50. \App\Module\Mex\Commands\MexUserSellItemMatchCommand::class,
  51. \App\Module\Mex\Commands\MexTestCommand::class,
  52. \App\Module\Mex\Commands\TestMultiCurrencyCommand::class,
  53. ]);
  54. }
  55. }
  56. /**
  57. * 注册事件监听器
  58. */
  59. protected function registerEventListeners(): void
  60. {
  61. Event::listen(
  62. \App\Module\Mex\Events\OrderCreatedEvent::class,
  63. \App\Module\Mex\Listeners\OrderCreatedListener::class
  64. );
  65. Event::listen(
  66. \App\Module\Mex\Events\OrderCompletedEvent::class,
  67. [\App\Module\Mex\Listeners\OrderCreatedListener::class, 'handleCompleted']
  68. );
  69. Event::listen(
  70. \App\Module\Mex\Events\TransactionCreatedEvent::class,
  71. \App\Module\Mex\Listeners\TransactionCreatedListener::class
  72. );
  73. }
  74. }