1
|
|
|
<?php |
2
|
|
|
namespace App\Shell; |
3
|
|
|
|
4
|
|
|
use Cake\Cache\Cache; |
5
|
|
|
use Cake\Console\ConsoleOptionParser; |
6
|
|
|
use Cake\Console\Shell; |
7
|
|
|
use Cake\Utility\Inflector; |
8
|
|
|
|
9
|
|
|
class MaintenanceShell extends Shell |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* The setting key to modify. |
13
|
|
|
* |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
protected $settingKey = 'Site.maintenance'; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Initialize method. |
20
|
|
|
* |
21
|
|
|
* @return void |
22
|
|
|
*/ |
23
|
|
|
public function initialize() |
24
|
|
|
{ |
25
|
|
|
parent::initialize(); |
26
|
|
|
$this->loadModel('Settings'); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Display help for this console. |
31
|
|
|
* |
32
|
|
|
* @return ConsoleOptionParser |
33
|
|
|
*/ |
34
|
|
|
public function getOptionParser() |
35
|
|
|
{ |
36
|
|
|
$parser = parent::getOptionParser(); |
37
|
|
|
$parser->description( |
|
|
|
|
38
|
|
|
'This shell is used with the maintenance mode in the application.' |
39
|
|
|
)->addSubcommand('down', [ |
40
|
|
|
'help' => 'Put the website in maintenance mode.' |
41
|
|
|
])->addSubcommand('up', [ |
42
|
|
|
'help' => 'Remove the website from the maintenance mode.' |
43
|
|
|
]); |
44
|
|
|
|
45
|
|
|
return $parser; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Put the application in maintenance mode. |
50
|
|
|
* |
51
|
|
|
* @return bool |
52
|
|
|
*/ |
53
|
|
|
public function down() |
54
|
|
|
{ |
55
|
|
|
return $this->handleMaintenance('down', '1'); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Remove the application from the maintenance mode. |
60
|
|
|
* |
61
|
|
|
* @return bool |
62
|
|
|
*/ |
63
|
|
|
public function up() |
64
|
|
|
{ |
65
|
|
|
return $this->handleMaintenance('up', '0'); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Handle the maintenance. |
70
|
|
|
* |
71
|
|
|
* @param string $type The type of the maintenance; up, down. |
72
|
|
|
* @param string $value The value of the setting key. |
73
|
|
|
* |
74
|
|
|
* @return bool |
75
|
|
|
*/ |
76
|
|
|
protected function handleMaintenance($type, $value) |
77
|
|
|
{ |
78
|
|
|
$setting = $this->Settings->find() |
|
|
|
|
79
|
|
|
->where(['name' => $this->settingKey]) |
80
|
|
|
->first(); |
81
|
|
|
|
82
|
|
|
if ($this->checkKey($setting) === false) { |
83
|
|
|
return false; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$data = [ |
87
|
|
|
'value_int' => null, |
88
|
|
|
'value_str' => null, |
89
|
|
|
'value_bool' => $value |
90
|
|
|
]; |
91
|
|
|
|
92
|
|
|
$this->Settings->patchEntity($setting, $data); |
|
|
|
|
93
|
|
|
|
94
|
|
|
if ($this->Settings->save($setting)) { |
|
|
|
|
95
|
|
|
Cache::clear(false, 'database'); |
96
|
|
|
$this->out('<success>The</success> "<info>maintenance ' . $type . '</info>" <success>command has been executed successfully !</success>', 2); |
97
|
|
|
|
98
|
|
|
return true; |
99
|
|
|
} else { |
100
|
|
|
$this->out('<error>The</error> "<info>maintenance ' . $type . '</info>" <error>has failed while saving the setting key !</error>', 2); |
101
|
|
|
|
102
|
|
|
return false; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Check if the setting key exist in the database. |
108
|
|
|
* |
109
|
|
|
* @param null|App\Model\Entity\Setting $setting The setting to check. |
110
|
|
|
* |
111
|
|
|
* @return bool |
112
|
|
|
*/ |
113
|
|
|
protected function checkKey($setting) |
114
|
|
|
{ |
115
|
|
|
if (is_null($setting)) { |
116
|
|
|
$msg = sprintf( |
117
|
|
|
'The setting key "%s" can not be found in the database, ' . |
118
|
|
|
'please add it to use this command shell.', |
119
|
|
|
$this->settingKey |
120
|
|
|
); |
121
|
|
|
$this->error($msg); |
|
|
|
|
122
|
|
|
|
123
|
|
|
return false; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return true; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.