Skip to content

Commit

Permalink
Merge pull request #1996 from alissn/prohibiteCommands
Browse files Browse the repository at this point in the history
Add `prohibitDestructiveCommands` Method to Prevent Running Destructive Commands in Production
  • Loading branch information
dcblogdev authored Dec 3, 2024
2 parents e37afcf + dffd26f commit 79cdb18
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 7 deletions.
5 changes: 5 additions & 0 deletions src/Commands/Actions/ModuleDeleteCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,9 @@ public function getConfirmableLabel(): string
{
return 'Warning: Do you want to remove the module?';
}

public function getConfirmableCallback(): \Closure|bool|null
{
return true;
}
}
11 changes: 8 additions & 3 deletions src/Commands/BaseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,12 @@ public function getInfo(): ?string

public function getConfirmableLabel(): ?string
{
return 'Warning';
return 'Application In Production';
}

public function getConfirmableCallback(): \Closure|bool|null
{
return null;
}

/**
Expand All @@ -71,8 +76,8 @@ public function handle()
{
if ($this instanceof ConfirmableCommand) {
if ($this->isProhibited() ||
! $this->confirmToProceed($this->getConfirmableLabel(), fn () => true)) {
return 1;
! $this->confirmToProceed($this->getConfirmableLabel(), $this->getConfirmableCallback())) {
return Command::FAILURE;
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/Commands/Database/MigrateFreshCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
use Illuminate\Database\Migrations\Migrator;
use Illuminate\Support\Collection;
use Nwidart\Modules\Commands\BaseCommand;
use Nwidart\Modules\Contracts\ConfirmableCommand;
use Symfony\Component\Console\Input\InputOption;

class MigrateFreshCommand extends BaseCommand
class MigrateFreshCommand extends BaseCommand implements ConfirmableCommand
{
/**
* The console command name.
Expand Down
3 changes: 2 additions & 1 deletion src/Commands/Database/MigrateRefreshCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
namespace Nwidart\Modules\Commands\Database;

use Nwidart\Modules\Commands\BaseCommand;
use Nwidart\Modules\Contracts\ConfirmableCommand;
use Symfony\Component\Console\Input\InputOption;

class MigrateRefreshCommand extends BaseCommand
class MigrateRefreshCommand extends BaseCommand implements ConfirmableCommand
{
/**
* The console command name.
Expand Down
3 changes: 2 additions & 1 deletion src/Commands/Database/MigrateResetCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
namespace Nwidart\Modules\Commands\Database;

use Nwidart\Modules\Commands\BaseCommand;
use Nwidart\Modules\Contracts\ConfirmableCommand;
use Nwidart\Modules\Migrations\Migrator;
use Nwidart\Modules\Traits\MigrationLoaderTrait;
use Symfony\Component\Console\Input\InputOption;

class MigrateResetCommand extends BaseCommand
class MigrateResetCommand extends BaseCommand implements ConfirmableCommand
{
use MigrationLoaderTrait;

Expand Down
20 changes: 19 additions & 1 deletion src/Facades/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
namespace Nwidart\Modules\Facades;

use Illuminate\Support\Facades\Facade;
use Nwidart\Modules\Commands\Database\MigrateFreshCommand;
use Nwidart\Modules\Commands\Database\MigrateRefreshCommand;
use Nwidart\Modules\Commands\Database\MigrateResetCommand;

/**
* @method static array all()
Expand All @@ -19,7 +22,7 @@
* @method static \Nwidart\Modules\Module findOrFail(string $name)
* @method static string getModulePath($moduleName)
* @method static \Illuminate\Filesystem\Filesystem getFiles()
* @method static mixed config(string $key, $default = NULL)
* @method static mixed config(string $key, $default = null)
* @method static string getPath()
* @method static void boot()
* @method static void register(): void
Expand All @@ -30,6 +33,21 @@
*/
class Module extends Facade
{
/**
* Indicate if destructive Artisan commands should be prohibited.
*
* Prohibits: module:migrate-fresh, module:migrate-refresh, and module:migrate-reset
*
* @param bool $prohibit
* @return void
*/
public static function prohibitDestructiveCommands(bool $prohibit = true): void
{
MigrateFreshCommand::prohibit($prohibit);
MigrateRefreshCommand::prohibit($prohibit);
MigrateResetCommand::prohibit($prohibit);
}

protected static function getFacadeAccessor(): string
{
return 'modules';
Expand Down

0 comments on commit 79cdb18

Please sign in to comment.