FinanceServiceProvider.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. namespace Modules\Finance\Providers;
  3. use Illuminate\Support\ServiceProvider;
  4. use Illuminate\Database\Eloquent\Factory;
  5. class FinanceServiceProvider extends ServiceProvider
  6. {
  7. /**
  8. * @var string $moduleName
  9. */
  10. protected $moduleName = 'Finance';
  11. /**
  12. * @var string $moduleNameLower
  13. */
  14. protected $moduleNameLower = 'finance';
  15. /**
  16. * Boot the application events.
  17. *
  18. * @return void
  19. */
  20. public function boot()
  21. {
  22. $this->registerTranslations();
  23. $this->registerConfig();
  24. $this->registerViews();
  25. $this->loadMigrationsFrom(module_path($this->moduleName, 'Database/Migrations'));
  26. }
  27. /**
  28. * Register the service provider.
  29. *
  30. * @return void
  31. */
  32. public function register()
  33. {
  34. $this->app->register(RouteServiceProvider::class);
  35. }
  36. /**
  37. * Register config.
  38. *
  39. * @return void
  40. */
  41. protected function registerConfig()
  42. {
  43. $this->publishes([
  44. module_path($this->moduleName, 'Config/config.php') => config_path($this->moduleNameLower . '.php'),
  45. ], 'config');
  46. $this->mergeConfigFrom(
  47. module_path($this->moduleName, 'Config/config.php'), $this->moduleNameLower
  48. );
  49. }
  50. /**
  51. * Register views.
  52. *
  53. * @return void
  54. */
  55. public function registerViews()
  56. {
  57. $viewPath = resource_path('views/modules/' . $this->moduleNameLower);
  58. $sourcePath = module_path($this->moduleName, 'Resources/views');
  59. $this->publishes([
  60. $sourcePath => $viewPath
  61. ], ['views', $this->moduleNameLower . '-module-views']);
  62. $this->loadViewsFrom(array_merge($this->getPublishableViewPaths(), [$sourcePath]), $this->moduleNameLower);
  63. }
  64. /**
  65. * Register translations.
  66. *
  67. * @return void
  68. */
  69. public function registerTranslations()
  70. {
  71. $langPath = resource_path('lang/modules/' . $this->moduleNameLower);
  72. if (is_dir($langPath)) {
  73. $this->loadTranslationsFrom($langPath, $this->moduleNameLower);
  74. } else {
  75. $this->loadTranslationsFrom(module_path($this->moduleName, 'Resources/lang'), $this->moduleNameLower);
  76. }
  77. }
  78. /**
  79. * Get the services provided by the provider.
  80. *
  81. * @return array
  82. */
  83. public function provides()
  84. {
  85. return [];
  86. }
  87. private function getPublishableViewPaths(): array
  88. {
  89. $paths = [];
  90. foreach (\Config::get('view.paths') as $path) {
  91. if (is_dir($path . '/modules/' . $this->moduleNameLower)) {
  92. $paths[] = $path . '/modules/' . $this->moduleNameLower;
  93. }
  94. }
  95. return $paths;
  96. }
  97. }