|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace dominus77\maintenance\commands; |
|
4
|
|
|
|
|
5
|
|
|
use dominus77\maintenance\BackendMaintenance; |
|
6
|
|
|
use yii\helpers\Console; |
|
7
|
|
|
use yii\console\Controller; |
|
8
|
|
|
use yii\base\Module; |
|
9
|
|
|
use dominus77\maintenance\Maintenance; |
|
10
|
|
|
use dominus77\maintenance\models\FileStateForm; |
|
11
|
|
|
use dominus77\maintenance\models\SubscribeForm; |
|
12
|
|
|
use dominus77\maintenance\interfaces\StateInterface; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Maintenance mode |
|
16
|
|
|
* @package dominus77\maintenance\commands |
|
17
|
|
|
* |
|
18
|
|
|
* @property FileStateForm $defaultValue |
|
19
|
|
|
* @property FileStateForm $fileStateForm |
|
20
|
|
|
*/ |
|
21
|
|
|
class MaintenanceController extends Controller |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* Date |
|
25
|
|
|
* @var string |
|
26
|
|
|
*/ |
|
27
|
|
|
public $date; |
|
28
|
|
|
/** |
|
29
|
|
|
* Title |
|
30
|
|
|
* @var string |
|
31
|
|
|
*/ |
|
32
|
|
|
public $title; |
|
33
|
|
|
/** |
|
34
|
|
|
* Content |
|
35
|
|
|
* @var string |
|
36
|
|
|
*/ |
|
37
|
|
|
public $content; |
|
38
|
|
|
/** |
|
39
|
|
|
* @var string |
|
40
|
|
|
*/ |
|
41
|
|
|
public $subscribe; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var string |
|
45
|
|
|
*/ |
|
46
|
|
|
public $timer; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @var StateInterface |
|
50
|
|
|
*/ |
|
51
|
|
|
protected $state; |
|
52
|
|
|
/** |
|
53
|
|
|
* @var mixed string |
|
54
|
|
|
*/ |
|
55
|
|
|
protected $exampleData; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* MaintenanceController constructor. |
|
59
|
|
|
* @param string $id |
|
60
|
|
|
* @param Module $module |
|
61
|
|
|
* @param StateInterface $state |
|
62
|
|
|
* @param array $config |
|
63
|
|
|
*/ |
|
64
|
|
|
public function __construct($id, Module $module, StateInterface $state, array $config = []) |
|
65
|
|
|
{ |
|
66
|
|
|
$this->state = $state; |
|
67
|
|
|
$this->exampleData = $this->exampleDateFormat(); |
|
68
|
|
|
parent::__construct($id, $module, $config); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Options |
|
74
|
|
|
* @param string $actionId |
|
75
|
|
|
* @return array|string[] |
|
76
|
|
|
*/ |
|
77
|
|
|
public function options($actionId) |
|
78
|
|
|
{ |
|
79
|
|
|
return [ |
|
80
|
|
|
'date', |
|
81
|
|
|
'title', |
|
82
|
|
|
'content', |
|
83
|
|
|
'subscribe', |
|
84
|
|
|
'timer' |
|
85
|
|
|
]; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Aliases |
|
90
|
|
|
* @return array |
|
91
|
|
|
*/ |
|
92
|
|
|
public function optionAliases() |
|
93
|
|
|
{ |
|
94
|
|
|
return [ |
|
95
|
|
|
'd' => 'date', |
|
96
|
|
|
't' => 'title', |
|
97
|
|
|
'c' => 'content', |
|
98
|
|
|
's' => 'subscribe', |
|
99
|
|
|
'tm' => 'timer' |
|
100
|
|
|
]; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Maintenance status or commands |
|
105
|
|
|
*/ |
|
106
|
|
|
public function actionIndex() |
|
107
|
|
|
{ |
|
108
|
|
|
$stateForm = new FileStateForm(); |
|
109
|
|
|
$subscribeForm = new SubscribeForm(); |
|
110
|
|
|
|
|
111
|
|
|
$enabledMode = $this->ansiFormat(Maintenance::t('app', 'ENABLED'), Console::FG_RED); |
|
112
|
|
|
$disabledMode = $this->ansiFormat(Maintenance::t('app', 'DISABLED'), Console::FG_GREEN); |
|
113
|
|
|
|
|
114
|
|
|
$enabled = $this->ansiFormat(Maintenance::t('app', 'ENABLED'), Console::FG_GREEN); |
|
115
|
|
|
$disabled = $this->ansiFormat(Maintenance::t('app', 'DISABLED'), Console::FG_RED); |
|
116
|
|
|
|
|
117
|
|
|
if ($this->state->isEnabled()) { |
|
118
|
|
|
$datetime = $stateForm->getDateTime(); |
|
119
|
|
|
|
|
120
|
|
|
$message = Maintenance::t('app', 'Maintenance Mode has been {:status}', [ |
|
121
|
|
|
':status' => $enabledMode |
|
122
|
|
|
]); |
|
123
|
|
|
$this->stdout($message . PHP_EOL); |
|
124
|
|
|
|
|
125
|
|
|
$message = Maintenance::t('app', 'on until {:datetime}', [ |
|
126
|
|
|
':datetime' => $datetime |
|
127
|
|
|
]); |
|
128
|
|
|
$this->stdout($message . PHP_EOL); |
|
129
|
|
|
|
|
130
|
|
|
$message = Maintenance::t('app', '{n, plural, =0{No followers} =1{Total one follower} other{Total # followers}}.', [ |
|
131
|
|
|
'n' => count($subscribeForm->getEmails()) |
|
132
|
|
|
]); |
|
133
|
|
|
$this->stdout($message . PHP_EOL); |
|
134
|
|
|
$this->stdout(PHP_EOL); |
|
135
|
|
|
|
|
136
|
|
|
$status = $stateForm->isTimer() ? $enabled : $disabled; |
|
137
|
|
|
$message = Maintenance::t('app', 'Count Down: {:status}', [ |
|
138
|
|
|
':status' => $status |
|
139
|
|
|
]); |
|
140
|
|
|
$this->stdout($message . PHP_EOL); |
|
141
|
|
|
|
|
142
|
|
|
$status = $stateForm->isSubscribe() ? $enabled : $disabled; |
|
143
|
|
|
$message = Maintenance::t('app', 'Subscription form: {:status}', [ |
|
144
|
|
|
':status' => $status |
|
145
|
|
|
]); |
|
146
|
|
|
$this->stdout($message . PHP_EOL); |
|
147
|
|
|
|
|
148
|
|
|
$message = Maintenance::t('app', 'You can update the maintenance mode.'); |
|
149
|
|
|
$this->stdout(PHP_EOL . $message . PHP_EOL); |
|
150
|
|
|
|
|
151
|
|
|
$message = Maintenance::t('app', "Use:\nphp yii maintenance/update --option='value'\n"); |
|
152
|
|
|
$this->stdout($message . PHP_EOL); |
|
153
|
|
|
$this->getOptionsTable(); |
|
154
|
|
|
} else { |
|
155
|
|
|
$message = Maintenance::t('app', 'Maintenance Mode has been {:status}', [ |
|
156
|
|
|
':status' => $disabledMode |
|
157
|
|
|
]); |
|
158
|
|
|
$this->stdout($message . PHP_EOL); |
|
159
|
|
|
|
|
160
|
|
|
$this->stdout("\nMaintenance Mode enable.\n"); |
|
161
|
|
|
$this->stdout("Use:\nphp yii maintenance/enable\nto enable maintenance mode.\n"); |
|
162
|
|
|
|
|
163
|
|
|
$this->stdout("\nAlso maintenance Mode enable set to date and time.\n"); |
|
164
|
|
|
$this->stdout("Use:\nphp yii maintenance/enable --date=\"$this->exampleData\"\nto enable maintenance mode to $this->exampleData.\n"); |
|
165
|
|
|
$this->stdout("Note:\nThis date and time not disable maintenance mode\n"); |
|
166
|
|
|
|
|
167
|
|
|
$this->stdout("\nMaintenance Mode update date and time.\n"); |
|
168
|
|
|
$this->stdout("Use:\nphp yii maintenance/update --date=\"$this->exampleData\"\nto update maintenance mode to $this->exampleData.\n"); |
|
169
|
|
|
$this->stdout("Note:\nThis date and time not disable maintenance mode\n"); |
|
170
|
|
|
|
|
171
|
|
|
$this->stdout("\nSubscribers to whom messages will be sent after turning off the mode maintenance\n"); |
|
172
|
|
|
$this->stdout("Use:\nphp yii maintenance/followers\nto show followers.\n"); |
|
173
|
|
|
|
|
174
|
|
|
$this->stdout("\nMaintenance Mode disable.\n"); |
|
175
|
|
|
$this->stdout("Use:\nphp yii maintenance/disable\nto disable maintenance mode.\n"); |
|
176
|
|
|
} |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* Enable maintenance mode |
|
181
|
|
|
*/ |
|
182
|
|
|
public function actionEnable() |
|
183
|
|
|
{ |
|
184
|
|
|
$stateForm = new FileStateForm(); |
|
185
|
|
|
if (!$this->state->isEnabled()) { |
|
186
|
|
|
$stateForm->mode = Maintenance::STATUS_CODE_MAINTENANCE; |
|
187
|
|
|
$stateForm = $this->setFileStateForm($stateForm); |
|
188
|
|
|
$this->setDefaultValue($stateForm); |
|
189
|
|
|
if ($stateForm->validate()) { |
|
190
|
|
|
$stateForm->save(); |
|
191
|
|
|
} |
|
192
|
|
|
} |
|
193
|
|
|
$datetime = $stateForm->getDateTime(); |
|
194
|
|
|
$enabled = $this->ansiFormat('ENABLED', Console::FG_RED); |
|
195
|
|
|
$this->stdout("Maintenance Mode has been $enabled\n"); |
|
196
|
|
|
$this->stdout("on until $datetime\n"); |
|
197
|
|
|
|
|
198
|
|
|
$this->stdout("\nMaintenance Mode update date and time.\n"); |
|
199
|
|
|
$this->stdout("Use:\nphp yii maintenance/update --date=\"$this->exampleData\"\nto update maintenance mode to $this->exampleData.\n"); |
|
200
|
|
|
$this->stdout("Note:\nThis date and time not disable maintenance mode\n"); |
|
201
|
|
|
|
|
202
|
|
|
$this->stdout("\nSubscribers to whom messages will be sent after turning off the mode maintenance\n"); |
|
203
|
|
|
$this->stdout("Use:\nphp yii maintenance/followers\nto show followers.\n"); |
|
204
|
|
|
|
|
205
|
|
|
$this->stdout("\nMaintenance Mode disable.\n"); |
|
206
|
|
|
$this->stdout("Use:\nphp yii maintenance/disable\nto disable maintenance mode.\n"); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* Update date and time maintenance mode |
|
211
|
|
|
*/ |
|
212
|
|
|
public function actionUpdate() |
|
213
|
|
|
{ |
|
214
|
|
|
$stateForm = new FileStateForm(); |
|
215
|
|
|
if ($this->state->isEnabled()) { |
|
216
|
|
|
$stateForm->mode = Maintenance::STATUS_CODE_MAINTENANCE; |
|
217
|
|
|
$stateForm = $this->setFileStateForm($stateForm); |
|
218
|
|
|
if ($stateForm->validate()) { |
|
219
|
|
|
$stateForm->save(); |
|
220
|
|
|
|
|
221
|
|
|
$updated = $this->ansiFormat(Maintenance::t('app', 'UPDATED'), Console::FG_GREEN); |
|
222
|
|
|
$message = Maintenance::t('app', 'Maintenance Mode has been {:status}', [ |
|
223
|
|
|
':status' => $updated |
|
224
|
|
|
]); |
|
225
|
|
|
$this->stdout($message . PHP_EOL); |
|
226
|
|
|
|
|
227
|
|
|
} else { |
|
228
|
|
|
$this->stdout("Not specified what to update\n"); |
|
229
|
|
|
$this->stdout("\nUse:\n"); |
|
230
|
|
|
$this->stdout("\nphp yii maintenance/update --date=\"$this->exampleData\"\nto update maintenance mode to $this->exampleData.\n"); |
|
231
|
|
|
$this->stdout("\nphp yii maintenance/update --title=\"Maintenance\"\nto update maintenance mode title.\n"); |
|
232
|
|
|
$this->stdout("\nphp yii maintenance/update --content=\"Maintenance\"\nto update maintenance mode text content.\n"); |
|
233
|
|
|
$this->stdout("\nphp yii maintenance/update --subscribe=true\nto enable subscribe form for maintenance mode.\n"); |
|
234
|
|
|
$this->stdout("\nphp yii maintenance/update --timer=true\nto enable count down timer form for maintenance mode.\n"); |
|
235
|
|
|
} |
|
236
|
|
|
} else { |
|
237
|
|
|
$this->stdout("Maintenance Mode not enable!\n"); |
|
238
|
|
|
|
|
239
|
|
|
$this->stdout("Use:\nphp yii maintenance/enable\nto enable maintenance mode.\n"); |
|
240
|
|
|
|
|
241
|
|
|
$this->stdout("\nAlso maintenance Mode enable set to date and time.\n"); |
|
242
|
|
|
$this->stdout("Use:\nphp yii maintenance/enable --date=\"$this->exampleData\"\nto enable maintenance mode to $this->exampleData.\n"); |
|
243
|
|
|
$this->stdout("Note:\nThis date and time not disable maintenance mode\n"); |
|
244
|
|
|
} |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
/** |
|
248
|
|
|
* Disable maintenance mode |
|
249
|
|
|
*/ |
|
250
|
|
|
public function actionDisable() |
|
251
|
|
|
{ |
|
252
|
|
|
$stateForm = new FileStateForm(); |
|
253
|
|
|
$this->stdout("Maintenance Mode has been disabled.\n"); |
|
254
|
|
|
if ($stateForm->isEnabled()) { |
|
255
|
|
|
$stateForm->disable(); |
|
256
|
|
|
$subscribeForm = new SubscribeForm(); |
|
257
|
|
|
$result = $subscribeForm->send(); |
|
258
|
|
|
if ($result || $result === 0) { |
|
259
|
|
|
$this->stdout("Notified ($result) subscribers.\n"); |
|
260
|
|
|
} |
|
261
|
|
|
} |
|
262
|
|
|
|
|
263
|
|
|
$this->stdout("\nUse:\nphp yii maintenance/enable\nto enable maintenance mode.\n"); |
|
264
|
|
|
|
|
265
|
|
|
$this->stdout("\nAlso maintenance Mode enable set to date and time.\n"); |
|
266
|
|
|
$this->stdout("Use:\nphp yii maintenance/enable --date=\"$this->exampleData\"\nto enable maintenance mode to $this->exampleData.\n"); |
|
267
|
|
|
$this->stdout("Note:\nThis date and time not disable maintenance mode\n"); |
|
268
|
|
|
} |
|
269
|
|
|
|
|
270
|
|
|
/** |
|
271
|
|
|
* Show subscribers to whom messages |
|
272
|
|
|
*/ |
|
273
|
|
|
public function actionFollowers() |
|
274
|
|
|
{ |
|
275
|
|
|
$stateForm = new FileStateForm(); |
|
276
|
|
|
$subscribeForm = new SubscribeForm(); |
|
277
|
|
|
if (!$stateForm->isEnabled()) { |
|
278
|
|
|
$this->stdout("Maintenance Mode not enable!\n"); |
|
279
|
|
|
|
|
280
|
|
|
$this->stdout("\nUse:\nphp yii maintenance/enable\nto enable maintenance mode.\n"); |
|
281
|
|
|
|
|
282
|
|
|
$this->stdout("\nAlso maintenance Mode enable set to date and time.\n"); |
|
283
|
|
|
$this->stdout("Use:\nphp yii maintenance/enable --date=\"$this->exampleData\"\nto enable maintenance mode to $this->exampleData.\n"); |
|
284
|
|
|
$this->stdout("Note:\nThis date and time not disable maintenance mode\n"); |
|
285
|
|
|
} else if ($emails = $subscribeForm->getEmails()) { |
|
286
|
|
|
$this->stdout('Total (' . count($emails) . ') followers:' . PHP_EOL); |
|
287
|
|
|
foreach ($emails as $email) { |
|
288
|
|
|
$this->stdout($email . PHP_EOL); |
|
289
|
|
|
} |
|
290
|
|
|
} else { |
|
291
|
|
|
$this->stdout("No followers\n"); |
|
292
|
|
|
} |
|
293
|
|
|
} |
|
294
|
|
|
|
|
295
|
|
|
public function actionOptions() |
|
296
|
|
|
{ |
|
297
|
|
|
$this->getOptionsTable(); |
|
298
|
|
|
} |
|
299
|
|
|
|
|
300
|
|
|
/** |
|
301
|
|
|
* Options and aliases |
|
302
|
|
|
*/ |
|
303
|
|
|
public function getOptionsTable() |
|
304
|
|
|
{ |
|
305
|
|
|
$this->stdout('---------------------------------------------' . PHP_EOL); |
|
306
|
|
|
$this->stdout('| Option | Alias | Value |' . PHP_EOL); |
|
307
|
|
|
$this->stdout('|=============|=======|=====================|' . PHP_EOL); |
|
308
|
|
|
$this->stdout('| --date | -d | ' . $this->exampleDateFormat() . ' |' . PHP_EOL); |
|
309
|
|
|
$this->stdout('| --title | -t | string |' . PHP_EOL); |
|
310
|
|
|
$this->stdout('| --content | -c | string |' . PHP_EOL); |
|
311
|
|
|
$this->stdout('| --subscribe | -s | true/false |' . PHP_EOL); |
|
312
|
|
|
$this->stdout('| --timer | -tm | true/false |' . PHP_EOL); |
|
313
|
|
|
$this->stdout('---------------------------------------------' . PHP_EOL); |
|
314
|
|
|
} |
|
315
|
|
|
|
|
316
|
|
|
/** |
|
317
|
|
|
* @param FileStateForm $stateForm |
|
318
|
|
|
* @return FileStateForm |
|
319
|
|
|
*/ |
|
320
|
|
|
protected function setFileStateForm(FileStateForm $stateForm) |
|
321
|
|
|
{ |
|
322
|
|
|
if ($this->date) { |
|
323
|
|
|
$stateForm->date = $this->date; |
|
324
|
|
|
} |
|
325
|
|
|
if ($this->title) { |
|
326
|
|
|
$stateForm->title = $this->title; |
|
327
|
|
|
} |
|
328
|
|
|
if ($this->content) { |
|
329
|
|
|
$stateForm->text = $this->content; |
|
330
|
|
|
} |
|
331
|
|
|
if ($this->subscribe) { |
|
332
|
|
|
$stateForm->subscribe = $this->subscribe === 'true'; |
|
333
|
|
|
} |
|
334
|
|
|
if ($this->timer) { |
|
335
|
|
|
$stateForm->countDown = $this->timer === 'true'; |
|
336
|
|
|
} |
|
337
|
|
|
return $stateForm; |
|
338
|
|
|
} |
|
339
|
|
|
|
|
340
|
|
|
/** |
|
341
|
|
|
* @param FileStateForm $stateForm |
|
342
|
|
|
*/ |
|
343
|
|
|
protected function setDefaultValue(FileStateForm $stateForm) |
|
344
|
|
|
{ |
|
345
|
|
|
if ($stateForm->title === null && $this->title === null) { |
|
346
|
|
|
$stateForm->title = BackendMaintenance::t('app', $stateForm->getDefaultTitle()); |
|
347
|
|
|
} |
|
348
|
|
|
if ($stateForm->text === null && $this->content === null) { |
|
349
|
|
|
$stateForm->text = BackendMaintenance::t('app', $stateForm->getDefaultText()); |
|
350
|
|
|
} |
|
351
|
|
|
} |
|
352
|
|
|
|
|
353
|
|
|
/** |
|
354
|
|
|
* Example format date time |
|
355
|
|
|
* @return mixed |
|
356
|
|
|
*/ |
|
357
|
|
|
protected function exampleDateFormat() |
|
358
|
|
|
{ |
|
359
|
|
|
return date($this->state->getDateFormat()); |
|
360
|
|
|
} |
|
361
|
|
|
} |
|
362
|
|
|
|
|
363
|
|
|
|