Skip to content

Commit 79cdb18

Browse files
authored
Merge pull request #1996 from alissn/prohibiteCommands
Add `prohibitDestructiveCommands` Method to Prevent Running Destructive Commands in Production
2 parents e37afcf + dffd26f commit 79cdb18

File tree

6 files changed

+38
-7
lines changed

6 files changed

+38
-7
lines changed

src/Commands/Actions/ModuleDeleteCommand.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,9 @@ public function getConfirmableLabel(): string
2828
{
2929
return 'Warning: Do you want to remove the module?';
3030
}
31+
32+
public function getConfirmableCallback(): \Closure|bool|null
33+
{
34+
return true;
35+
}
3136
}

src/Commands/BaseCommand.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,12 @@ public function getInfo(): ?string
6161

6262
public function getConfirmableLabel(): ?string
6363
{
64-
return 'Warning';
64+
return 'Application In Production';
65+
}
66+
67+
public function getConfirmableCallback(): \Closure|bool|null
68+
{
69+
return null;
6570
}
6671

6772
/**
@@ -71,8 +76,8 @@ public function handle()
7176
{
7277
if ($this instanceof ConfirmableCommand) {
7378
if ($this->isProhibited() ||
74-
! $this->confirmToProceed($this->getConfirmableLabel(), fn () => true)) {
75-
return 1;
79+
! $this->confirmToProceed($this->getConfirmableLabel(), $this->getConfirmableCallback())) {
80+
return Command::FAILURE;
7681
}
7782
}
7883

src/Commands/Database/MigrateFreshCommand.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
use Illuminate\Database\Migrations\Migrator;
66
use Illuminate\Support\Collection;
77
use Nwidart\Modules\Commands\BaseCommand;
8+
use Nwidart\Modules\Contracts\ConfirmableCommand;
89
use Symfony\Component\Console\Input\InputOption;
910

10-
class MigrateFreshCommand extends BaseCommand
11+
class MigrateFreshCommand extends BaseCommand implements ConfirmableCommand
1112
{
1213
/**
1314
* The console command name.

src/Commands/Database/MigrateRefreshCommand.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
namespace Nwidart\Modules\Commands\Database;
44

55
use Nwidart\Modules\Commands\BaseCommand;
6+
use Nwidart\Modules\Contracts\ConfirmableCommand;
67
use Symfony\Component\Console\Input\InputOption;
78

8-
class MigrateRefreshCommand extends BaseCommand
9+
class MigrateRefreshCommand extends BaseCommand implements ConfirmableCommand
910
{
1011
/**
1112
* The console command name.

src/Commands/Database/MigrateResetCommand.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
namespace Nwidart\Modules\Commands\Database;
44

55
use Nwidart\Modules\Commands\BaseCommand;
6+
use Nwidart\Modules\Contracts\ConfirmableCommand;
67
use Nwidart\Modules\Migrations\Migrator;
78
use Nwidart\Modules\Traits\MigrationLoaderTrait;
89
use Symfony\Component\Console\Input\InputOption;
910

10-
class MigrateResetCommand extends BaseCommand
11+
class MigrateResetCommand extends BaseCommand implements ConfirmableCommand
1112
{
1213
use MigrationLoaderTrait;
1314

src/Facades/Module.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
namespace Nwidart\Modules\Facades;
44

55
use Illuminate\Support\Facades\Facade;
6+
use Nwidart\Modules\Commands\Database\MigrateFreshCommand;
7+
use Nwidart\Modules\Commands\Database\MigrateRefreshCommand;
8+
use Nwidart\Modules\Commands\Database\MigrateResetCommand;
69

710
/**
811
* @method static array all()
@@ -19,7 +22,7 @@
1922
* @method static \Nwidart\Modules\Module findOrFail(string $name)
2023
* @method static string getModulePath($moduleName)
2124
* @method static \Illuminate\Filesystem\Filesystem getFiles()
22-
* @method static mixed config(string $key, $default = NULL)
25+
* @method static mixed config(string $key, $default = null)
2326
* @method static string getPath()
2427
* @method static void boot()
2528
* @method static void register(): void
@@ -30,6 +33,21 @@
3033
*/
3134
class Module extends Facade
3235
{
36+
/**
37+
* Indicate if destructive Artisan commands should be prohibited.
38+
*
39+
* Prohibits: module:migrate-fresh, module:migrate-refresh, and module:migrate-reset
40+
*
41+
* @param bool $prohibit
42+
* @return void
43+
*/
44+
public static function prohibitDestructiveCommands(bool $prohibit = true): void
45+
{
46+
MigrateFreshCommand::prohibit($prohibit);
47+
MigrateRefreshCommand::prohibit($prohibit);
48+
MigrateResetCommand::prohibit($prohibit);
49+
}
50+
3351
protected static function getFacadeAccessor(): string
3452
{
3553
return 'modules';

0 commit comments

Comments
 (0)