1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Rdehnhardt\MaintenanceMode\Http\Middleware; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use Illuminate\Contracts\Foundation\Application; |
7
|
|
|
use Illuminate\Http\Request; |
8
|
|
|
use Illuminate\Http\Response; |
9
|
|
|
use Rdehnhardt\MaintenanceMode\MaintenanceModeService; |
10
|
|
|
|
11
|
|
|
class MaintenanceModeMiddleware |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Maintenance Mode Service. |
15
|
|
|
* |
16
|
|
|
* @var \Rdehnhardt\MaintenanceMode\MaintenanceModeService |
17
|
|
|
*/ |
18
|
|
|
protected $maintenance; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Lumen Application Instance. |
22
|
|
|
* |
23
|
|
|
* @var \Laravel\Lumen\Application |
24
|
|
|
*/ |
25
|
|
|
protected $app; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* View Factory. |
29
|
|
|
* |
30
|
|
|
* @var \Illuminate\View\Factory |
31
|
|
|
*/ |
32
|
|
|
protected $view; |
33
|
|
|
|
34
|
|
|
public function __construct(MaintenanceModeService $maintenance, Application $app) |
35
|
|
|
{ |
36
|
|
|
$this->maintenance = $maintenance; |
37
|
|
|
$this->app = $app; |
|
|
|
|
38
|
|
|
$this->view = $app['view']; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Handle incoming requests. |
43
|
|
|
* |
44
|
|
|
* @param Request $request |
45
|
|
|
* @param \Closure $next |
46
|
|
|
* |
47
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
48
|
|
|
* @throws \Symfony\Component\HttpKernel\Exception\HttpException |
49
|
|
|
* @throws \InvalidArgumentException |
50
|
|
|
*/ |
51
|
|
|
public function handle($request, Closure $next) |
52
|
|
|
{ |
53
|
|
|
if ($this->maintenance->isDownMode() && !$this->maintenance->checkAllowedIp($this->getIp())) { |
54
|
|
|
if ($this->view->exists('errors.503')) { |
55
|
|
|
return new Response($this->view->make('errors.503'), 503); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return $this->app->abort(503, 'The application is down for maintenance.'); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return $next($request); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Get client ip |
66
|
|
|
*/ |
67
|
|
|
private function getIp() |
|
|
|
|
68
|
|
|
{ |
69
|
|
|
if (!empty($_SERVER['HTTP_CLIENT_IP'])) { |
70
|
|
|
return $_SERVER['HTTP_CLIENT_IP']; |
71
|
|
|
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { |
72
|
|
|
return $_SERVER['HTTP_X_FORWARDED_FOR']; |
73
|
|
|
} else { |
74
|
|
|
return $_SERVER['REMOTE_ADDR']; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..