File tree 7 files changed +100
-4
lines changed
7 files changed +100
-4
lines changed Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace Illuminate \Console ;
4
+
5
+ use function Laravel \Prompts \confirm ;
6
+
7
+ trait Preventable
8
+ {
9
+ protected static $ preventFromRunning = false ;
10
+
11
+ /**
12
+ * Whether to prevent command from running.
13
+ *
14
+ * @param bool $prevent
15
+ * @return bool
16
+ */
17
+ public static function preventFromRunning ($ prevent = true )
18
+ {
19
+ static ::$ preventFromRunning = $ prevent ;
20
+ }
21
+
22
+ /**
23
+ * Determine if the command is prevented from
24
+ * running and display a warning if so.
25
+ *
26
+ * @return bool
27
+ */
28
+ protected function preventedFromRunning ()
29
+ {
30
+ if (! static ::$ preventFromRunning ) {
31
+ return false ;
32
+ }
33
+
34
+ $ this ->components ->warn ('Command has been prevented from running in this environment. ' );
35
+
36
+ return true ;
37
+ }
38
+ }
Original file line number Diff line number Diff line change 4
4
5
5
use Illuminate \Console \Command ;
6
6
use Illuminate \Console \ConfirmableTrait ;
7
+ use Illuminate \Console \Preventable ;
7
8
use Illuminate \Contracts \Events \Dispatcher ;
8
9
use Illuminate \Database \Events \DatabaseRefreshed ;
9
10
use Illuminate \Database \Migrations \Migrator ;
13
14
#[AsCommand(name: 'migrate:fresh ' )]
14
15
class FreshCommand extends Command
15
16
{
16
- use ConfirmableTrait;
17
+ use ConfirmableTrait, Preventable ;
17
18
18
19
/**
19
20
* The console command name.
@@ -56,6 +57,10 @@ public function __construct(Migrator $migrator)
56
57
*/
57
58
public function handle ()
58
59
{
60
+ if ($ this ->preventedFromRunning ()) {
61
+ return 1 ;
62
+ }
63
+
59
64
if (! $ this ->confirmToProceed ()) {
60
65
return 1 ;
61
66
}
Original file line number Diff line number Diff line change 4
4
5
5
use Illuminate \Console \Command ;
6
6
use Illuminate \Console \ConfirmableTrait ;
7
+ use Illuminate \Console \Preventable ;
7
8
use Illuminate \Contracts \Events \Dispatcher ;
8
9
use Illuminate \Database \Events \DatabaseRefreshed ;
9
10
use Symfony \Component \Console \Attribute \AsCommand ;
12
13
#[AsCommand(name: 'migrate:refresh ' )]
13
14
class RefreshCommand extends Command
14
15
{
15
- use ConfirmableTrait;
16
+ use ConfirmableTrait, Preventable ;
16
17
17
18
/**
18
19
* The console command name.
@@ -35,6 +36,10 @@ class RefreshCommand extends Command
35
36
*/
36
37
public function handle ()
37
38
{
39
+ if ($ this ->preventedFromRunning ()) {
40
+ return 1 ;
41
+ }
42
+
38
43
if (! $ this ->confirmToProceed ()) {
39
44
return 1 ;
40
45
}
Original file line number Diff line number Diff line change 3
3
namespace Illuminate \Database \Console \Migrations ;
4
4
5
5
use Illuminate \Console \ConfirmableTrait ;
6
+ use Illuminate \Console \Preventable ;
6
7
use Illuminate \Database \Migrations \Migrator ;
7
8
use Symfony \Component \Console \Attribute \AsCommand ;
8
9
use Symfony \Component \Console \Input \InputOption ;
9
10
10
11
#[AsCommand(name: 'migrate:reset ' )]
11
12
class ResetCommand extends BaseCommand
12
13
{
13
- use ConfirmableTrait;
14
+ use ConfirmableTrait, Preventable ;
14
15
15
16
/**
16
17
* The console command name.
@@ -53,6 +54,10 @@ public function __construct(Migrator $migrator)
53
54
*/
54
55
public function handle ()
55
56
{
57
+ if ($ this ->preventedFromRunning ()) {
58
+ return 1 ;
59
+ }
60
+
56
61
if (! $ this ->confirmToProceed ()) {
57
62
return 1 ;
58
63
}
Original file line number Diff line number Diff line change 4
4
5
5
use Illuminate \Console \Command ;
6
6
use Illuminate \Console \ConfirmableTrait ;
7
+ use Illuminate \Console \Preventable ;
7
8
use Symfony \Component \Console \Attribute \AsCommand ;
8
9
use Symfony \Component \Console \Input \InputOption ;
9
10
10
11
#[AsCommand(name: 'db:wipe ' )]
11
12
class WipeCommand extends Command
12
13
{
13
- use ConfirmableTrait;
14
+ use ConfirmableTrait, Preventable ;
14
15
15
16
/**
16
17
* The console command name.
@@ -33,6 +34,10 @@ class WipeCommand extends Command
33
34
*/
34
35
public function handle ()
35
36
{
37
+ if ($ this ->preventedFromRunning ()) {
38
+ return 1 ;
39
+ }
40
+
36
41
if (! $ this ->confirmToProceed ()) {
37
42
return 1 ;
38
43
}
Original file line number Diff line number Diff line change @@ -72,6 +72,27 @@ public function testRefreshCommandCallsCommandsWithStep()
72
72
$ this ->runCommand ($ command , ['--step ' => 2 ]);
73
73
}
74
74
75
+ public function testRefreshCommandExitsWhenPrevented ()
76
+ {
77
+ $ command = new RefreshCommand ;
78
+
79
+ $ app = new ApplicationDatabaseRefreshStub (['path.database ' => __DIR__ ]);
80
+ $ dispatcher = $ app ->instance (Dispatcher::class, $ events = m::mock ());
81
+ $ console = m::mock (ConsoleApplication::class)->makePartial ();
82
+ $ console ->__construct ();
83
+ $ command ->setLaravel ($ app );
84
+ $ command ->setApplication ($ console );
85
+
86
+ RefreshCommand::preventFromRunning ();
87
+
88
+ $ code = $ this ->runCommand ($ command );
89
+
90
+ $ this ->assertSame (1 , $ code );
91
+
92
+ $ console ->shouldNotHaveBeenCalled ();
93
+ $ dispatcher ->shouldNotReceive ('dispatch ' );
94
+ }
95
+
75
96
protected function runCommand ($ command , $ input = [])
76
97
{
77
98
return $ command ->run (new ArrayInput ($ input ), new NullOutput );
Original file line number Diff line number Diff line change @@ -52,6 +52,23 @@ public function testResetCommandCanBePretended()
52
52
$ this ->runCommand ($ command , ['--pretend ' => true , '--database ' => 'foo ' ]);
53
53
}
54
54
55
+ public function testRefreshCommandExitsWhenPrevented ()
56
+ {
57
+ $ command = new ResetCommand ($ migrator = m::mock (Migrator::class));
58
+
59
+ $ app = new ApplicationDatabaseResetStub (['path.database ' => __DIR__ ]);
60
+ $ app ->useDatabasePath (__DIR__ );
61
+ $ command ->setLaravel ($ app );
62
+
63
+ ResetCommand::preventFromRunning ();
64
+
65
+ $ code = $ this ->runCommand ($ command );
66
+
67
+ $ this ->assertSame (1 , $ code );
68
+
69
+ $ migrator ->shouldNotHaveBeenCalled ();
70
+ }
71
+
55
72
protected function runCommand ($ command , $ input = [])
56
73
{
57
74
return $ command ->run (new ArrayInput ($ input ), new NullOutput );
You can’t perform that action at this time.
0 commit comments