PointServiceProvider.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. namespace App\Module\Point\Providers;
  3. use Illuminate\Support\ServiceProvider;
  4. /**
  5. * 积分模块服务提供者
  6. *
  7. * 负责注册积分模块的服务、配置和路由
  8. */
  9. class PointServiceProvider extends ServiceProvider
  10. {
  11. /**
  12. * 注册服务
  13. *
  14. * @return void
  15. */
  16. public function register()
  17. {
  18. // 注册积分服务
  19. $this->app->singleton('point.service', function ($app) {
  20. return new \App\Module\Point\Services\PointService(0, 0);
  21. });
  22. // 注册积分账户服务
  23. $this->app->singleton('point.account.service', function ($app) {
  24. return new \App\Module\Point\Services\AccountService();
  25. });
  26. // 注册积分类型服务
  27. $this->app->singleton('point.currency.service', function ($app) {
  28. return new \App\Module\Point\Services\CurrencyService();
  29. });
  30. // 注册积分日志服务
  31. $this->app->singleton('point.log.service', function ($app) {
  32. return new \App\Module\Point\Services\LogService();
  33. });
  34. }
  35. /**
  36. * 启动服务
  37. *
  38. * @return void
  39. */
  40. public function boot()
  41. {
  42. // 加载路由
  43. $this->loadRoutes();
  44. // 加载视图
  45. $this->loadViews();
  46. // 加载配置
  47. $this->loadConfigs();
  48. // 发布资源
  49. $this->publishResources();
  50. }
  51. /**
  52. * 加载路由
  53. *
  54. * @return void
  55. */
  56. protected function loadRoutes()
  57. {
  58. // 加载API路由
  59. if (file_exists($apiRoutes = __DIR__ . '/../routes/api.php')) {
  60. $this->loadRoutesFrom($apiRoutes);
  61. }
  62. // 加载Web路由
  63. if (file_exists($webRoutes = __DIR__ . '/../routes/web.php')) {
  64. $this->loadRoutesFrom($webRoutes);
  65. }
  66. // 加载后台路由
  67. if (file_exists($adminRoutes = __DIR__ . '/../routes/admin.php')) {
  68. $this->loadRoutesFrom($adminRoutes);
  69. }
  70. }
  71. /**
  72. * 加载视图
  73. *
  74. * @return void
  75. */
  76. protected function loadViews()
  77. {
  78. $viewPath = __DIR__ . '/../resources/views';
  79. if (is_dir($viewPath)) {
  80. $this->loadViewsFrom($viewPath, 'point');
  81. }
  82. }
  83. /**
  84. * 加载配置
  85. *
  86. * @return void
  87. */
  88. protected function loadConfigs()
  89. {
  90. $configPath = __DIR__ . '/../config';
  91. if (is_dir($configPath)) {
  92. $configs = glob($configPath . '/*.php');
  93. foreach ($configs as $config) {
  94. $name = 'point.' . basename($config, '.php');
  95. $this->mergeConfigFrom($config, $name);
  96. }
  97. }
  98. }
  99. /**
  100. * 发布资源
  101. *
  102. * @return void
  103. */
  104. protected function publishResources()
  105. {
  106. // 发布配置文件
  107. $configPath = __DIR__ . '/../config';
  108. if (is_dir($configPath)) {
  109. $this->publishes([
  110. $configPath => config_path('point'),
  111. ], 'point-config');
  112. }
  113. // 发布数据库迁移文件
  114. $migrationPath = __DIR__ . '/../database/migrations';
  115. if (is_dir($migrationPath)) {
  116. $this->publishes([
  117. $migrationPath => database_path('migrations'),
  118. ], 'point-migrations');
  119. }
  120. // 发布视图文件
  121. $viewPath = __DIR__ . '/../resources/views';
  122. if (is_dir($viewPath)) {
  123. $this->publishes([
  124. $viewPath => resource_path('views/vendor/point'),
  125. ], 'point-views');
  126. }
  127. // 发布前端资源
  128. $assetsPath = __DIR__ . '/../resources/assets';
  129. if (is_dir($assetsPath)) {
  130. $this->publishes([
  131. $assetsPath => public_path('vendor/point'),
  132. ], 'point-assets');
  133. }
  134. }
  135. /**
  136. * 获取提供的服务
  137. *
  138. * @return array
  139. */
  140. public function provides()
  141. {
  142. return [
  143. 'point.service',
  144. 'point.account.service',
  145. 'point.currency.service',
  146. 'point.log.service',
  147. ];
  148. }
  149. }