Skip to content
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

[9.x] Move maintenance mode logic #40070

Merged
merged 27 commits into from
Dec 17, 2021
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
e230133
chore (maintenance): move maintenance mode logic
Dec 15, 2021
09031d4
refactor (maintenance): add isUp method
Dec 16, 2021
29aaa45
doc (maintenance): document code
Dec 16, 2021
113f193
fix (maintenance): correctly check if in maintenance
Dec 16, 2021
c99e067
refactor (maintenance): use make method
Dec 16, 2021
27d5c68
doc (maintenance): update docblock
Dec 16, 2021
48aa9ff
doc (maintenance): update docblock
Dec 16, 2021
f66ba1c
style (doc): update documentation style
Dec 16, 2021
1ee6193
style (doc): update documentation style
Dec 16, 2021
6ec23f6
refactor (maintenance): change namespace
Dec 16, 2021
10ff03a
style (doc): update documentation style
Dec 16, 2021
10d2020
style (doc): update documentation style
Dec 16, 2021
1835d14
Update src/Illuminate/Foundation/Http/Middleware/PreventRequestsDurin…
wimulkeman Dec 16, 2021
b15b8d1
Update src/Illuminate/Foundation/Http/Middleware/PreventRequestsDurin…
wimulkeman Dec 16, 2021
7f3d335
refactor (maintenance): move to application
Dec 16, 2021
59e4e7e
doc (maintenance): add DockBlock
Dec 16, 2021
1e9633f
refactor (maintenance): use application method
Dec 16, 2021
614c6a0
Update src/Illuminate/Foundation/MaintenanceMode.php
wimulkeman Dec 16, 2021
45c9236
Update src/Illuminate/Foundation/MaintenanceMode.php
wimulkeman Dec 16, 2021
2121afb
refactor (maintenance): remove comparison
Dec 16, 2021
2ec1619
chore (code): remove unused import
Dec 16, 2021
040d691
refactor (maintenance): add interface
Dec 16, 2021
b17e090
style (doc): update documentation style
Dec 16, 2021
8dd9f7a
refactor (maintenance): bind instance to contract
Dec 17, 2021
838f09c
style (doc): update documentation style
Dec 17, 2021
c617f62
Update src/Illuminate/Foundation/Providers/FoundationServiceProvider.php
wimulkeman Dec 17, 2021
54ce40c
formatting and renaming
taylorotwell Dec 17, 2021
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
8 changes: 8 additions & 0 deletions src/Illuminate/Contracts/Foundation/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Illuminate\Contracts\Foundation;

use Illuminate\Contracts\Container\Container;
use Illuminate\Foundation\MaintenanceMode;

interface Application extends Container
{
Expand Down Expand Up @@ -83,6 +84,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.
*
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 @@ -1105,14 +1105,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);
wimulkeman marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* 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 @@ -47,16 +47,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 @@ -39,13 +39,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
76 changes: 76 additions & 0 deletions src/Illuminate/Foundation/MaintenanceMode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?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) {
return;
}

unlink($this->getDownFilePath());
wimulkeman marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* 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
*/
private function getDownFilePath(): string
wimulkeman marked this conversation as resolved.
Show resolved Hide resolved
wimulkeman marked this conversation as resolved.
Show resolved Hide resolved
{
return storage_path('framework/down');
}
}