1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the O2System PHP Framework package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
* |
8
|
|
|
* @author Steeve Andrian Salim |
9
|
|
|
* @copyright Copyright (c) Steeve Andrian Salim |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
// ------------------------------------------------------------------------ |
13
|
|
|
|
14
|
|
|
namespace O2System\Reactor\Cli\Commanders; |
15
|
|
|
|
16
|
|
|
// ------------------------------------------------------------------------ |
17
|
|
|
|
18
|
|
|
use O2System\Cache\Item; |
19
|
|
|
use O2System\Kernel\Cli\Commander; |
20
|
|
|
use O2System\Kernel\Cli\Writers\Format; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Class Maintenance |
24
|
|
|
* |
25
|
|
|
* @package O2System\Reactor\Cli\Commanders |
26
|
|
|
*/ |
27
|
|
|
class Maintenance extends Commander |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* Maintenance::$commandVersion |
31
|
|
|
* |
32
|
|
|
* Command version. |
33
|
|
|
* |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
protected $commandVersion = '1.0.0'; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Maintenance::$commandDescription |
40
|
|
|
* |
41
|
|
|
* Command description. |
42
|
|
|
* |
43
|
|
|
* @var string |
44
|
|
|
*/ |
45
|
|
|
protected $commandDescription = 'CLI_MAINTENANCE_DESC'; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Maintenance::$commandOptions |
49
|
|
|
* |
50
|
|
|
* Command options. |
51
|
|
|
* |
52
|
|
|
* @var array |
53
|
|
|
*/ |
54
|
|
|
protected $commandOptions = [ |
55
|
|
|
'mode' => [ |
56
|
|
|
'description' => 'CLI_MAINTENANCE_MODE_HELP', |
57
|
|
|
'required' => true, |
58
|
|
|
], |
59
|
|
|
'switch' => [ |
60
|
|
|
'description' => 'CLI_MAINTENANCE_SWITCH_HELP', |
61
|
|
|
'required' => true, |
62
|
|
|
], |
63
|
|
|
'title' => [ |
64
|
|
|
'description' => 'CLI_MAINTENANCE_TITLE_HELP', |
65
|
|
|
'required' => false, |
66
|
|
|
], |
67
|
|
|
'message' => [ |
68
|
|
|
'description' => 'CLI_MAINTENANCE_MESSAGE_HELP', |
69
|
|
|
'required' => false, |
70
|
|
|
], |
71
|
|
|
]; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Maintenance::$optionSwitch |
75
|
|
|
* |
76
|
|
|
* Maintenance switch. |
77
|
|
|
* |
78
|
|
|
* @var string |
79
|
|
|
*/ |
80
|
|
|
protected $optionSwitch; |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Maintenance::$optionMode |
84
|
|
|
* |
85
|
|
|
* Maintenance mode. |
86
|
|
|
* |
87
|
|
|
* @var string |
88
|
|
|
*/ |
89
|
|
|
protected $optionMode; |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Maintenance::$optionLifetime |
93
|
|
|
* |
94
|
|
|
* Maintenance mode. |
95
|
|
|
* |
96
|
|
|
* @var string |
97
|
|
|
*/ |
98
|
|
|
protected $optionLifetime; |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Maintenance::$optionTitle |
102
|
|
|
* |
103
|
|
|
* Maintenance title. |
104
|
|
|
* |
105
|
|
|
* @var string |
106
|
|
|
*/ |
107
|
|
|
protected $optionTitle; |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Maintenance::$optionMessage |
111
|
|
|
* |
112
|
|
|
* Maintenance message. |
113
|
|
|
* |
114
|
|
|
* @var string |
115
|
|
|
*/ |
116
|
|
|
protected $optionMessage; |
117
|
|
|
|
118
|
|
|
// ------------------------------------------------------------------------ |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Maintenance::optionSwitch |
122
|
|
|
* |
123
|
|
|
* @param string $switch |
124
|
|
|
*/ |
125
|
|
|
public function optionSwitch($switch) |
126
|
|
|
{ |
127
|
|
|
$switch = strtoupper($switch); |
128
|
|
|
|
129
|
|
|
if (in_array($switch, ['ON', 'OFF'])) { |
130
|
|
|
$this->optionSwitch = $switch; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
// ------------------------------------------------------------------------ |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Maintenance::optionMode |
138
|
|
|
* |
139
|
|
|
* @param string $mode |
140
|
|
|
*/ |
141
|
|
|
public function optionMode($mode) |
142
|
|
|
{ |
143
|
|
|
$this->optionMode = $mode; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
// ------------------------------------------------------------------------ |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Maintenance::optionLifetime |
150
|
|
|
* |
151
|
|
|
* @param int $lifetime |
152
|
|
|
*/ |
153
|
|
|
public function optionLifetime($lifetime) |
154
|
|
|
{ |
155
|
|
|
$this->optionLifetime = (int)$lifetime; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
// ------------------------------------------------------------------------ |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Maintenance::optionTitle |
162
|
|
|
* |
163
|
|
|
* @param string $title |
164
|
|
|
*/ |
165
|
|
|
public function optionTitle($title) |
166
|
|
|
{ |
167
|
|
|
$this->optionTitle = trim($title); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
// ------------------------------------------------------------------------ |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Maintenance::optionMessage |
174
|
|
|
* |
175
|
|
|
* @param string $message |
176
|
|
|
*/ |
177
|
|
|
public function optionMessage($message) |
178
|
|
|
{ |
179
|
|
|
$this->optionMessage = $message; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
// ------------------------------------------------------------------------ |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Maintenance::execute |
186
|
|
|
* |
187
|
|
|
* @throws \ReflectionException |
188
|
|
|
*/ |
189
|
|
|
public function execute() |
190
|
|
|
{ |
191
|
|
|
$options = input()->get(); |
192
|
|
|
|
193
|
|
|
if (empty($options)) { |
194
|
|
|
$_GET[ 'switch' ] = 'ON'; |
195
|
|
|
$_GET[ 'mode' ] = 'default'; |
196
|
|
|
$_GET[ 'lifetime' ] = 300; |
197
|
|
|
$_GET[ 'title' ] = language()->getLine(strtoupper('CLI_MAINTENANCE_TITLE')); |
198
|
|
|
$_GET[ 'message' ] = language()->getLine(strtoupper('CLI_MAINTENANCE_MESSAGE')); |
199
|
|
|
} else { |
200
|
|
|
$_GET[ 'mode' ] = 'default'; |
201
|
|
|
$_GET[ 'lifetime' ] = 300; |
202
|
|
|
$_GET[ 'title' ] = language()->getLine(strtoupper('CLI_MAINTENANCE_TITLE')); |
203
|
|
|
$_GET[ 'message' ] = language()->getLine(strtoupper('CLI_MAINTENANCE_MESSAGE')); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
$this->__callOptions(); |
207
|
|
|
|
208
|
|
|
if ($this->optionSwitch === 'ON') { |
209
|
|
|
if (cache()->hasItem('maintenance')) { |
210
|
|
|
|
211
|
|
|
$maintenanceInfo = cache()->getItem('maintenance')->get(); |
212
|
|
|
output()->write( |
|
|
|
|
213
|
|
|
(new Format()) |
214
|
|
|
->setContextualClass(Format::DANGER) |
215
|
|
|
->setString(language()->getLine('CLI_MAINTENANCE_ALREADY_STARTED', [ |
216
|
|
|
$maintenanceInfo[ 'mode' ], |
217
|
|
|
$maintenanceInfo[ 'datetime' ], |
218
|
|
|
date('r', strtotime($maintenanceInfo[ 'datetime' ]) + $maintenanceInfo[ 'lifetime' ]), |
219
|
|
|
$maintenanceInfo[ 'title' ], |
220
|
|
|
$maintenanceInfo[ 'message' ], |
221
|
|
|
])) |
222
|
|
|
->setNewLinesAfter(1) |
223
|
|
|
); |
224
|
|
|
} else { |
225
|
|
|
output()->write( |
226
|
|
|
(new Format()) |
227
|
|
|
->setContextualClass(Format::WARNING) |
228
|
|
|
->setString(language()->getLine('CLI_MAINTENANCE_STARTED', [ |
229
|
|
|
$datetime = date('r'), |
230
|
|
|
$this->optionLifetime, |
231
|
|
|
$this->optionMode, |
232
|
|
|
$this->optionTitle, |
233
|
|
|
$this->optionMessage, |
234
|
|
|
])) |
235
|
|
|
->setNewLinesAfter(1) |
236
|
|
|
); |
237
|
|
|
|
238
|
|
|
cache()->save(new Item('maintenance', [ |
239
|
|
|
'datetime' => $datetime, |
240
|
|
|
'lifetime' => $this->optionLifetime, |
241
|
|
|
'mode' => $this->optionMode, |
242
|
|
|
'title' => $this->optionTitle, |
243
|
|
|
'message' => $this->optionMessage, |
244
|
|
|
], $this->optionLifetime)); |
|
|
|
|
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
} elseif ($this->optionSwitch === 'OFF') { |
248
|
|
|
if (cache()->hasItem('maintenance')) { |
249
|
|
|
output()->write( |
250
|
|
|
(new Format()) |
251
|
|
|
->setContextualClass(Format::DANGER) |
252
|
|
|
->setString(language()->getLine('CLI_MAINTENANCE_STOPPED', [ |
253
|
|
|
$this->optionMode, |
254
|
|
|
date('r'), |
255
|
|
|
])) |
256
|
|
|
->setNewLinesAfter(1) |
257
|
|
|
); |
258
|
|
|
|
259
|
|
|
cache()->deleteItem('maintenance'); |
260
|
|
|
} else { |
261
|
|
|
output()->write( |
262
|
|
|
(new Format()) |
263
|
|
|
->setContextualClass(Format::DANGER) |
264
|
|
|
->setString(language()->getLine('CLI_MAINTENANCE_INACTIVE')) |
265
|
|
|
->setNewLinesAfter(1) |
266
|
|
|
); |
267
|
|
|
} |
268
|
|
|
} |
269
|
|
|
} |
270
|
|
|
} |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.