1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DigiFactory\PartialDown\Commands; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Illuminate\Console\Command; |
7
|
|
|
use Illuminate\Support\InteractsWithTime; |
8
|
|
|
use Illuminate\Support\Str; |
9
|
|
|
|
10
|
|
|
class PartialDown extends Command |
11
|
|
|
{ |
12
|
|
|
use InteractsWithTime; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* The console command signature. |
16
|
|
|
* |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
protected $signature = 'partial-down {part} |
20
|
|
|
{--message= : The message for the maintenance mode} |
21
|
|
|
{--retry= : The number of seconds after which the request may be retried} |
22
|
|
|
{--allow=* : IP or networks allowed to access the application while in maintenance mode}'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* The console command description. |
26
|
|
|
* |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $description = 'Put specific part of the application into maintenance mode'; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Execute the console command. |
33
|
|
|
* |
34
|
|
|
* @return int |
35
|
|
|
*/ |
36
|
|
View Code Duplication |
public function handle() |
|
|
|
|
37
|
|
|
{ |
38
|
|
|
$part = Str::slug($this->argument('part')); |
|
|
|
|
39
|
|
|
|
40
|
|
|
$lockFilename = 'framework/partial-down-'.$part; |
41
|
|
|
|
42
|
|
|
try { |
43
|
|
|
if (file_exists(storage_path($lockFilename))) { |
44
|
|
|
$this->comment("This part [{$part}] of the application is already down."); |
45
|
|
|
|
46
|
|
|
return true; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
file_put_contents(storage_path($lockFilename), |
50
|
|
|
json_encode($this->getDownFilePayload(), |
51
|
|
|
JSON_PRETTY_PRINT)); |
52
|
|
|
|
53
|
|
|
$this->comment("This part [{$part}] of the application is now in maintenance mode."); |
54
|
|
|
} catch (Exception $e) { |
55
|
|
|
$this->error("Failed to put this part [{$part}] of the application in maintenance mode."); |
56
|
|
|
|
57
|
|
|
$this->error($e->getMessage()); |
58
|
|
|
|
59
|
|
|
return 1; |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Get the payload to be placed in the "down" file. |
65
|
|
|
* |
66
|
|
|
* @return array |
67
|
|
|
*/ |
68
|
|
|
protected function getDownFilePayload() |
69
|
|
|
{ |
70
|
|
|
return [ |
71
|
|
|
'time' => $this->currentTime(), |
72
|
|
|
'message' => $this->option('message'), |
73
|
|
|
'retry' => $this->getRetryTime(), |
74
|
|
|
'allowed' => $this->option('allow'), |
75
|
|
|
]; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Get the number of seconds the client should wait before retrying their request. |
80
|
|
|
* |
81
|
|
|
* @return int|null |
82
|
|
|
*/ |
83
|
|
|
protected function getRetryTime() |
84
|
|
|
{ |
85
|
|
|
$retry = $this->option('retry'); |
86
|
|
|
|
87
|
|
|
return is_numeric($retry) && $retry > 0 ? (int) $retry : null; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.