]> git.mxchange.org Git - friendica.git/blob - src/Console/Config.php
When comparing config values, use strict string comparison
[friendica.git] / src / Console / Config.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Console;
23
24 use Asika\SimpleConsole\CommandArgsException;
25 use Friendica\App;
26 use Friendica\Core\Config\IConfig;
27 use RuntimeException;
28
29 /**
30  * tool to access the system config from the CLI
31  *
32  * With this script you can access the system configuration of your node from
33  * the CLI. You can do both, reading current values stored in the database and
34  * set new values to config variables.
35  *
36  * Usage:
37  *   If you specify no parameters at the CLI, the script will list all config
38  *   variables defined.
39  *
40  *   If you specify one parameter, the script will list all config variables
41  *   defined in this section of the configuration (e.g. "system").
42  *
43  *   If you specify two parameters, the script will show you the current value
44  *   of the named configuration setting. (e.g. "system loglevel")
45  *
46  *   If you specify three parameters, the named configuration setting will be
47  *   set to the value of the last parameter. (e.g. "system loglevel 0" will
48  *   disable logging)
49  */
50 class Config extends \Asika\SimpleConsole\Console
51 {
52         protected $helpOptions = ['h', 'help', '?'];
53
54         /**
55          * @var App\Mode
56          */
57         private $appMode;
58         /**
59          * @var IConfig
60          */
61         private $config;
62
63         protected function getHelp()
64         {
65                 $help = <<<HELP
66 console config - Manage site configuration
67 Synopsis
68         bin/console config [-h|--help|-?] [-v]
69         bin/console config <category> [-h|--help|-?] [-v]
70         bin/console config <category> <key> [-h|--help|-?] [-v]
71         bin/console config <category> <key> <value> [-h|--help|-?] [-v]
72
73 Description
74         bin/console config
75                 Lists all config values
76
77         bin/console config <category>
78                 Lists all config values in the provided category
79
80         bin/console config <category> <key>
81                 Shows the value of the provided key in the category
82
83         bin/console config <category> <key> <value>
84                 Sets the value of the provided key in the category
85
86 Notes:
87         Setting config entries which are manually set in config/local.config.php may result in
88         conflict between database settings and the manual startup settings.
89
90 Options
91     -h|--help|-? Show help information
92     -v           Show more debug information.
93 HELP;
94                 return $help;
95         }
96
97         public function __construct(App\Mode $appMode, IConfig $config, array $argv = null)
98         {
99                 parent::__construct($argv);
100
101                 $this->appMode = $appMode;
102                 $this->config = $config;
103         }
104
105         protected function doExecute()
106         {
107                 if ($this->getOption('v')) {
108                         $this->out('Executable: ' . $this->executable);
109                         $this->out('Class: ' . __CLASS__);
110                         $this->out('Arguments: ' . var_export($this->args, true));
111                         $this->out('Options: ' . var_export($this->options, true));
112                 }
113
114                 if (count($this->args) > 3) {
115                         throw new CommandArgsException('Too many arguments');
116                 }
117
118                 if (!$this->appMode->has(App\Mode::DBCONFIGAVAILABLE)) {
119                         $this->out('Database isn\'t ready or populated yet, showing file config only');
120                 }
121
122                 if (count($this->args) == 3) {
123                         $cat = $this->getArgument(0);
124                         $key = $this->getArgument(1);
125                         $value = $this->getArgument(2);
126
127                         if (is_array($this->config->get($cat, $key))) {
128                                 throw new RuntimeException("$cat.$key is an array and can't be set using this command.");
129                         }
130
131                         if ($this->config->get($cat, $key) === $value) {
132                                 throw new RuntimeException("$cat.$key already set to $value.");
133                         }
134
135                         $result = $this->config->set($cat, $key, $value);
136                         if ($result) {
137                                 $this->out("{$cat}.{$key} <= " .
138                                            $this->config->get($cat, $key));
139                         } else {
140                                 $this->out("Unable to set {$cat}.{$key}");
141                         }
142                 }
143
144                 if (count($this->args) == 2) {
145                         $cat = $this->getArgument(0);
146                         $key = $this->getArgument(1);
147                         $value = $this->config->get($this->getArgument(0), $this->getArgument(1));
148
149                         if (is_array($value)) {
150                                 foreach ($value as $k => $v) {
151                                         $this->out("{$cat}.{$key}[{$k}] => " . (is_array($v) ? implode(', ', $v) : $v));
152                                 }
153                         } else {
154                                 $this->out("{$cat}.{$key} => " . $value);
155                         }
156                 }
157
158                 if (count($this->args) == 1) {
159                         $cat = $this->getArgument(0);
160                         $this->config->load($cat);
161                         $configCache = $this->config->getCache();
162
163                         if ($configCache->get($cat) !== null) {
164                                 $this->out("[{$cat}]");
165                                 $catVal = $configCache->get($cat);
166                                 foreach ($catVal as $key => $value) {
167                                         if (is_array($value)) {
168                                                 foreach ($value as $k => $v) {
169                                                         $this->out("{$key}[{$k}] => " . (is_array($v) ? implode(', ', $v) : $v));
170                                                 }
171                                         } else {
172                                                 $this->out("{$key} => " . $value);
173                                         }
174                                 }
175                         } else {
176                                 $this->out('Config section ' . $this->getArgument(0) . ' returned nothing');
177                         }
178                 }
179
180                 if (count($this->args) == 0) {
181                         $this->config->load();
182
183                         if ($this->config->get('system', 'config_adapter') == 'jit' && $this->appMode->has(App\Mode::DBCONFIGAVAILABLE)) {
184                                 $this->out('Warning: The JIT (Just In Time) Config adapter doesn\'t support loading the entire configuration, showing file config only');
185                         }
186
187                         $config = $this->config->getCache()->getAll();
188                         foreach ($config as $cat => $section) {
189                                 if (is_array($section)) {
190                                         foreach ($section as $key => $value) {
191                                                 if (is_array($value)) {
192                                                         foreach ($value as $k => $v) {
193                                                                 $this->out("{$cat}.{$key}[{$k}] => " . (is_array($v) ? implode(', ', $v) : $v));
194                                                         }
195                                                 } else {
196                                                         $this->out("{$cat}.{$key} => " . $value);
197                                                 }
198                                         }
199                                 } else {
200                                         $this->out("config.{$cat} => " . $section);
201                                 }
202                         }
203                 }
204
205                 return 0;
206         }
207 }