Skip to content

[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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion src/Illuminate/Foundation/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Illuminate\Foundation\Events\LocaleUpdated;
use Illuminate\Http\Request;
use Illuminate\Log\LogServiceProvider;
use Illuminate\Maintenance\MaintenanceMode;
use Illuminate\Routing\RoutingServiceProvider;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
Expand Down Expand Up @@ -1109,7 +1110,7 @@ public function addAbsoluteCachePathPrefix($prefix)
*/
public function isDownForMaintenance()
{
return file_exists($this->storagePath().'/framework/down');
return app(MaintenanceMode::class)->isDown();
Copy link
Member

@driesvints driesvints Dec 16, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return app(MaintenanceMode::class)->isDown();
return $this->make(MaintenanceMode::class)->isDown();

}

/**
Expand Down
10 changes: 4 additions & 6 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\Maintenance\MaintenanceMode;
use Throwable;

class DownCommand extends Command
Expand Down Expand Up @@ -35,19 +36,16 @@ class DownCommand extends Command
*
* @return int
*/
public function handle()
public function handle(MaintenanceMode $maintenanceMode)
Copy link
Member

Choose a reason for hiding this comment

The 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'),
Expand Down
7 changes: 4 additions & 3 deletions src/Illuminate/Foundation/Console/UpCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Exception;
use Illuminate\Console\Command;
use Illuminate\Foundation\Events\MaintenanceModeDisabled;
use Illuminate\Maintenance\MaintenanceMode;

class UpCommand extends Command
{
Expand All @@ -27,16 +28,16 @@ class UpCommand extends Command
*
* @return int
*/
public function handle()
public function handle(MaintenanceMode $maintenanceMode)
Copy link
Member

Choose a reason for hiding this comment

The 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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add an isUp method as well.

$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'));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -23,15 +24,21 @@ class PreventRequestsDuringMaintenance
*/
protected $except = [];

/**
* @var MaintenanceMode
Copy link
Member

Choose a reason for hiding this comment

The 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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding a new dependency here is a breaking change unfortunately.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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;
}

/**
Expand All @@ -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']);
Expand Down
38 changes: 38 additions & 0 deletions src/Illuminate/Maintenance/MaintenanceMode.php
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');
}
}