7
7
use Illuminate \Console \Command ;
8
8
use Illuminate \Foundation \Events \MaintenanceModeEnabled ;
9
9
use Illuminate \Foundation \Exceptions \RegisterErrorViewPaths ;
10
+ use Illuminate \Support \Str ;
10
11
use Symfony \Component \Console \Attribute \AsCommand ;
11
12
use Throwable ;
12
13
@@ -23,6 +24,7 @@ class DownCommand extends Command
23
24
{--retry= : The number of seconds after which the request may be retried}
24
25
{--refresh= : The number of seconds after which the browser may refresh}
25
26
{--secret= : The secret phrase that may be used to bypass maintenance mode}
27
+ {--with-secret : Generate a random secret phrase that may be used to bypass maintenance mode}
26
28
{--status=503 : The status code that should be used when returning the maintenance mode response} ' ;
27
29
28
30
/**
@@ -46,7 +48,9 @@ public function handle()
46
48
return 0 ;
47
49
}
48
50
49
- $ this ->laravel ->maintenanceMode ()->activate ($ this ->getDownFilePayload ());
51
+ $ downFilePayload = $ this ->getDownFilePayload ();
52
+
53
+ $ this ->laravel ->maintenanceMode ()->activate ($ downFilePayload );
50
54
51
55
file_put_contents (
52
56
storage_path ('framework/maintenance.php ' ),
@@ -56,6 +60,10 @@ public function handle()
56
60
$ this ->laravel ->get ('events ' )->dispatch (new MaintenanceModeEnabled ());
57
61
58
62
$ this ->components ->info ('Application is now in maintenance mode. ' );
63
+
64
+ if ($ downFilePayload ['secret ' ] !== null ) {
65
+ $ this ->components ->info ("You may bypass maintenance mode via [ " .config ('app.url ' )."/ {$ downFilePayload ['secret ' ]}]. " );
66
+ }
59
67
} catch (Exception $ e ) {
60
68
$ this ->components ->error (sprintf (
61
69
'Failed to enter maintenance mode: %s. ' ,
@@ -78,7 +86,7 @@ protected function getDownFilePayload()
78
86
'redirect ' => $ this ->redirectPath (),
79
87
'retry ' => $ this ->getRetryTime (),
80
88
'refresh ' => $ this ->option ('refresh ' ),
81
- 'secret ' => $ this ->option ( ' secret ' ),
89
+ 'secret ' => $ this ->getSecret ( ),
82
90
'status ' => (int ) $ this ->option ('status ' , 503 ),
83
91
'template ' => $ this ->option ('render ' ) ? $ this ->prerenderView () : null ,
84
92
];
@@ -137,4 +145,18 @@ protected function getRetryTime()
137
145
138
146
return is_numeric ($ retry ) && $ retry > 0 ? (int ) $ retry : null ;
139
147
}
148
+
149
+ /**
150
+ * Get the secret phrase that may be used to bypass maintenance mode.
151
+ *
152
+ * @return string|null
153
+ */
154
+ protected function getSecret ()
155
+ {
156
+ return match (true ) {
157
+ ! is_null ($ this ->option ('secret ' )) => $ this ->option ('secret ' ),
158
+ $ this ->option ('with-secret ' ) => Str::random (),
159
+ default => null ,
160
+ };
161
+ }
140
162
}
0 commit comments