From d425133caef5bb4907a2d00872af08947dce7ca1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Kv=C3=A1=C4=8Dek?= Date: Wed, 12 Oct 2016 17:38:06 +0200 Subject: [PATCH] REFACTORING: refactor module service providers to reduce amoount of duplicated code --- app/Providers/ModuleServiceProvider.php | 77 +++++++++++++ .../Providers/ArticleEventServiceProvider.php | 11 +- .../Providers/ArticleServiceProvider.php | 109 ++++-------------- .../Providers/ArticleListServiceProvider.php | 84 +------------- .../Providers/CategoriesServiceProvider.php | 84 +------------- .../Providers/ContactFormServiceProvider.php | 73 +++--------- .../Providers/GalleryServiceProvider.php | 88 +------------- .../Providers/StaticPagesServiceProvider.php | 69 +---------- 8 files changed, 143 insertions(+), 452 deletions(-) create mode 100644 app/Providers/ModuleServiceProvider.php diff --git a/app/Providers/ModuleServiceProvider.php b/app/Providers/ModuleServiceProvider.php new file mode 100644 index 00000000..d9d338ce --- /dev/null +++ b/app/Providers/ModuleServiceProvider.php @@ -0,0 +1,77 @@ +setModuleAlias(); + $this->setModulePath(); + + $this->registerTranslations(); + $this->registerConfig(); + $this->registerViews(); + } + + /** + * Register config. + * + * @return void + */ + protected function registerConfig() + { + $this->publishes([$this->path . '/../Config/config.php' => config_path($this->alias.'.php')]); + $this->mergeConfigFrom($this->path . '/../Config/config.php', $this->alias); + } + + public function register() + { + } + + /** + * Register views. + * + * @return void + */ + protected function registerViews() + { + $viewPath = base_path('resources/views/modules/'.$this->alias); + $sourcePath = $this->path . '/../Resources/views'; + + $this->publishes([$sourcePath => $viewPath], 'module-views'); + + $this->loadViewsFrom(array_merge(array_map(function ($path) { + return $path . '/modules/'.$this->alias; + }, \Config::get('view.paths')), [$sourcePath]), $this->alias); + } + + /** + * Register translations. + * + * @return void + */ + protected function registerTranslations() + { + $this->loadTranslationsFrom($this->path . '/../Resources/lang', $this->alias); + } + +} \ No newline at end of file diff --git a/modules/Blog/Article/Providers/ArticleEventServiceProvider.php b/modules/Blog/Article/Providers/ArticleEventServiceProvider.php index 13059628..1004bfd3 100644 --- a/modules/Blog/Article/Providers/ArticleEventServiceProvider.php +++ b/modules/Blog/Article/Providers/ArticleEventServiceProvider.php @@ -20,12 +20,17 @@ class ArticleEventServiceProvider extends ServiceProvider */ protected $listen = [ ApprovedComment::class => [LogCommentApproval::class], - DeletedComment::class => [LogDeletedComment::class], - CreatedComment::class => [LogNewComment::class], + DeletedComment::class => [LogDeletedComment::class], + CreatedComment::class => [LogNewComment::class], ]; public function boot(DispatcherContract $events) { - parent::boot($events); // TODO: Change the autogenerated stub + parent::boot($events); + } + + protected function setModulePath() + { + $this->path = __DIR__; } } \ No newline at end of file diff --git a/modules/Blog/Article/Providers/ArticleServiceProvider.php b/modules/Blog/Article/Providers/ArticleServiceProvider.php index 188e2fef..ccfe2120 100644 --- a/modules/Blog/Article/Providers/ArticleServiceProvider.php +++ b/modules/Blog/Article/Providers/ArticleServiceProvider.php @@ -1,95 +1,28 @@ app->register(ArticleEventServiceProvider::class); + } - /** - * Boot the application events. - * - * @return void - */ - public function boot() - { - $this->registerTranslations(); - $this->registerConfig(); - $this->registerViews(); - - $this->app->register(ArticleEventServiceProvider::class); - } - - /** - * Register the service provider. - * - * @return void - */ - public function register() - { - } - - /** - * Register config. - * - * @return void - */ - protected function registerConfig() - { - $this->publishes([__DIR__.'/../Config/config.php' => config_path('article.php')]); - $this->mergeConfigFrom(__DIR__.'/../Config/config.php', 'article'); - } - - /** - * Register views. - * - * @return void - */ - public function registerViews() - { - $viewPath = base_path('resources/views/modules/article'); - $sourcePath = __DIR__.'/../Resources/views'; - - $this->publishes([$sourcePath => $viewPath], 'module-views'); - - $this->loadViewsFrom(array_merge(array_map(function ($path) { - return $path . '/modules/article'; - }, \Config::get('view.paths')), [$sourcePath]), 'article'); - } - - /** - * Register translations. - * - * @return void - */ - public function registerTranslations() - { - $langPath = base_path('resources/lang/modules/article'); - - if (is_dir($langPath)) { - $this->loadTranslationsFrom($langPath, 'article'); - } else { - $this->loadTranslationsFrom(__DIR__ .'/../Resources/lang', 'article'); - } - } - - /** - * Get the services provided by the provider. - * - * @return array - */ - public function provides() - { - return array(); - } + protected function setModuleAlias() + { + $this->alias = 'article'; + } + protected function setModulePath() + { + $this->path = __DIR__; + } } diff --git a/modules/Blog/ArticleList/Providers/ArticleListServiceProvider.php b/modules/Blog/ArticleList/Providers/ArticleListServiceProvider.php index 439e4afb..280c1f84 100644 --- a/modules/Blog/ArticleList/Providers/ArticleListServiceProvider.php +++ b/modules/Blog/ArticleList/Providers/ArticleListServiceProvider.php @@ -1,88 +1,16 @@ registerTranslations(); - $this->registerConfig(); - $this->registerViews(); + $this->alias = 'articlelist'; } - /** - * Register the service provider. - * - * @return void - */ - public function register() + protected function setModulePath() { - } - - /** - * Register config. - * - * @return void - */ - protected function registerConfig() - { - $this->publishes([__DIR__ . '/../Config/config.php' => config_path('articlelist.php')]); - $this->mergeConfigFrom(__DIR__ . '/../Config/config.php', 'articlelist'); - } - - /** - * Register views. - * - * @return void - */ - public function registerViews() - { - $viewPath = base_path('resources/views/modules/articlelist'); - $sourcePath = __DIR__ . '/../Resources/views'; - - $this->publishes([$sourcePath => $viewPath]); - - $this->loadViewsFrom(array_merge(array_map(function ($path) { - return $path . '/modules/articlelist'; - }, \Config::get('view.paths')), [$sourcePath]), 'articlelist'); - } - - /** - * Register translations. - * - * @return void - */ - public function registerTranslations() - { - $langPath = base_path('resources/lang/modules/articlelist'); - - if (is_dir($langPath)) { - $this->loadTranslationsFrom($langPath, 'articlelist'); - } else { - $this->loadTranslationsFrom(__DIR__ . '/../Resources/lang', 'articlelist'); - } - } - - /** - * Get the services provided by the provider. - * - * @return array - */ - public function provides() - { - return array (); + $this->path = __DIR__; } } diff --git a/modules/Categories/Providers/CategoriesServiceProvider.php b/modules/Categories/Providers/CategoriesServiceProvider.php index 13d64b18..4f70a624 100644 --- a/modules/Categories/Providers/CategoriesServiceProvider.php +++ b/modules/Categories/Providers/CategoriesServiceProvider.php @@ -1,88 +1,16 @@ registerTranslations(); - $this->registerConfig(); - $this->registerViews(); + $this->alias = 'categories'; } - /** - * Register the service provider. - * - * @return void - */ - public function register() + protected function setModulePath() { - } - - /** - * Register config. - * - * @return void - */ - protected function registerConfig() - { - $this->publishes([__DIR__ . '/../Config/config.php' => config_path('categories.php')]); - $this->mergeConfigFrom(__DIR__ . '/../Config/config.php', 'categories'); - } - - /** - * Register views. - * - * @return void - */ - public function registerViews() - { - $viewPath = base_path('resources/views/modules/categories'); - $sourcePath = __DIR__ . '/../Resources/views'; - - $this->publishes([$sourcePath => $viewPath], 'module-views'); - - $this->loadViewsFrom(array_merge(array_map(function ($path) { - return $path . '/modules/categories'; - }, \Config::get('view.paths')), [$sourcePath]), 'categories'); - } - - /** - * Register translations. - * - * @return void - */ - public function registerTranslations() - { - $langPath = base_path('resources/lang/modules/categories'); - - if (is_dir($langPath)) { - $this->loadTranslationsFrom($langPath, 'categories'); - } else { - $this->loadTranslationsFrom(__DIR__ . '/../Resources/lang', 'categories'); - } - } - - /** - * Get the services provided by the provider. - * - * @return array - */ - public function provides() - { - return array (); + $this->path = __DIR__; } } diff --git a/modules/ContactForm/Providers/ContactFormServiceProvider.php b/modules/ContactForm/Providers/ContactFormServiceProvider.php index d6c77bc0..a0a64438 100644 --- a/modules/ContactForm/Providers/ContactFormServiceProvider.php +++ b/modules/ContactForm/Providers/ContactFormServiceProvider.php @@ -1,19 +1,12 @@ registerTranslations(); - $this->registerConfig(); - $this->registerViews(); + parent::boot(); $this->registerWidgets(); } @@ -38,54 +29,6 @@ class ContactFormServiceProvider extends ServiceProvider $registrator->register('contactForm', 'contactform::module.name', 'contactform::admin.settings.form'); } - /** - * Register config. - * - * @return void - */ - protected function registerConfig() - { - $this->publishes([__DIR__ . '/../Config/config.php' => config_path('contactform.php')]); - $this->mergeConfigFrom(__DIR__ . '/../Config/config.php', 'contactform'); - } - - /** - * Register views. - * - * @return void - */ - public function registerViews() - { - $viewPath = base_path('resources/views/modules/contactform'); - $sourcePath = __DIR__ . '/../Resources/views'; - - $this->publishes([$sourcePath => $viewPath], 'module-views'); - - $this->loadViewsFrom(array_merge(array_map(function ($path) { - return $path . '/modules/contactform'; - }, \Config::get('view.paths')), [$sourcePath]), 'contactform'); - } - - /** - * Register translations. - * - * @return void - */ - public function registerTranslations() - { - $this->loadTranslationsFrom(__DIR__ . '/../Resources/lang', 'contactform'); - } - - /** - * Get the services provided by the provider. - * - * @return array - */ - public function provides() - { - return array (); - } - private function registerWidgets() { $registrator = $this->app->make('registerWidget'); @@ -93,4 +36,14 @@ class ContactFormServiceProvider extends ServiceProvider $registrator->register(Address::class, null, ContactForm::getModuleId()); $registrator->register(Form::class, null, ContactForm::getModuleId()); } + + protected function setModuleAlias() + { + $this->alias = 'contactform'; + } + + protected function setModulePath() + { + $this->path = __DIR__; + } } diff --git a/modules/Gallery/Providers/GalleryServiceProvider.php b/modules/Gallery/Providers/GalleryServiceProvider.php index abe54e13..aac0bc39 100644 --- a/modules/Gallery/Providers/GalleryServiceProvider.php +++ b/modules/Gallery/Providers/GalleryServiceProvider.php @@ -1,92 +1,16 @@ registerTranslations(); - $this->registerConfig(); - $this->registerViews(); + $this->alias = 'gallery'; } - /** - * Register the service provider. - * - * @return void - */ - public function register() + protected function setModulePath() { - } - - /** - * Register config. - * - * @return void - */ - protected function registerConfig() - { - $this->publishes([ - __DIR__.'/../Config/config.php' => config_path('gallery.php'), - ]); - $this->mergeConfigFrom(__DIR__.'/../Config/config.php', 'gallery'); - } - - /** - * Register views. - * - * @return void - */ - public function registerViews() - { - $viewPath = base_path('resources/views/modules/gallery'); - $sourcePath = __DIR__.'/../Resources/views'; - - $this->publishes([$sourcePath => $viewPath], 'module-views'); - - $this->loadViewsFrom(array_merge(array_map(function ($path) { - return $path.'/modules/gallery'; - }, \Config::get('view.paths')), [$sourcePath]), 'gallery'); - } - - /** - * Register translations. - * - * @return void - */ - public function registerTranslations() - { - $langPath = base_path('resources/lang/modules/gallery'); - - if (is_dir($langPath)) { - $this->loadTranslationsFrom($langPath, 'gallery'); - } else { - $this->loadTranslationsFrom(__DIR__ . '/../Resources/lang', 'gallery'); - } - } - - /** - * Get the services provided by the provider. - * - * @return array - */ - public function provides() - { - return array (); + $this->path = __DIR__; } } diff --git a/modules/StaticPages/Providers/StaticPagesServiceProvider.php b/modules/StaticPages/Providers/StaticPagesServiceProvider.php index 5e38229e..ba92ce7f 100644 --- a/modules/StaticPages/Providers/StaticPagesServiceProvider.php +++ b/modules/StaticPages/Providers/StaticPagesServiceProvider.php @@ -1,73 +1,16 @@ registerTranslations(); - $this->registerConfig(); - $this->registerViews(); + $this->alias = 'staticpages'; } - /** - * Register the service provider. - * - * @return void - */ - public function register() + protected function setModulePath() { - } - - /** - * Register config. - * - * @return void - */ - protected function registerConfig() - { - $config = __DIR__ . DIRECTORY_SEPARATOR . '../Config/config.php'; - $this->publishes([$config => config_path('staticpages.php')]); - $this->mergeConfigFrom($config, 'staticpages'); - } - - /** - * Register views. - * - * @return void - */ - public function registerViews() - { - $sourcePath = __DIR__ . '/../Resources/views'; - $viewPath = base_path('resources/views/modules/staticpages'); - - $this->publishes([$sourcePath => $viewPath], 'module-views'); - - $this->loadViewsFrom(array_merge(array_map(function ($path) { - return $path . '/modules/staticpages'; - }, \Config::get('view.paths')), [$sourcePath]), 'staticpages'); - } - - /** - * Register translations. - * - * @return void - */ - public function registerTranslations() - { - $this->loadTranslationsFrom(__DIR__ . '/../Resources/lang', 'staticpages'); + $this->path = __DIR__; } }