From c41fed8d1e9c3ec987b610b29f2fcb3950f6281d Mon Sep 17 00:00:00 2001 From: Anton Komarev <1849174+antonkomarev@users.noreply.github.com> Date: Thu, 26 Sep 2019 03:13:27 +0300 Subject: [PATCH] Add ability to ignore default migrations (#50) * Add ability to ignore default migrations --- CHANGELOG.md | 11 ++++++- config/ban.php | 28 ++++++++++++++++++ src/Providers/BanServiceProvider.php | 44 ++++++++++++++++++++++++++-- 3 files changed, 79 insertions(+), 4 deletions(-) create mode 100644 config/ban.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 0b12185..d1e70b0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,13 @@ All notable changes to `cybercog/laravel-ban` will be documented in this file. ## [Unreleased] +## [4.2.0] - 2019-09-26 + +### Added + +- ([#50]) Added publishable configuration file +- ([#50]) Added ability to ignore default migrations + ## [4.1.0] - 2019-09-04 ### Added @@ -118,7 +125,8 @@ All notable changes to `cybercog/laravel-ban` will be documented in this file. - Initial release -[Unreleased]: https://github.com/cybercog/laravel-ban/compare/4.1.0...master +[Unreleased]: https://github.com/cybercog/laravel-ban/compare/4.2.0...master +[4.2.0]: https://github.com/cybercog/laravel-ban/compare/4.1.0...4.2.0 [4.1.0]: https://github.com/cybercog/laravel-ban/compare/4.0.0...4.1.0 [4.0.0]: https://github.com/cybercog/laravel-ban/compare/3.5.0...4.0.0 [3.5.0]: https://github.com/cybercog/laravel-ban/compare/3.4.0...3.5.0 @@ -131,6 +139,7 @@ All notable changes to `cybercog/laravel-ban` will be documented in this file. [2.0.1]: https://github.com/cybercog/laravel-ban/compare/2.0.0...2.0.1 [2.0.0]: https://github.com/cybercog/laravel-ban/compare/1.0.0...2.0.0 +[#50]: https://github.com/cybercog/laravel-ban/pull/50 [#48]: https://github.com/cybercog/laravel-ban/pull/48 [#35]: https://github.com/cybercog/laravel-ban/pull/35 [#30]: https://github.com/cybercog/laravel-ban/pull/30 diff --git a/config/ban.php b/config/ban.php new file mode 100644 index 0000000..d83121b --- /dev/null +++ b/config/ban.php @@ -0,0 +1,28 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +return [ + + /* + |-------------------------------------------------------------------------- + | Ban Database Migrations + |-------------------------------------------------------------------------- + | + | Determine if default package migrations should be registered. + | Set value to `false` when using customized migrations. + | + */ + + 'load_default_migrations' => true, + +]; diff --git a/src/Providers/BanServiceProvider.php b/src/Providers/BanServiceProvider.php index 76b409f..a480999 100644 --- a/src/Providers/BanServiceProvider.php +++ b/src/Providers/BanServiceProvider.php @@ -19,6 +19,7 @@ use Cog\Laravel\Ban\Models\Ban; use Cog\Laravel\Ban\Observers\BanObserver; use Cog\Laravel\Ban\Services\BanService; +use Illuminate\Support\Facades\Config; use Illuminate\Support\ServiceProvider; class BanServiceProvider extends ServiceProvider @@ -43,6 +44,7 @@ public function register(): void */ public function boot(): void { + $this->configure(); $this->registerPublishes(); $this->registerObservers(); } @@ -94,13 +96,49 @@ protected function registerObservers(): void protected function registerPublishes(): void { if ($this->app->runningInConsole()) { - $migrationsPath = __DIR__ . '/../../database/migrations'; + $this->publishes([ + __DIR__ . '/../../config/ban.php' => config_path('ban.php'), + ], 'ban-config'); $this->publishes([ - $migrationsPath => database_path('migrations'), + __DIR__ . '/../../database/migrations' => database_path('migrations'), ], 'migrations'); + } + + $this->registerMigrations(); + } - $this->loadMigrationsFrom($migrationsPath); + /** + * Register the Ban migrations. + * + * @return void + */ + private function registerMigrations(): void + { + if ($this->app->runningInConsole() && $this->shouldLoadDefaultMigrations()) { + $this->loadMigrationsFrom(__DIR__ . '/../../database/migrations'); } } + + /** + * Merge Ban configuration with the application configuration. + * + * @return void + */ + private function configure(): void + { + if (!$this->app->configurationIsCached()) { + $this->mergeConfigFrom(__DIR__ . '/../../config/ban.php', 'ban'); + } + } + + /** + * Determine if we should register default migrations. + * + * @return bool + */ + private function shouldLoadDefaultMigrations(): bool + { + return Config::get('ban.load_default_migrations', true); + } }