-
Notifications
You must be signed in to change notification settings - Fork 11.4k
[8.x] move maintenance mode logic #40059
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
use Illuminate\Console\Command; | ||
use Illuminate\Foundation\Events\MaintenanceModeEnabled; | ||
use Illuminate\Foundation\Exceptions\RegisterErrorViewPaths; | ||
use Illuminate\Maintenance\MaintenanceMode; | ||
use Throwable; | ||
|
||
class DownCommand extends Command | ||
|
@@ -35,19 +36,16 @@ class DownCommand extends Command | |
* | ||
* @return int | ||
*/ | ||
public function handle() | ||
public function handle(MaintenanceMode $maintenanceMode) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Another breaking change here. |
||
{ | ||
try { | ||
if (is_file(storage_path('framework/down'))) { | ||
if ($maintenanceMode->isDown()) { | ||
$this->comment('Application is already down.'); | ||
|
||
return 0; | ||
} | ||
|
||
file_put_contents( | ||
storage_path('framework/down'), | ||
json_encode($this->getDownFilePayload(), JSON_PRETTY_PRINT) | ||
); | ||
$maintenanceMode->down($this->getDownFilePayload()); | ||
|
||
file_put_contents( | ||
storage_path('framework/maintenance.php'), | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
use Exception; | ||
use Illuminate\Console\Command; | ||
use Illuminate\Foundation\Events\MaintenanceModeDisabled; | ||
use Illuminate\Maintenance\MaintenanceMode; | ||
|
||
class UpCommand extends Command | ||
{ | ||
|
@@ -27,16 +28,16 @@ class UpCommand extends Command | |
* | ||
* @return int | ||
*/ | ||
public function handle() | ||
public function handle(MaintenanceMode $maintenanceMode) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a breaking change. |
||
{ | ||
try { | ||
if (! is_file(storage_path('framework/down'))) { | ||
if ($maintenanceMode->isDown() === false) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe add an |
||
$this->comment('Application is already up.'); | ||
|
||
return 0; | ||
} | ||
|
||
unlink(storage_path('framework/down')); | ||
$maintenanceMode->up(); | ||
|
||
if (is_file(storage_path('framework/maintenance.php'))) { | ||
unlink(storage_path('framework/maintenance.php')); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
use Closure; | ||
use Illuminate\Contracts\Foundation\Application; | ||
use Illuminate\Foundation\Http\MaintenanceModeBypassCookie; | ||
use Illuminate\Maintenance\MaintenanceMode; | ||
use Symfony\Component\HttpKernel\Exception\HttpException; | ||
|
||
class PreventRequestsDuringMaintenance | ||
|
@@ -23,15 +24,21 @@ class PreventRequestsDuringMaintenance | |
*/ | ||
protected $except = []; | ||
|
||
/** | ||
* @var MaintenanceMode | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use FQN and a short description for DocBlocks |
||
*/ | ||
private $maintenanceMode; | ||
|
||
/** | ||
* Create a new middleware instance. | ||
* | ||
* @param \Illuminate\Contracts\Foundation\Application $app | ||
* @return void | ||
*/ | ||
public function __construct(Application $app) | ||
public function __construct(Application $app, MaintenanceMode $maintenanceMode) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Adding a new dependency here is a breaking change unfortunately. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @driesvints You're right. It would for example break implementation if people have extended the class. Should I point the update to the Laravel 9 branch instead? |
||
{ | ||
$this->app = $app; | ||
$this->maintenanceMode = $maintenanceMode; | ||
} | ||
|
||
/** | ||
|
@@ -45,8 +52,8 @@ public function __construct(Application $app) | |
*/ | ||
public function handle($request, Closure $next) | ||
{ | ||
if ($this->app->isDownForMaintenance()) { | ||
$data = json_decode(file_get_contents($this->app->storagePath().'/framework/down'), true); | ||
if ($this->maintenanceMode->isDown()) { | ||
$data = $this->maintenanceMode->getPayload(); | ||
|
||
if (isset($data['secret']) && $request->path() === $data['secret']) { | ||
return $this->bypassResponse($data['secret']); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
namespace Illuminate\Maintenance; | ||
|
||
class MaintenanceMode | ||
{ | ||
public function isDown(): bool | ||
{ | ||
return file_exists($this->getDownFilePath()); | ||
} | ||
|
||
public function down(array $payload): void | ||
{ | ||
file_put_contents( | ||
$this->getDownFilePath(), | ||
json_encode($payload, JSON_PRETTY_PRINT) | ||
); | ||
} | ||
|
||
public function up(): void | ||
{ | ||
if ($this->isDown() === false) { | ||
return; | ||
} | ||
|
||
unlink($this->getDownFilePath()); | ||
} | ||
|
||
public function getPayload(): array | ||
{ | ||
return json_decode(file_get_contents($this->getDownFilePath()), true); | ||
} | ||
|
||
private function getDownFilePath(): string | ||
{ | ||
return storage_path('framework/down'); | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.