REFACTORING: refactor module service providers to reduce amoount of duplicated code

This commit is contained in:
Michal Kváček
2016-10-12 17:38:06 +02:00
parent 9b6ae9f9f8
commit d425133cae
8 changed files with 143 additions and 452 deletions
+77
View File
@@ -0,0 +1,77 @@
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
abstract class ModuleServiceProvider extends ServiceProvider {
/**
* Indicates if loading of the provider is deferred.
*
* @var bool
*/
protected $defer = false;
protected $alias = false;
protected $path = false;
abstract protected function setModuleAlias();
abstract protected function setModulePath();
/**
* Boot the application events.
*
* @return void
*/
public function boot()
{
$this->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);
}
}
@@ -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__;
}
}
@@ -1,95 +1,28 @@
<?php namespace Modules\Blog\Article\Providers;
use App\Handlers\Registrators\TemplateRegistrator;
use Illuminate\Support\ServiceProvider;
use InvalidArgumentException;
use Modules\Blog\Article\Handlers\Article;
use Pingpong\Modules\Facades\Module;
use App\Providers\ModuleServiceProvider;
class ArticleServiceProvider extends ServiceProvider {
class ArticleServiceProvider extends ModuleServiceProvider
{
/**
* Boot the application events.
*
* @return void
*/
public function boot()
{
parent::boot();
/**
* Indicates if loading of the provider is deferred.
*
* @var bool
*/
protected $defer = false;
$this->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__;
}
}
@@ -1,88 +1,16 @@
<?php namespace Modules\Blog\ArticleList\Providers;
use Illuminate\Support\ServiceProvider;
use App\Providers\ModuleServiceProvider;
class ArticleListServiceProvider extends ServiceProvider
class ArticleListServiceProvider extends ModuleServiceProvider
{
/**
* Indicates if loading of the provider is deferred.
*
* @var bool
*/
protected $defer = false;
/**
* Boot the application events.
*
* @return void
*/
public function boot()
protected function setModuleAlias()
{
$this->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__;
}
}
@@ -1,88 +1,16 @@
<?php namespace Modules\Categories\Providers;
use Illuminate\Support\ServiceProvider;
use App\Providers\ModuleServiceProvider;
class CategoriesServiceProvider extends ServiceProvider
class CategoriesServiceProvider extends ModuleServiceProvider
{
/**
* Indicates if loading of the provider is deferred.
*
* @var bool
*/
protected $defer = false;
/**
* Boot the application events.
*
* @return void
*/
public function boot()
protected function setModuleAlias()
{
$this->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__;
}
}
@@ -1,19 +1,12 @@
<?php namespace Modules\ContactForm\Providers;
use Illuminate\Support\ServiceProvider;
use App\Providers\ModuleServiceProvider;
use Modules\ContactForm\Handlers\ContactForm;
use Modules\ContactForm\Widgets\Address\Address;
use Modules\ContactForm\Widgets\Form\Form;
class ContactFormServiceProvider extends ServiceProvider
class ContactFormServiceProvider extends ModuleServiceProvider
{
/**
* Indicates if loading of the provider is deferred.
*
* @var bool
*/
protected $defer = false;
/**
* Boot the application events.
*
@@ -21,9 +14,7 @@ class ContactFormServiceProvider extends ServiceProvider
*/
public function boot()
{
$this->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__;
}
}
@@ -1,92 +1,16 @@
<?php namespace Modules\Gallery\Providers;
use App\Handlers\Registrators\TemplateRegistrator;
use Illuminate\Support\ServiceProvider;
use Symfony\Component\Process\Exception\InvalidArgumentException;
use App\Providers\ModuleServiceProvider;
class GalleryServiceProvider extends ServiceProvider
class GalleryServiceProvider extends ModuleServiceProvider
{
/**
* Indicates if loading of the provider is deferred.
*
* @var bool
*/
protected $defer = false;
/**
* Boot the application events.
*
* @return void
*/
public function boot()
protected function setModuleAlias()
{
$this->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__;
}
}
@@ -1,73 +1,16 @@
<?php namespace Modules\Staticpages\Providers;
use Illuminate\Support\ServiceProvider;
use App\Providers\ModuleServiceProvider;
class StaticPagesServiceProvider extends ServiceProvider
class StaticPagesServiceProvider extends ModuleServiceProvider
{
/**
* Indicates if loading of the provider is deferred.
*
* @var bool
*/
protected $defer = false;
/**
* Boot the application events.
*
* @return void
*/
public function boot()
protected function setModuleAlias()
{
$this->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__;
}
}