Skip to content

Commit dc47126

Browse files
wimulkemanWim Ulkemandriesvintsaxlontaylorotwell
authored
[9.x] Move maintenance mode logic (#40070)
* 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 * refactor (maintenance): add isUp method Adding the isUp method to check if the application is up or is down for maintenance. Relates to #36474 * doc (maintenance): document code Extend the documentation about what the code does. Also use FQN. Relates to #36474 * fix (maintenance): correctly check if in maintenance Relates to #36474 * refactor (maintenance): use make method Use the make method instead of the app function to retrieve an instance of the MaintenanceMode container. Relates to #36474 * doc (maintenance): update docblock Update the docblock with the newly added method param. Relates to #36474 * doc (maintenance): update docblock Update the docblock with the newly added method param. Relates to #36474 * style (doc): update documentation style Updating documentation style to comply to the style used within the Laravel codebase. * style (doc): update documentation style Updating documentation style to comply to the style used within the Laravel codebase. * refactor (maintenance): change namespace Move the class from its own namespace to the Foundation namespace. Relates to #36474 * style (doc): update documentation style Updating documentation style to comply to the style used within the Laravel codebase. * style (doc): update documentation style Updating documentation style to comply to the style used within the Laravel codebase. * Update src/Illuminate/Foundation/Http/Middleware/PreventRequestsDuringMaintenance.php Co-authored-by: Dries Vints <[email protected]> * Update src/Illuminate/Foundation/Http/Middleware/PreventRequestsDuringMaintenance.php Co-authored-by: Dries Vints <[email protected]> * refactor (maintenance): move to application Create an application methode to retrieve the MaintenanceMode instance. This method reduces the need to fetch the MaintenanceMode with the usage of an additional argument in the middleware. Relates to #36474 * doc (maintenance): add DockBlock Adding documentation for the newly added methods. Relates to #36474 * refactor (maintenance): use application method Use the application method instead of using autowiring. The application method provides the MaintenanceMode instance which removes the requirement to inject the class in the handler method. Relates to #36474 * Update src/Illuminate/Foundation/MaintenanceMode.php Co-authored-by: Dries Vints <[email protected]> * Update src/Illuminate/Foundation/MaintenanceMode.php Co-authored-by: Dries Vints <[email protected]> * refactor (maintenance): remove comparison Remove the comparison with the false value. The method always returns a bool which only allows a true or false return value. This make the comparison redundant. Relates to #36474 * chore (code): remove unused import Remove the unused import. Because the FQN is used in the DocBlock the import has become redundant. * refactor (maintenance): add interface Add an interface under the contracts to enable developers to easily extend the MaintenanceMode class. * style (doc): update documentation style Updating documentation style to comply to the style used within the Laravel codebase. * refactor (maintenance): bind instance to contract Bind the default MaintenanceMode class to the contract reference in the service container. This enables users to set-up their own contract binding to overwrite the default Maintenance Mode behavior. * style (doc): update documentation style Updating documentation style to comply to the style used within the Laravel codebase. * Update src/Illuminate/Foundation/Providers/FoundationServiceProvider.php Co-authored-by: Choraimy Kroonstuiver <[email protected]> * formatting and renaming Co-authored-by: Wim Ulkeman <[email protected]> Co-authored-by: Dries Vints <[email protected]> Co-authored-by: Choraimy Kroonstuiver <[email protected]> Co-authored-by: Taylor Otwell <[email protected]>
1 parent ec7045e commit dc47126

File tree

8 files changed

+138
-10
lines changed

8 files changed

+138
-10
lines changed

src/Illuminate/Contracts/Foundation/Application.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,13 @@ public function runningInConsole();
8383
*/
8484
public function runningUnitTests();
8585

86+
/**
87+
* Get an instance of the maintenance mode manager implementation.
88+
*
89+
* @return \Illuminate\Contracts\Foundation\MaintenanceMode
90+
*/
91+
public function maintenanceMode();
92+
8693
/**
8794
* Determine if the application is currently down for maintenance.
8895
*
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Illuminate\Contracts\Foundation;
4+
5+
interface MaintenanceMode
6+
{
7+
/**
8+
* Take the application down for maintenance.
9+
*
10+
* @param array $payload
11+
* @return void
12+
*/
13+
public function activate(array $payload): void;
14+
15+
/**
16+
* Take the application out of maintenance.
17+
*
18+
* @return void
19+
*/
20+
public function deactivate(): void;
21+
22+
/**
23+
* Determine if the application is currently down for maintenance.
24+
*
25+
* @return bool
26+
*/
27+
public function active(): bool;
28+
29+
/**
30+
* Get the data array which was provided when the application was placed into maintenance.
31+
*
32+
* @return array
33+
*/
34+
public function data(): array;
35+
}

src/Illuminate/Foundation/Application.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Illuminate\Contracts\Foundation\Application as ApplicationContract;
88
use Illuminate\Contracts\Foundation\CachesConfiguration;
99
use Illuminate\Contracts\Foundation\CachesRoutes;
10+
use Illuminate\Contracts\Foundation\MaintenanceMode as MaintenanceModeContract;
1011
use Illuminate\Contracts\Http\Kernel as HttpKernelContract;
1112
use Illuminate\Events\EventServiceProvider;
1213
use Illuminate\Filesystem\Filesystem;
@@ -1105,14 +1106,24 @@ public function addAbsoluteCachePathPrefix($prefix)
11051106
return $this;
11061107
}
11071108

1109+
/**
1110+
* Get an instance of the maintenance mode manager implementation.
1111+
*
1112+
* @return \Illuminate\Contracts\Foundation\MaintenanceMode
1113+
*/
1114+
public function maintenanceMode()
1115+
{
1116+
return $this->make(MaintenanceModeContract::class);
1117+
}
1118+
11081119
/**
11091120
* Determine if the application is currently down for maintenance.
11101121
*
11111122
* @return bool
11121123
*/
11131124
public function isDownForMaintenance()
11141125
{
1115-
return file_exists($this->storagePath().'/framework/down');
1126+
return $this->maintenanceMode()->active();
11161127
}
11171128

11181129
/**

src/Illuminate/Foundation/Console/DownCommand.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,13 @@ class DownCommand extends Command
4747
public function handle()
4848
{
4949
try {
50-
if (is_file(storage_path('framework/down'))) {
50+
if ($this->laravel->maintenanceMode()->active()) {
5151
$this->comment('Application is already down.');
5252

5353
return 0;
5454
}
5555

56-
file_put_contents(
57-
storage_path('framework/down'),
58-
json_encode($this->getDownFilePayload(), JSON_PRETTY_PRINT)
59-
);
56+
$this->laravel->maintenanceMode()->activate($this->getDownFilePayload());
6057

6158
file_put_contents(
6259
storage_path('framework/maintenance.php'),

src/Illuminate/Foundation/Console/UpCommand.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,13 @@ class UpCommand extends Command
3939
public function handle()
4040
{
4141
try {
42-
if (! is_file(storage_path('framework/down'))) {
42+
if (! $this->laravel->maintenanceMode()->active()) {
4343
$this->comment('Application is already up.');
4444

4545
return 0;
4646
}
4747

48-
unlink(storage_path('framework/down'));
48+
$this->laravel->maintenanceMode()->deactivate();
4949

5050
if (is_file(storage_path('framework/maintenance.php'))) {
5151
unlink(storage_path('framework/maintenance.php'));
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
namespace Illuminate\Foundation;
4+
5+
use Illuminate\Contracts\Foundation\MaintenanceMode as MaintenanceModeContract;
6+
7+
class FileBasedMaintenanceMode implements MaintenanceModeContract
8+
{
9+
/**
10+
* Take the application down for maintenance.
11+
*
12+
* @param array $payload
13+
* @return void
14+
*/
15+
public function activate(array $payload): void
16+
{
17+
file_put_contents(
18+
$this->path(),
19+
json_encode($payload, JSON_PRETTY_PRINT)
20+
);
21+
}
22+
23+
/**
24+
* Take the application out of maintenance.
25+
*
26+
* @return void
27+
*/
28+
public function deactivate(): void
29+
{
30+
if ($this->active()) {
31+
unlink($this->path());
32+
}
33+
}
34+
35+
/**
36+
* Determine if the application is currently down for maintenance.
37+
*
38+
* @return bool
39+
*/
40+
public function active(): bool
41+
{
42+
return file_exists($this->path());
43+
}
44+
45+
/**
46+
* Get the data array which was provided when the application was placed into maintenance.
47+
*
48+
* @return array
49+
*/
50+
public function data(): array
51+
{
52+
return json_decode(file_get_contents($this->path()), true);
53+
}
54+
55+
/**
56+
* Get the path where the file is stored that signals that the application is down for maintenance.
57+
*
58+
* @return string
59+
*/
60+
protected function path(): string
61+
{
62+
return storage_path('framework/down');
63+
}
64+
}

src/Illuminate/Foundation/Http/Middleware/PreventRequestsDuringMaintenance.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ public function __construct(Application $app)
4545
*/
4646
public function handle($request, Closure $next)
4747
{
48-
if ($this->app->isDownForMaintenance()) {
49-
$data = json_decode(file_get_contents($this->app->storagePath().'/framework/down'), true);
48+
if ($this->app->maintenanceMode()->active()) {
49+
$data = $this->app->maintenanceMode()->data();
5050

5151
if (isset($data['secret']) && $request->path() === $data['secret']) {
5252
return $this->bypassResponse($data['secret']);

src/Illuminate/Foundation/Providers/FoundationServiceProvider.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
namespace Illuminate\Foundation\Providers;
44

5+
use Illuminate\Contracts\Foundation\MaintenanceMode as MaintenanceModeContract;
6+
use Illuminate\Foundation\FileBasedMaintenanceMode;
7+
use Illuminate\Foundation\MaintenanceMode;
58
use Illuminate\Http\Request;
69
use Illuminate\Log\Events\MessageLogged;
710
use Illuminate\Support\AggregateServiceProvider;
@@ -48,6 +51,7 @@ public function register()
4851
$this->registerRequestValidation();
4952
$this->registerRequestSignatureValidation();
5053
$this->registerExceptionTracking();
54+
$this->registerMaintenanceModeManager();
5155
}
5256

5357
/**
@@ -117,4 +121,14 @@ protected function registerExceptionTracking()
117121
}
118122
});
119123
}
124+
125+
/**
126+
* Register the maintenance mode manager service.
127+
*
128+
* @return void
129+
*/
130+
public function registerMaintenanceModeManager()
131+
{
132+
$this->app->bind(MaintenanceModeContract::class, FileBasedMaintenanceMode::class);
133+
}
120134
}

0 commit comments

Comments
 (0)