From fa6b10987fbb52cc637698971352b9b859c1c13a Mon Sep 17 00:00:00 2001 From: devansh bawari Date: Wed, 8 Sep 2021 14:00:04 +0530 Subject: [PATCH 1/2] New Individual Command Added --- .../src/Listeners/ChannelSettingsChange.php | 10 ++-- .../Console/Commands/DownChannelCommand.php | 29 +++++++++ .../Core/src/Console/Commands/DownCommand.php | 7 ++- .../src/Console/Commands/UpChannelCommand.php | 21 +++++++ .../Core/src/Console/Commands/UpCommand.php | 7 ++- .../Middleware/CheckForMaintenanceMode.php | 60 ++++++++++--------- .../src/Providers/CoreServiceProvider.php | 43 +++++++------ 7 files changed, 121 insertions(+), 56 deletions(-) create mode 100644 packages/Webkul/Core/src/Console/Commands/DownChannelCommand.php create mode 100644 packages/Webkul/Core/src/Console/Commands/UpChannelCommand.php diff --git a/packages/Webkul/Admin/src/Listeners/ChannelSettingsChange.php b/packages/Webkul/Admin/src/Listeners/ChannelSettingsChange.php index e5ddc81b5..032d50d0c 100644 --- a/packages/Webkul/Admin/src/Listeners/ChannelSettingsChange.php +++ b/packages/Webkul/Admin/src/Listeners/ChannelSettingsChange.php @@ -14,10 +14,12 @@ class ChannelSettingsChange */ public function checkForMaintenaceMode($channel) { - if ((bool) $channel->is_maintenance_on) { - Artisan::call('down'); + $channels = core()->getAllChannels(); + + if ($channels->contains('is_maintenance_on', 1)) { + Artisan::call('channel:down'); } else { - Artisan::call('up'); + Artisan::call('channel:up'); } } -} \ No newline at end of file +} diff --git a/packages/Webkul/Core/src/Console/Commands/DownChannelCommand.php b/packages/Webkul/Core/src/Console/Commands/DownChannelCommand.php new file mode 100644 index 000000000..7c904d764 --- /dev/null +++ b/packages/Webkul/Core/src/Console/Commands/DownChannelCommand.php @@ -0,0 +1,29 @@ +downAllChannel(); - + $this->downAllChannels(); + parent::handle(); } - protected function downAllChannel() + protected function downAllChannels() { $this->comment('All channels are down.'); + return Channel::query()->update(['is_maintenance_on' => 1]); } } diff --git a/packages/Webkul/Core/src/Console/Commands/UpChannelCommand.php b/packages/Webkul/Core/src/Console/Commands/UpChannelCommand.php new file mode 100644 index 000000000..8036576fe --- /dev/null +++ b/packages/Webkul/Core/src/Console/Commands/UpChannelCommand.php @@ -0,0 +1,21 @@ +upAllChannel(); - + $this->upAllChannels(); + parent::handle(); } - protected function upAllChannel() + protected function upAllChannels() { $this->comment('Activating all channels.'); + return Channel::query()->update(['is_maintenance_on' => 0]); } } diff --git a/packages/Webkul/Core/src/Http/Middleware/CheckForMaintenanceMode.php b/packages/Webkul/Core/src/Http/Middleware/CheckForMaintenanceMode.php index 76b3bb9c7..c0915470f 100644 --- a/packages/Webkul/Core/src/Http/Middleware/CheckForMaintenanceMode.php +++ b/packages/Webkul/Core/src/Http/Middleware/CheckForMaintenanceMode.php @@ -3,10 +3,10 @@ namespace Webkul\Core\Http\Middleware; use Closure; -use Illuminate\Routing\Route; use Illuminate\Contracts\Foundation\Application; -use Symfony\Component\HttpKernel\Exception\HttpException; use Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode as Original; +use Illuminate\Routing\Route; +use Symfony\Component\HttpKernel\Exception\HttpException; class CheckForMaintenanceMode extends Original { @@ -49,6 +49,9 @@ class CheckForMaintenanceMode extends Original /* application */ $this->app = $app; + /* current channel */ + $this->channel = core()->getCurrentChannel(); + /* adding exception for admin routes */ $this->except[] = env('APP_ADMIN_URL', 'admin') . '*'; @@ -56,27 +59,6 @@ class CheckForMaintenanceMode extends Original $this->setAllowedIps(); } - /** - * Check for the except routes. - * - * @param \Illuminate\Http\Request $request - * @return boolean - */ - protected function shouldPassThrough($request) - { - foreach ($this->except as $except) { - if ($except !== '/') { - $except = trim($except, '/'); - } - - if ($request->is($except)) { - return true; - } - } - - return false; - } - /** * Handle an incoming request. * @@ -103,8 +85,11 @@ class CheckForMaintenanceMode extends Original } } - if ($this->shouldPassThrough($request)) - { + if ($this->shouldPassThrough($request)) { + return $response; + } + + if (! (bool) $this->channel->is_maintenance_on) { return $response; } @@ -121,10 +106,29 @@ class CheckForMaintenanceMode extends Original */ protected function setAllowedIps() { - $this->channel = core()->getCurrentChannel(); - if ($this->channel) { $this->excludedIPs = array_map('trim', explode(',', $this->channel->allowed_ips)); } } -} \ No newline at end of file + + /** + * Check for the except routes. + * + * @param \Illuminate\Http\Request $request + * @return boolean + */ + protected function shouldPassThrough($request) + { + foreach ($this->except as $except) { + if ($except !== '/') { + $except = trim($except, '/'); + } + + if ($request->is($except)) { + return true; + } + } + + return false; + } +} diff --git a/packages/Webkul/Core/src/Providers/CoreServiceProvider.php b/packages/Webkul/Core/src/Providers/CoreServiceProvider.php index 43ade8d49..4e5892ee7 100755 --- a/packages/Webkul/Core/src/Providers/CoreServiceProvider.php +++ b/packages/Webkul/Core/src/Providers/CoreServiceProvider.php @@ -2,25 +2,27 @@ namespace Webkul\Core\Providers; +use Illuminate\Contracts\Debug\ExceptionHandler; +use Illuminate\Database\Eloquent\Factory as EloquentFactory; +use Illuminate\Foundation\AliasLoader; +use Illuminate\Support\Facades\Event; +use Illuminate\Support\Facades\Validator; +use Illuminate\Support\ServiceProvider; +use Webkul\Core\Console\Commands\BagistoVersion; +use Webkul\Core\Console\Commands\BookingCron; +use Webkul\Core\Console\Commands\DownChannelCommand; +use Webkul\Core\Console\Commands\DownCommand; +use Webkul\Core\Console\Commands\ExchangeRateUpdate; +use Webkul\Core\Console\Commands\Install; +use Webkul\Core\Console\Commands\UpChannelCommand; +use Webkul\Core\Console\Commands\UpCommand; use Webkul\Core\Core; use Webkul\Core\Exceptions\Handler; -use Webkul\Core\Models\SliderProxy; -use Illuminate\Support\Facades\Event; -use Illuminate\Foundation\AliasLoader; -use Illuminate\Support\ServiceProvider; -use Webkul\Theme\ViewRenderEventManager; -use Illuminate\Support\Facades\Validator; -use Webkul\Core\Console\Commands\Install; -use Webkul\Core\Observers\SliderObserver; -use Webkul\Core\Console\Commands\UpCommand; use Webkul\Core\Facades\Core as CoreFacade; -use Webkul\Core\Console\Commands\BookingCron; -use Webkul\Core\Console\Commands\DownCommand; +use Webkul\Core\Models\SliderProxy; +use Webkul\Core\Observers\SliderObserver; use Webkul\Core\View\Compilers\BladeCompiler; -use Illuminate\Contracts\Debug\ExceptionHandler; -use Webkul\Core\Console\Commands\BagistoVersion; -use Webkul\Core\Console\Commands\ExchangeRateUpdate; -use Illuminate\Database\Eloquent\Factory as EloquentFactory; +use Webkul\Theme\ViewRenderEventManager; class CoreServiceProvider extends ServiceProvider { @@ -60,11 +62,11 @@ class CoreServiceProvider extends ServiceProvider $this->loadViewsFrom(__DIR__ . '/../Resources/views', 'core'); - Event::listen('bagisto.shop.layout.body.after', static function(ViewRenderEventManager $viewRenderEventManager) { + Event::listen('bagisto.shop.layout.body.after', static function (ViewRenderEventManager $viewRenderEventManager) { $viewRenderEventManager->addTemplate('core::blade.tracer.style'); }); - Event::listen('bagisto.admin.layout.head', static function(ViewRenderEventManager $viewRenderEventManager) { + Event::listen('bagisto.admin.layout.head', static function (ViewRenderEventManager $viewRenderEventManager) { $viewRenderEventManager->addTemplate('core::blade.tracer.style'); }); @@ -118,9 +120,14 @@ class CoreServiceProvider extends ServiceProvider BagistoVersion::class, Install::class, ExchangeRateUpdate::class, - BookingCron::class + BookingCron::class, ]); } + + $this->commands([ + DownChannelCommand::class, + UpChannelCommand::class, + ]); } /** From ad08dc930b291645b498e1ac30ccd15b15e403b4 Mon Sep 17 00:00:00 2001 From: devansh bawari Date: Wed, 8 Sep 2021 14:17:49 +0530 Subject: [PATCH 2/2] Comments Updated --- .../Console/Commands/DownChannelCommand.php | 7 +++++ .../Core/src/Console/Commands/DownCommand.php | 10 +++++++ .../src/Console/Commands/UpChannelCommand.php | 13 ++++++++- .../Core/src/Console/Commands/UpCommand.php | 10 +++++++ .../src/Providers/CoreServiceProvider.php | 27 +++++++------------ 5 files changed, 48 insertions(+), 19 deletions(-) diff --git a/packages/Webkul/Core/src/Console/Commands/DownChannelCommand.php b/packages/Webkul/Core/src/Console/Commands/DownChannelCommand.php index 7c904d764..8b7a0dcd4 100644 --- a/packages/Webkul/Core/src/Console/Commands/DownChannelCommand.php +++ b/packages/Webkul/Core/src/Console/Commands/DownChannelCommand.php @@ -17,6 +17,13 @@ class DownChannelCommand extends OriginalCommand {--secret= : The secret phrase that may be used to bypass maintenance mode} {--status=503 : The status code that should be used when returning the maintenance mode response}'; + /** + * The console command description. + * + * @var string + */ + protected $description = 'Down channel command. Same as parent but database will not update.'; + /** * Execute the console command. * diff --git a/packages/Webkul/Core/src/Console/Commands/DownCommand.php b/packages/Webkul/Core/src/Console/Commands/DownCommand.php index b81317916..1568644cb 100644 --- a/packages/Webkul/Core/src/Console/Commands/DownCommand.php +++ b/packages/Webkul/Core/src/Console/Commands/DownCommand.php @@ -7,6 +7,11 @@ use Webkul\Core\Models\Channel; class DownCommand extends OriginalCommand { + /** + * Execute the console command. + * + * @return int + */ public function handle() { $this->downAllChannels(); @@ -14,6 +19,11 @@ class DownCommand extends OriginalCommand parent::handle(); } + /** + * Update all channels. + * + * @return mixed + */ protected function downAllChannels() { $this->comment('All channels are down.'); diff --git a/packages/Webkul/Core/src/Console/Commands/UpChannelCommand.php b/packages/Webkul/Core/src/Console/Commands/UpChannelCommand.php index 8036576fe..6bfb52691 100644 --- a/packages/Webkul/Core/src/Console/Commands/UpChannelCommand.php +++ b/packages/Webkul/Core/src/Console/Commands/UpChannelCommand.php @@ -3,7 +3,6 @@ namespace Webkul\Core\Console\Commands; use Illuminate\Foundation\Console\UpCommand as OriginalCommand; -use Webkul\Core\Models\Channel; class UpChannelCommand extends OriginalCommand { @@ -14,6 +13,18 @@ class UpChannelCommand extends OriginalCommand */ protected $name = 'channel:up'; + /** + * The console command description. + * + * @var string + */ + protected $description = 'Up channel command. Same as parent but database will not update.'; + + /** + * Execute the console command. + * + * @return int + */ public function handle() { parent::handle(); diff --git a/packages/Webkul/Core/src/Console/Commands/UpCommand.php b/packages/Webkul/Core/src/Console/Commands/UpCommand.php index b752c7d5e..d09850769 100644 --- a/packages/Webkul/Core/src/Console/Commands/UpCommand.php +++ b/packages/Webkul/Core/src/Console/Commands/UpCommand.php @@ -7,6 +7,11 @@ use Webkul\Core\Models\Channel; class UpCommand extends OriginalCommand { + /** + * Execute the console command. + * + * @return int + */ public function handle() { $this->upAllChannels(); @@ -14,6 +19,11 @@ class UpCommand extends OriginalCommand parent::handle(); } + /** + * Update all channels. + * + * @return mixed + */ protected function upAllChannels() { $this->comment('Activating all channels.'); diff --git a/packages/Webkul/Core/src/Providers/CoreServiceProvider.php b/packages/Webkul/Core/src/Providers/CoreServiceProvider.php index 4e5892ee7..d47c9bf53 100755 --- a/packages/Webkul/Core/src/Providers/CoreServiceProvider.php +++ b/packages/Webkul/Core/src/Providers/CoreServiceProvider.php @@ -8,14 +8,6 @@ use Illuminate\Foundation\AliasLoader; use Illuminate\Support\Facades\Event; use Illuminate\Support\Facades\Validator; use Illuminate\Support\ServiceProvider; -use Webkul\Core\Console\Commands\BagistoVersion; -use Webkul\Core\Console\Commands\BookingCron; -use Webkul\Core\Console\Commands\DownChannelCommand; -use Webkul\Core\Console\Commands\DownCommand; -use Webkul\Core\Console\Commands\ExchangeRateUpdate; -use Webkul\Core\Console\Commands\Install; -use Webkul\Core\Console\Commands\UpChannelCommand; -use Webkul\Core\Console\Commands\UpCommand; use Webkul\Core\Core; use Webkul\Core\Exceptions\Handler; use Webkul\Core\Facades\Core as CoreFacade; @@ -71,11 +63,11 @@ class CoreServiceProvider extends ServiceProvider }); $this->app->extend('command.down', function () { - return new DownCommand; + return new \Webkul\Core\Console\Commands\DownCommand; }); $this->app->extend('command.up', function () { - return new UpCommand; + return new \Webkul\Core\Console\Commands\UpCommand; }); } @@ -109,7 +101,7 @@ class CoreServiceProvider extends ServiceProvider } /** - * Register the console commands of this package + * Register the console commands of this package. * * @return void */ @@ -117,16 +109,16 @@ class CoreServiceProvider extends ServiceProvider { if ($this->app->runningInConsole()) { $this->commands([ - BagistoVersion::class, - Install::class, - ExchangeRateUpdate::class, - BookingCron::class, + \Webkul\Core\Console\Commands\BagistoVersion::class, + \Webkul\Core\Console\Commands\Install::class, + \Webkul\Core\Console\Commands\ExchangeRateUpdate::class, + \Webkul\Core\Console\Commands\BookingCron::class, ]); } $this->commands([ - DownChannelCommand::class, - UpChannelCommand::class, + \Webkul\Core\Console\Commands\DownChannelCommand::class, + \Webkul\Core\Console\Commands\UpChannelCommand::class, ]); } @@ -134,7 +126,6 @@ class CoreServiceProvider extends ServiceProvider * Register factories. * * @param string $path - * * @return void * @throws \Illuminate\Contracts\Container\BindingResolutionException */