| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
namespace Illuminate\Foundation\Console;
|
|
|
4 |
|
|
|
5 |
use App\Http\Middleware\PreventRequestsDuringMaintenance;
|
|
|
6 |
use Exception;
|
|
|
7 |
use Illuminate\Console\Command;
|
|
|
8 |
use Illuminate\Foundation\Events\MaintenanceModeEnabled;
|
|
|
9 |
use Illuminate\Foundation\Exceptions\RegisterErrorViewPaths;
|
|
|
10 |
use Symfony\Component\Console\Attribute\AsCommand;
|
|
|
11 |
use Throwable;
|
|
|
12 |
|
|
|
13 |
#[AsCommand(name: 'down')]
|
|
|
14 |
class DownCommand extends Command
|
|
|
15 |
{
|
|
|
16 |
/**
|
|
|
17 |
* The console command signature.
|
|
|
18 |
*
|
|
|
19 |
* @var string
|
|
|
20 |
*/
|
|
|
21 |
protected $signature = 'down {--redirect= : The path that users should be redirected to}
|
|
|
22 |
{--render= : The view that should be prerendered for display during maintenance mode}
|
|
|
23 |
{--retry= : The number of seconds after which the request may be retried}
|
|
|
24 |
{--refresh= : The number of seconds after which the browser may refresh}
|
|
|
25 |
{--secret= : The secret phrase that may be used to bypass maintenance mode}
|
|
|
26 |
{--status=503 : The status code that should be used when returning the maintenance mode response}';
|
|
|
27 |
|
|
|
28 |
/**
|
|
|
29 |
* The name of the console command.
|
|
|
30 |
*
|
|
|
31 |
* This name is used to identify the command during lazy loading.
|
|
|
32 |
*
|
|
|
33 |
* @var string|null
|
|
|
34 |
*
|
|
|
35 |
* @deprecated
|
|
|
36 |
*/
|
|
|
37 |
protected static $defaultName = 'down';
|
|
|
38 |
|
|
|
39 |
/**
|
|
|
40 |
* The console command description.
|
|
|
41 |
*
|
|
|
42 |
* @var string
|
|
|
43 |
*/
|
|
|
44 |
protected $description = 'Put the application into maintenance / demo mode';
|
|
|
45 |
|
|
|
46 |
/**
|
|
|
47 |
* Execute the console command.
|
|
|
48 |
*
|
|
|
49 |
* @return int
|
|
|
50 |
*/
|
|
|
51 |
public function handle()
|
|
|
52 |
{
|
|
|
53 |
try {
|
|
|
54 |
if ($this->laravel->maintenanceMode()->active()) {
|
|
|
55 |
$this->components->info('Application is already down.');
|
|
|
56 |
|
|
|
57 |
return 0;
|
|
|
58 |
}
|
|
|
59 |
|
|
|
60 |
$this->laravel->maintenanceMode()->activate($this->getDownFilePayload());
|
|
|
61 |
|
|
|
62 |
file_put_contents(
|
|
|
63 |
storage_path('framework/maintenance.php'),
|
|
|
64 |
file_get_contents(__DIR__.'/stubs/maintenance-mode.stub')
|
|
|
65 |
);
|
|
|
66 |
|
|
|
67 |
$this->laravel->get('events')->dispatch(new MaintenanceModeEnabled());
|
|
|
68 |
|
|
|
69 |
$this->components->info('Application is now in maintenance mode.');
|
|
|
70 |
} catch (Exception $e) {
|
|
|
71 |
$this->components->error(sprintf(
|
|
|
72 |
'Failed to enter maintenance mode: %s.',
|
|
|
73 |
$e->getMessage(),
|
|
|
74 |
));
|
|
|
75 |
|
|
|
76 |
return 1;
|
|
|
77 |
}
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
/**
|
|
|
81 |
* Get the payload to be placed in the "down" file.
|
|
|
82 |
*
|
|
|
83 |
* @return array
|
|
|
84 |
*/
|
|
|
85 |
protected function getDownFilePayload()
|
|
|
86 |
{
|
|
|
87 |
return [
|
|
|
88 |
'except' => $this->excludedPaths(),
|
|
|
89 |
'redirect' => $this->redirectPath(),
|
|
|
90 |
'retry' => $this->getRetryTime(),
|
|
|
91 |
'refresh' => $this->option('refresh'),
|
|
|
92 |
'secret' => $this->option('secret'),
|
|
|
93 |
'status' => (int) $this->option('status', 503),
|
|
|
94 |
'template' => $this->option('render') ? $this->prerenderView() : null,
|
|
|
95 |
];
|
|
|
96 |
}
|
|
|
97 |
|
|
|
98 |
/**
|
|
|
99 |
* Get the paths that should be excluded from maintenance mode.
|
|
|
100 |
*
|
|
|
101 |
* @return array
|
|
|
102 |
*/
|
|
|
103 |
protected function excludedPaths()
|
|
|
104 |
{
|
|
|
105 |
try {
|
|
|
106 |
return $this->laravel->make(PreventRequestsDuringMaintenance::class)->getExcludedPaths();
|
|
|
107 |
} catch (Throwable $e) {
|
|
|
108 |
return [];
|
|
|
109 |
}
|
|
|
110 |
}
|
|
|
111 |
|
|
|
112 |
/**
|
|
|
113 |
* Get the path that users should be redirected to.
|
|
|
114 |
*
|
|
|
115 |
* @return string
|
|
|
116 |
*/
|
|
|
117 |
protected function redirectPath()
|
|
|
118 |
{
|
|
|
119 |
if ($this->option('redirect') && $this->option('redirect') !== '/') {
|
|
|
120 |
return '/'.trim($this->option('redirect'), '/');
|
|
|
121 |
}
|
|
|
122 |
|
|
|
123 |
return $this->option('redirect');
|
|
|
124 |
}
|
|
|
125 |
|
|
|
126 |
/**
|
|
|
127 |
* Prerender the specified view so that it can be rendered even before loading Composer.
|
|
|
128 |
*
|
|
|
129 |
* @return string
|
|
|
130 |
*/
|
|
|
131 |
protected function prerenderView()
|
|
|
132 |
{
|
|
|
133 |
(new RegisterErrorViewPaths)();
|
|
|
134 |
|
|
|
135 |
return view($this->option('render'), [
|
|
|
136 |
'retryAfter' => $this->option('retry'),
|
|
|
137 |
])->render();
|
|
|
138 |
}
|
|
|
139 |
|
|
|
140 |
/**
|
|
|
141 |
* Get the number of seconds the client should wait before retrying their request.
|
|
|
142 |
*
|
|
|
143 |
* @return int|null
|
|
|
144 |
*/
|
|
|
145 |
protected function getRetryTime()
|
|
|
146 |
{
|
|
|
147 |
$retry = $this->option('retry');
|
|
|
148 |
|
|
|
149 |
return is_numeric($retry) && $retry > 0 ? (int) $retry : null;
|
|
|
150 |
}
|
|
|
151 |
}
|