diff --git a/src/Illuminate/Contracts/Foundation/Application.php b/src/Illuminate/Contracts/Foundation/Application.php index 74af47c751cb..971ea5f86d6e 100644 --- a/src/Illuminate/Contracts/Foundation/Application.php +++ b/src/Illuminate/Contracts/Foundation/Application.php @@ -82,6 +82,13 @@ public function runningInConsole(); */ public function runningUnitTests(); + /** + * Get the instance of the MaintenanceMode. + * + * @return \Illuminate\Foundation\MaintenanceMode + */ + public function maintenanceMode(); + /** * Determine if the application is currently down for maintenance. * diff --git a/src/Illuminate/Foundation/Application.php b/src/Illuminate/Foundation/Application.php index 49313cb24f5c..f7904a055c5e 100755 --- a/src/Illuminate/Foundation/Application.php +++ b/src/Illuminate/Foundation/Application.php @@ -1102,6 +1102,16 @@ public function addAbsoluteCachePathPrefix($prefix) return $this; } + /** + * Get the instance of the MaintenanceMode. + * + * @return \Illuminate\Foundation\MaintenanceMode + */ + public function maintenanceMode() + { + return $this->make(MaintenanceMode::class); + } + /** * Determine if the application is currently down for maintenance. * @@ -1109,7 +1119,7 @@ public function addAbsoluteCachePathPrefix($prefix) */ public function isDownForMaintenance() { - return file_exists($this->storagePath().'/framework/down'); + return $this->maintenanceMode()->isDown(); } /** diff --git a/src/Illuminate/Foundation/Console/DownCommand.php b/src/Illuminate/Foundation/Console/DownCommand.php index 676715af4701..382e0e685dd7 100644 --- a/src/Illuminate/Foundation/Console/DownCommand.php +++ b/src/Illuminate/Foundation/Console/DownCommand.php @@ -38,16 +38,13 @@ class DownCommand extends Command public function handle() { try { - if (is_file(storage_path('framework/down'))) { + if ($this->laravel->maintenanceMode()->isDown()) { $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()->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..77de464cc54c 100644 --- a/src/Illuminate/Foundation/Console/UpCommand.php +++ b/src/Illuminate/Foundation/Console/UpCommand.php @@ -30,13 +30,13 @@ class UpCommand extends Command public function handle() { try { - if (! is_file(storage_path('framework/down'))) { + if ($this->laravel->maintenanceMode()->isUp()) { $this->comment('Application is already up.'); return 0; } - unlink(storage_path('framework/down')); + $this->laravel->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..c36fbc5fb27d 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()->isDown()) { + $data = $this->app->maintenanceMode()->getPayload(); if (isset($data['secret']) && $request->path() === $data['secret']) { return $this->bypassResponse($data['secret']); diff --git a/src/Illuminate/Foundation/MaintenanceMode.php b/src/Illuminate/Foundation/MaintenanceMode.php new file mode 100644 index 000000000000..c6df51bb60cd --- /dev/null +++ b/src/Illuminate/Foundation/MaintenanceMode.php @@ -0,0 +1,74 @@ +getDownFilePath()); + } + + /** + * Determine if the application is currently up. + * + * @return bool + */ + public function isUp(): bool + { + return $this->isDown() === false; + } + + /** + * Take the application down for maintenance. + * + * @param array $payload + * @return void + */ + public function down(array $payload): void + { + file_put_contents( + $this->getDownFilePath(), + json_encode($payload, JSON_PRETTY_PRINT) + ); + } + + /** + * Take the application out of maintenance. + * + * @return void + */ + public function up(): void + { + if ($this->isDown() !== false) { + unlink($this->getDownFilePath()); + } + } + + /** + * Get the payload which was provided while the application was placed into maintenance. + * + * @return array + */ + public function getPayload(): array + { + return json_decode(file_get_contents($this->getDownFilePath()), true); + } + + /** + * Get the path where the file is stored that signals that the application is down for maintenance. + * + * @return string + */ + protected function getDownFilePath(): string + { + return storage_path('framework/down'); + } +}