LogServiceProvider.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. namespace Modules\Log\Providers;
  3. use Illuminate\Support\ServiceProvider;
  4. use Illuminate\Database\Eloquent\Factory;
  5. class LogServiceProvider extends ServiceProvider
  6. {
  7. /**
  8. * @var string $moduleName
  9. */
  10. protected $moduleName = 'Log';
  11. /**
  12. * @var string $moduleNameLower
  13. */
  14. protected $moduleNameLower = 'log';
  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->registerFactories();
  26. $this->loadMigrationsFrom(module_path($this->moduleName, 'Database/Migrations'));
  27. }
  28. /**
  29. * Register the service provider.
  30. *
  31. * @return void
  32. */
  33. public function register()
  34. {
  35. $this->app->register(RouteServiceProvider::class);
  36. }
  37. /**
  38. * Register config.
  39. *
  40. * @return void
  41. */
  42. protected function registerConfig()
  43. {
  44. $this->publishes([
  45. module_path($this->moduleName, 'Config/config.php') => config_path($this->moduleNameLower . '.php'),
  46. ], 'config');
  47. $this->mergeConfigFrom(
  48. module_path($this->moduleName, 'Config/config.php'), $this->moduleNameLower
  49. );
  50. }
  51. /**
  52. * Register views.
  53. *
  54. * @return void
  55. */
  56. public function registerViews()
  57. {
  58. $viewPath = resource_path('views/modules/' . $this->moduleNameLower);
  59. $sourcePath = module_path($this->moduleName, 'Resources/views');
  60. $this->publishes([
  61. $sourcePath => $viewPath
  62. ], ['views', $this->moduleNameLower . '-module-views']);
  63. $this->loadViewsFrom(array_merge($this->getPublishableViewPaths(), [$sourcePath]), $this->moduleNameLower);
  64. }
  65. /**
  66. * Register translations.
  67. *
  68. * @return void
  69. */
  70. public function registerTranslations()
  71. {
  72. $langPath = resource_path('lang/modules/' . $this->moduleNameLower);
  73. if (is_dir($langPath)) {
  74. $this->loadTranslationsFrom($langPath, $this->moduleNameLower);
  75. } else {
  76. $this->loadTranslationsFrom(module_path($this->moduleName, 'Resources/lang'), $this->moduleNameLower);
  77. }
  78. }
  79. /**
  80. * Register an additional directory of factories.
  81. *
  82. * @return void
  83. */
  84. public function registerFactories()
  85. {
  86. if (! app()->environment('production') && $this->app->runningInConsole()) {
  87. app(Factory::class)->load(module_path($this->moduleName, 'Database/factories'));
  88. }
  89. }
  90. /**
  91. * Get the services provided by the provider.
  92. *
  93. * @return array
  94. */
  95. public function provides()
  96. {
  97. return [];
  98. }
  99. private function getPublishableViewPaths(): array
  100. {
  101. $paths = [];
  102. foreach (\Config::get('view.paths') as $path) {
  103. if (is_dir($path . '/modules/' . $this->moduleNameLower)) {
  104. $paths[] = $path . '/modules/' . $this->moduleNameLower;
  105. }
  106. }
  107. return $paths;
  108. }
  109. }