Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[10.x] Add --with-secret option to Artisan down command. #49171

Merged
merged 7 commits into from
Nov 29, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions src/Illuminate/Foundation/Console/DownCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Console\Command;
use Illuminate\Foundation\Events\MaintenanceModeEnabled;
use Illuminate\Foundation\Exceptions\RegisterErrorViewPaths;
use Illuminate\Support\Str;
use Symfony\Component\Console\Attribute\AsCommand;
use Throwable;

Expand All @@ -23,6 +24,7 @@ class DownCommand extends Command
{--retry= : The number of seconds after which the request may be retried}
{--refresh= : The number of seconds after which the browser may refresh}
{--secret= : The secret phrase that may be used to bypass maintenance mode}
{--with-secret : Generate a random secret phrase that may be used to bypass maintenance mode}
{--status=503 : The status code that should be used when returning the maintenance mode response}';

/**
Expand All @@ -46,7 +48,9 @@ public function handle()
return 0;
}

$this->laravel->maintenanceMode()->activate($this->getDownFilePayload());
$downFilePayload = $this->getDownFilePayload();

$this->laravel->maintenanceMode()->activate($downFilePayload);

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

$this->components->info('Application is now in maintenance mode.');

if ($downFilePayload['secret'] !== null) {
$this->components->info("You may bypass maintenance mode via [".config('app.url')."/{$downFilePayload['secret']}].");
}
} catch (Exception $e) {
$this->components->error(sprintf(
'Failed to enter maintenance mode: %s.',
Expand All @@ -78,7 +86,7 @@ protected function getDownFilePayload()
'redirect' => $this->redirectPath(),
'retry' => $this->getRetryTime(),
'refresh' => $this->option('refresh'),
'secret' => $this->option('secret'),
'secret' => $this->getSecret(),
'status' => (int) $this->option('status', 503),
'template' => $this->option('render') ? $this->prerenderView() : null,
];
Expand Down Expand Up @@ -137,4 +145,18 @@ protected function getRetryTime()

return is_numeric($retry) && $retry > 0 ? (int) $retry : null;
}

/**
* Get the secret phrase that may be used to bypass maintenance mode.
*
* @return string|null
*/
protected function getSecret()
{
return match (true) {
! is_null($this->option('secret')) => $this->option('secret'),
$this->option('with-secret') => Str::random(),
default => null,
};
}
}