Last active
July 21, 2020 12:08
-
-
Save elishaukpong/a4a87a23a163b6dfd19141a24ee3f9da to your computer and use it in GitHub Desktop.
Laravel's default check for maintenance middleware class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
app/Http/Middleware/CheckForMaintenanceMode.php | |
<?php | |
namespace App\Http\Middleware; | |
use Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode as Middleware; | |
class CheckForMaintenanceMode extends Middleware | |
{ | |
/** | |
* The URIs that should be reachable while maintenance mode is enabled. | |
* | |
* @var array | |
*/ | |
protected $except = [ | |
// | |
]; | |
} | |
vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/CheckForMaintenanceMode.php | |
<?php | |
namespace Illuminate\Foundation\Http\Middleware; | |
use Closure; | |
use Symfony\Component\HttpFoundation\IpUtils; | |
use Illuminate\Contracts\Foundation\Application; | |
use Illuminate\Foundation\Http\Exceptions\MaintenanceModeException; | |
class CheckForMaintenanceMode | |
{ | |
/** | |
* The application implementation. | |
* | |
* @var \Illuminate\Contracts\Foundation\Application | |
*/ | |
protected $app; | |
/** | |
* The URIs that should be accessible while maintenance mode is enabled. | |
* | |
* @var array | |
*/ | |
protected $except = []; | |
/** | |
* Create a new middleware instance. | |
* | |
* @param \Illuminate\Contracts\Foundation\Application $app | |
* @return void | |
*/ | |
public function __construct(Application $app) | |
{ | |
$this->app = $app; | |
} | |
/** | |
* Handle an incoming request. | |
* | |
* @param \Illuminate\Http\Request $request | |
* @param \Closure $next | |
* @return mixed | |
* | |
* @throws \Symfony\Component\HttpKernel\Exception\HttpException | |
*/ | |
public function handle($request, Closure $next) | |
{ | |
if ($this->app->isDownForMaintenance()) { | |
$data = json_decode(file_get_contents($this->app->storagePath().'/framework/down'), true); | |
if (isset($data['allowed']) && IpUtils::checkIp($request->ip(), (array) $data['allowed'])) { | |
return $next($request); | |
} | |
if ($this->inExceptArray($request)) { | |
return $next($request); | |
} | |
throw new MaintenanceModeException($data['time'], $data['retry'], $data['message']); | |
} | |
return $next($request); | |
} | |
/** | |
* Determine if the request has a URI that should be accessible in maintenance mode. | |
* | |
* @param \Illuminate\Http\Request $request | |
* @return bool | |
*/ | |
protected function inExceptArray($request) | |
{ | |
foreach ($this->except as $except) { | |
if ($except !== '/') { | |
$except = trim($except, '/'); | |
} | |
if ($request->fullUrlIs($except) || $request->is($except)) { | |
return true; | |
} | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment