diff --git a/src/Illuminate/Contracts/Foundation/Application.php b/src/Illuminate/Contracts/Foundation/Application.php index e4a0bc20cd6c..081be267a306 100644 --- a/src/Illuminate/Contracts/Foundation/Application.php +++ b/src/Illuminate/Contracts/Foundation/Application.php @@ -83,6 +83,13 @@ public function runningInConsole(); */ public function runningUnitTests(); + /** + * Get an instance of the maintenance mode manager implementation. + * + * @return \Illuminate\Contracts\Foundation\MaintenanceMode + */ + public function maintenanceMode(); + /** * Determine if the application is currently down for maintenance. * diff --git a/src/Illuminate/Contracts/Foundation/MaintenanceMode.php b/src/Illuminate/Contracts/Foundation/MaintenanceMode.php new file mode 100644 index 000000000000..4c948f702d8e --- /dev/null +++ b/src/Illuminate/Contracts/Foundation/MaintenanceMode.php @@ -0,0 +1,35 @@ +make(MaintenanceModeContract::class); + } + /** * Determine if the application is currently down for maintenance. * @@ -1112,7 +1123,7 @@ public function addAbsoluteCachePathPrefix($prefix) */ public function isDownForMaintenance() { - return file_exists($this->storagePath().'/framework/down'); + return $this->maintenanceMode()->active(); } /** diff --git a/src/Illuminate/Foundation/Console/DownCommand.php b/src/Illuminate/Foundation/Console/DownCommand.php index 8e36453e1cda..052f87192f2a 100644 --- a/src/Illuminate/Foundation/Console/DownCommand.php +++ b/src/Illuminate/Foundation/Console/DownCommand.php @@ -47,16 +47,13 @@ class DownCommand extends Command public function handle() { try { - if (is_file(storage_path('framework/down'))) { + if ($this->laravel->maintenanceMode()->active()) { $this->comment('Application is already down.'); return 0; } - file_put_contents( - storage_path('framework/down'), - json_encode($this->getDownFilePayload(), JSON_PRETTY_PRINT) - ); + $this->laravel->maintenanceMode()->activate($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 84684de66d93..45eaa62c8d51 100644 --- a/src/Illuminate/Foundation/Console/UpCommand.php +++ b/src/Illuminate/Foundation/Console/UpCommand.php @@ -39,13 +39,13 @@ class UpCommand extends Command public function handle() { try { - if (! is_file(storage_path('framework/down'))) { + if (! $this->laravel->maintenanceMode()->active()) { $this->comment('Application is already up.'); return 0; } - unlink(storage_path('framework/down')); + $this->laravel->maintenanceMode()->deactivate(); if (is_file(storage_path('framework/maintenance.php'))) { unlink(storage_path('framework/maintenance.php')); diff --git a/src/Illuminate/Foundation/FileBasedMaintenanceMode.php b/src/Illuminate/Foundation/FileBasedMaintenanceMode.php new file mode 100644 index 000000000000..c63522ce34f3 --- /dev/null +++ b/src/Illuminate/Foundation/FileBasedMaintenanceMode.php @@ -0,0 +1,64 @@ +path(), + json_encode($payload, JSON_PRETTY_PRINT) + ); + } + + /** + * Take the application out of maintenance. + * + * @return void + */ + public function deactivate(): void + { + if ($this->active()) { + unlink($this->path()); + } + } + + /** + * Determine if the application is currently down for maintenance. + * + * @return bool + */ + public function active(): bool + { + return file_exists($this->path()); + } + + /** + * Get the data array which was provided when the application was placed into maintenance. + * + * @return array + */ + public function data(): array + { + return json_decode(file_get_contents($this->path()), true); + } + + /** + * Get the path where the file is stored that signals that the application is down for maintenance. + * + * @return string + */ + protected function path(): string + { + return storage_path('framework/down'); + } +} diff --git a/src/Illuminate/Foundation/Http/Middleware/PreventRequestsDuringMaintenance.php b/src/Illuminate/Foundation/Http/Middleware/PreventRequestsDuringMaintenance.php index a8692bc4f7e3..ef058661979e 100644 --- a/src/Illuminate/Foundation/Http/Middleware/PreventRequestsDuringMaintenance.php +++ b/src/Illuminate/Foundation/Http/Middleware/PreventRequestsDuringMaintenance.php @@ -45,8 +45,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->app->maintenanceMode()->active()) { + $data = $this->app->maintenanceMode()->data(); if (isset($data['secret']) && $request->path() === $data['secret']) { return $this->bypassResponse($data['secret']); diff --git a/src/Illuminate/Foundation/Providers/FoundationServiceProvider.php b/src/Illuminate/Foundation/Providers/FoundationServiceProvider.php index 9a1c748a19e6..558181d8db0e 100644 --- a/src/Illuminate/Foundation/Providers/FoundationServiceProvider.php +++ b/src/Illuminate/Foundation/Providers/FoundationServiceProvider.php @@ -2,6 +2,9 @@ namespace Illuminate\Foundation\Providers; +use Illuminate\Contracts\Foundation\MaintenanceMode as MaintenanceModeContract; +use Illuminate\Foundation\FileBasedMaintenanceMode; +use Illuminate\Foundation\MaintenanceMode; use Illuminate\Http\Request; use Illuminate\Log\Events\MessageLogged; use Illuminate\Support\AggregateServiceProvider; @@ -48,6 +51,7 @@ public function register() $this->registerRequestValidation(); $this->registerRequestSignatureValidation(); $this->registerExceptionTracking(); + $this->registerMaintenanceModeManager(); } /** @@ -117,4 +121,14 @@ protected function registerExceptionTracking() } }); } + + /** + * Register the maintenance mode manager service. + * + * @return void + */ + public function registerMaintenanceModeManager() + { + $this->app->bind(MaintenanceModeContract::class, FileBasedMaintenanceMode::class); + } }