]> git.mxchange.org Git - friendica.git/blob - src/Console/Maintenance.php
The value is used twice, so use a variable
[friendica.git] / src / Console / Maintenance.php
1 <?php
2
3 namespace Friendica\Console;
4
5 use Friendica\App;
6 use Friendica\Core\Config\Configuration;
7
8 /**
9  * @brief Sets maintenance mode for this node
10  *
11  * @author Hypolite Petovan <hypolite@mrpetovan.com>
12  */
13 class Maintenance extends \Asika\SimpleConsole\Console
14 {
15         protected $helpOptions = ['h', 'help', '?'];
16
17         /**
18          * @var App\Mode
19          */
20         private $appMode;
21         /**
22          * @var Configuration
23          */
24         private $config;
25
26         protected function getHelp()
27         {
28                 $help = <<<HELP
29 console maintenance - Sets maintenance mode for this node
30 Usage
31         bin/console maintenance <enable> [<reason>] [-h|--help|-?] [-v]
32
33 Description
34         <enable> cen be either 0 or 1 to disabled or enable the maintenance mode on this node.
35
36         <reason> is a quote-enclosed string with the optional reason for the maintenance mode.
37
38 Examples
39         bin/console maintenance 1
40                 Enables the maintenance mode without setting a reason message
41
42         bin/console maintenance 1 "SSL certification update"
43                 Enables the maintenance mode with setting a reason message
44
45         bin/console maintenance 0
46                 Disables the maintenance mode
47
48 Options
49     -h|--help|-? Show help information
50     -v           Show more debug information.
51 HELP;
52                 return $help;
53         }
54
55         public function __construct(App\Mode $appMode, Configuration $config, $argv = null)
56         {
57                 parent::__construct($argv);
58
59                 $this->appMode = $appMode;
60                 $this->config = $config;
61         }
62
63         protected function doExecute()
64         {
65                 if ($this->getOption('v')) {
66                         $this->out('Class: ' . __CLASS__);
67                         $this->out('Arguments: ' . var_export($this->args, true));
68                         $this->out('Options: ' . var_export($this->options, true));
69                 }
70
71                 if (count($this->args) == 0) {
72                         $this->out($this->getHelp());
73                         return 0;
74                 }
75
76                 if (count($this->args) > 2) {
77                         throw new \Asika\SimpleConsole\CommandArgsException('Too many arguments');
78                 }
79
80                 if ($this->appMode->isInstall()) {
81                         throw new \RuntimeException('Database isn\'t ready or populated yet');
82                 }
83
84                 $enabled = intval($this->getArgument(0));
85
86                 $this->config->set('system', 'maintenance', $enabled);
87
88                 $reason = $this->getArgument(1);
89
90                 if ($enabled && $this->getArgument(1)) {
91                         $this->config->set('system', 'maintenance_reason', $this->getArgument(1));
92                 } else {
93                         $this->config->set('system', 'maintenance_reason', '');
94                 }
95
96                 if ($enabled) {
97                         $mode_str = "maintenance mode";
98                 } else {
99                         $mode_str = "normal mode";
100                 }
101
102                 $this->out('System set in ' . $mode_str);
103
104                 if ($enabled && $reason != '') {
105                         $this->out('Maintenance reason: ' . $reason);
106                 }
107
108                 return 0;
109         }
110
111 }