Skip to content

Commit 3d2c03d

Browse files
[10.x] Add --generate-secret option to Artisan down command. (#49171)
* Add option to Artisan `down` command to generate a secret phrase. * Revise sentence. * Use `Str::random()` helper instead. * Rename `getSecretPhrase()` to `getSecret()`. * Fix spelling. * Move import statement. * formatting --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent d80af24 commit 3d2c03d

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

src/Illuminate/Foundation/Console/DownCommand.php

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Illuminate\Console\Command;
88
use Illuminate\Foundation\Events\MaintenanceModeEnabled;
99
use Illuminate\Foundation\Exceptions\RegisterErrorViewPaths;
10+
use Illuminate\Support\Str;
1011
use Symfony\Component\Console\Attribute\AsCommand;
1112
use Throwable;
1213

@@ -23,6 +24,7 @@ class DownCommand extends Command
2324
{--retry= : The number of seconds after which the request may be retried}
2425
{--refresh= : The number of seconds after which the browser may refresh}
2526
{--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}
2628
{--status=503 : The status code that should be used when returning the maintenance mode response}';
2729

2830
/**
@@ -46,7 +48,9 @@ public function handle()
4648
return 0;
4749
}
4850

49-
$this->laravel->maintenanceMode()->activate($this->getDownFilePayload());
51+
$downFilePayload = $this->getDownFilePayload();
52+
53+
$this->laravel->maintenanceMode()->activate($downFilePayload);
5054

5155
file_put_contents(
5256
storage_path('framework/maintenance.php'),
@@ -56,6 +60,10 @@ public function handle()
5660
$this->laravel->get('events')->dispatch(new MaintenanceModeEnabled());
5761

5862
$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+
}
5967
} catch (Exception $e) {
6068
$this->components->error(sprintf(
6169
'Failed to enter maintenance mode: %s.',
@@ -78,7 +86,7 @@ protected function getDownFilePayload()
7886
'redirect' => $this->redirectPath(),
7987
'retry' => $this->getRetryTime(),
8088
'refresh' => $this->option('refresh'),
81-
'secret' => $this->option('secret'),
89+
'secret' => $this->getSecret(),
8290
'status' => (int) $this->option('status', 503),
8391
'template' => $this->option('render') ? $this->prerenderView() : null,
8492
];
@@ -137,4 +145,18 @@ protected function getRetryTime()
137145

138146
return is_numeric($retry) && $retry > 0 ? (int) $retry : null;
139147
}
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+
}
140162
}

0 commit comments

Comments
 (0)