From be8c3bd0c8d0b601bb9f73f7bc7a0060096615a4 Mon Sep 17 00:00:00 2001 From: Wim Ulkeman Date: Wed, 15 Dec 2021 16:35:01 +0100 Subject: [PATCH] chore (maintenance): move maintenance mode logic Moving the maintenance mode logic to its own class. This enables users to extend/overwrite the class with their own logic. Overwriting the logic may be needed if for example the application is running on multiple servers. Fixes #36474 --- src/Illuminate/Foundation/Application.php | 3 +- .../Foundation/Console/DownCommand.php | 10 ++--- .../Foundation/Console/UpCommand.php | 7 ++-- .../PreventRequestsDuringMaintenance.php | 13 +++++-- .../Maintenance/MaintenanceMode.php | 38 +++++++++++++++++++ 5 files changed, 58 insertions(+), 13 deletions(-) create mode 100644 src/Illuminate/Maintenance/MaintenanceMode.php diff --git a/src/Illuminate/Foundation/Application.php b/src/Illuminate/Foundation/Application.php index 49313cb24f5c..8ab461ded40b 100755 --- a/src/Illuminate/Foundation/Application.php +++ b/src/Illuminate/Foundation/Application.php @@ -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; @@ -1109,7 +1110,7 @@ public function addAbsoluteCachePathPrefix($prefix) */ public function isDownForMaintenance() { - return file_exists($this->storagePath().'/framework/down'); + return app(MaintenanceMode::class)->isDown(); } /** diff --git a/src/Illuminate/Foundation/Console/DownCommand.php b/src/Illuminate/Foundation/Console/DownCommand.php index 676715af4701..078aa99a9409 100644 --- a/src/Illuminate/Foundation/Console/DownCommand.php +++ b/src/Illuminate/Foundation/Console/DownCommand.php @@ -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) { 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'), diff --git a/src/Illuminate/Foundation/Console/UpCommand.php b/src/Illuminate/Foundation/Console/UpCommand.php index b651247dbab2..bc5337c85dd1 100644 --- a/src/Illuminate/Foundation/Console/UpCommand.php +++ b/src/Illuminate/Foundation/Console/UpCommand.php @@ -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) { try { - if (! is_file(storage_path('framework/down'))) { + if ($maintenanceMode->isDown() === false) { $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')); diff --git a/src/Illuminate/Foundation/Http/Middleware/PreventRequestsDuringMaintenance.php b/src/Illuminate/Foundation/Http/Middleware/PreventRequestsDuringMaintenance.php index a8692bc4f7e3..5e8d2ed7f2cc 100644 --- a/src/Illuminate/Foundation/Http/Middleware/PreventRequestsDuringMaintenance.php +++ b/src/Illuminate/Foundation/Http/Middleware/PreventRequestsDuringMaintenance.php @@ -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 + */ + 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) { $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']); diff --git a/src/Illuminate/Maintenance/MaintenanceMode.php b/src/Illuminate/Maintenance/MaintenanceMode.php new file mode 100644 index 000000000000..84e1a42ed78b --- /dev/null +++ b/src/Illuminate/Maintenance/MaintenanceMode.php @@ -0,0 +1,38 @@ +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'); + } +}