|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace NormanHuth\Library\Http\Middleware; |
| 4 | + |
| 5 | +use Closure; |
| 6 | +use ErrorException; |
| 7 | +use Illuminate\Foundation\Http\Middleware\PreventRequestsDuringMaintenance; |
| 8 | +use NormanHuth\Library\Exceptions\MaintenanceException; |
| 9 | + |
| 10 | +class PreventRequestsDuringMaintenanceMiddleware extends PreventRequestsDuringMaintenance |
| 11 | +{ |
| 12 | + /** |
| 13 | + * Handle an incoming request. |
| 14 | + * |
| 15 | + * @param \Illuminate\Http\Request $request |
| 16 | + * |
| 17 | + * @throws \ErrorException |
| 18 | + */ |
| 19 | + public function handle($request, Closure $next): mixed |
| 20 | + { |
| 21 | + if ($this->inExceptArray($request)) { |
| 22 | + return $next($request); |
| 23 | + } |
| 24 | + |
| 25 | + if ($this->app->maintenanceMode()->active()) { |
| 26 | + try { |
| 27 | + $data = $this->app->maintenanceMode()->data(); |
| 28 | + } catch (ErrorException $exception) { |
| 29 | + if (! $this->app->maintenanceMode()->active()) { |
| 30 | + return $next($request); |
| 31 | + } |
| 32 | + |
| 33 | + throw $exception; |
| 34 | + } |
| 35 | + |
| 36 | + if (isset($data['secret']) && $request->path() === $data['secret']) { |
| 37 | + return $this->bypassResponse($data['secret']); |
| 38 | + } |
| 39 | + |
| 40 | + if ($this->hasValidBypassCookie($request, $data)) { |
| 41 | + return $next($request); |
| 42 | + } |
| 43 | + |
| 44 | + if (isset($data['redirect'])) { |
| 45 | + $path = $data['redirect'] === '/' |
| 46 | + ? $data['redirect'] |
| 47 | + : trim($data['redirect'], '/'); |
| 48 | + |
| 49 | + if ($request->path() !== $path) { |
| 50 | + return redirect($path); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + if (isset($data['template'])) { |
| 55 | + return response( |
| 56 | + $data['template'], |
| 57 | + $data['status'] ?? 503, |
| 58 | + $this->getHeaders($data) |
| 59 | + ); |
| 60 | + } |
| 61 | + |
| 62 | + throw new MaintenanceException( |
| 63 | + $data['status'] ?? 503, |
| 64 | + 'Service Unavailable', |
| 65 | + null, |
| 66 | + $this->getHeaders($data) |
| 67 | + ); |
| 68 | + } |
| 69 | + |
| 70 | + return $next($request); |
| 71 | + } |
| 72 | +} |
0 commit comments