Skip to content
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
7 changes: 7 additions & 0 deletions src/Illuminate/Contracts/Foundation/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,13 @@ public function runningInConsole();
*/
public function runningUnitTests();

/**
* Get the instance of the MaintenanceMode.
*
* @return \Illuminate\Foundation\MaintenanceMode
*/
public function maintenanceMode();
Copy link
Member

Choose a reason for hiding this comment

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

Forgot to mention but you'll need to remove this change since this is still a breaking change.

Copy link
Contributor Author

@wimulkeman wimulkeman Dec 16, 2021

Choose a reason for hiding this comment

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

I would suggest in that case to leave the PR to 9.x open instead. Introducing the method without updating the interface feels invalid to me.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah that's okay 👍


/**
* Determine if the application is currently down for maintenance.
*
Expand Down
12 changes: 11 additions & 1 deletion src/Illuminate/Foundation/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -1102,14 +1102,24 @@ 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.
*
* @return bool
*/
public function isDownForMaintenance()
{
return file_exists($this->storagePath().'/framework/down');
return $this->maintenanceMode()->isDown();
}

/**
Expand Down
7 changes: 2 additions & 5 deletions src/Illuminate/Foundation/Console/DownCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down
4 changes: 2 additions & 2 deletions src/Illuminate/Foundation/Console/UpCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand Down
74 changes: 74 additions & 0 deletions src/Illuminate/Foundation/MaintenanceMode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

namespace Illuminate\Foundation;

use function storage_path;

class MaintenanceMode
{
/**
* Determine if the application is currently down for maintenance.
*
* @return bool
*/
public function isDown(): bool
{
return file_exists($this->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');
}
}