8000 [9.x] Move maintenance mode logic by wimulkeman · Pull Request #40070 · laravel/framework · GitHub
[go: up one dir, main page]

Skip to content

[9.x] Move maintenance mode logic #40070

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

Merged
merged 27 commits into from
Dec 17, 2021
Merged
Show file tree
Hide file tree
Changes from 12 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
8000
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
2 changes: 1 addition & 1 deletion src/Illuminate/Foundation/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -1112,7 +1112,7 @@ public function addAbsoluteCachePathPrefix($prefix)
*/
public function isDownForMaintenance()
{
return file_exists($this->storagePath().'/framework/down');
return $this->make(MaintenanceMode::class)->isDown();
}

/**
Expand Down
11 changes: 5 additions & 6 deletions src/Illuminate/Foundation/Console/DownCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Console\Command;
use Illuminate\Foundation\Events\MaintenanceModeEnabled;
use Illuminate\Foundation\Exceptions\RegisterErrorViewPaths;
use Illuminate\Foundation\MaintenanceMode;
use Throwable;

class DownCommand extends Command
Expand Down Expand Up @@ -42,21 +43,19 @@ class DownCommand extends Command
/**
* Execute the console command.
*
* @param \Illuminate\Foundation\MaintenanceMode $maintenanceMode
* @return int
*/
public function handle()
public function handle(MaintenanceMode $maintenanceMode)
{
try {
if (is_file(storage_path('framework/down'))) {
if ($maintenanceMode->isDown()) {
$this->comment('Application is already down.');

return 0;
}

file_put_contents(
storage_path('framework/down'),
json_encode($this->getDownFilePayload(), JSON_PRETTY_PRINT)
);
$maintenanceMode->down($this->getDownFilePayload());

file_put_contents(
storage_path('framework/maintenance.php'),
Expand Down
8 changes: 5 additions & 3 deletions src/Illuminate/Foundation/Console/UpCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Exception;
use Illuminate\Console\Command;
use Illuminate\Foundation\Events\MaintenanceModeDisabled;
use Illuminate\Foundation\MaintenanceMode;

class UpCommand extends Command
{
Expand Down Expand Up @@ -34,18 +35,19 @@ class UpCommand extends Command
/**
* Execute the console command.
*
* @param \Illuminate\Foundation\MaintenanceMode $maintenanceMode
* @return int
*/
public function handle()
public function handle(MaintenanceMode $maintenanceMode)
{
try {
if (! is_file(storage_path('framework/down'))) {
if ($maintenanceMode->isUp()) {
$this->comment('Application is already up.');

return 0;
}

unlink(storage_path('framework/down'));
$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 @@ -5,6 +5,7 @@
use Closure;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Foundation\Http\MaintenanceModeBypassCookie;
use Illuminate\Foundation\MaintenanceMode;
use Symfony\Component\HttpKernel\Exception\HttpException;

class PreventRequestsDuringMaintenance
Expand All @@ -23,15 +24,24 @@ class PreventRequestsDuringMaintenance
*/
protected $except = [];

/**
* The maintenance mode implementation. Can be used to check
* if the application is under maintenance.
*
* @var \Illuminate\Foundation\MaintenanceMode
*/
protected $maintenanceMode;

/**
* Create a new middleware instance.
*
* @param \Illuminate\Contracts\Foundation\Application $app
* @return void
* @param \Illuminate\Foundation\MaintenanceMode $maintenanceMode
*/
public function __construct(Application $app)
public function __construct(Application $app, MaintenanceMode $maintenanceMode)
{
$this->app = $app;
$this->maintenanceMode = $maintenanceMode;
}

/**
Expand All @@ -45,8 +55,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->maintenanceMode->isDown()) {
$data = $this->maintenanceMode->getPayload();

if (isset($data['secret']) && $request->path() === $data['secret']) {
return $this->bypassResponse($data['secret']);
Expand Down
45 changes: 45 additions & 0 deletions src/Illuminate/Foundation/MaintenanceMode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Illuminate\Foundation;

use function storage_path;

class MaintenanceMode
{
public function isDown(): bool
{
return file_exists($this->getDownFilePath());
}

public function isUp(): bool
{
return $this->isDown() === false;
}

public function down(array $payload): void
{
file_put_contents(
$this->getDownFilePath(),
json_encode($payload, JSON_PRETTY_PRINT)
);
}

public function up(): void
{
if ($this->isDown() === false) {
return;
}

unlink($this->getDownFilePath());
}

public function getPayload(): array
{
return json_decode(file_get_contents($this->getDownFilePath()), true);
}

private function getDownFilePath(): string
{
return storage_path('framework/down');
}
}
0