]> git.mxchange.org Git - friendica.git/blob - src/Console/Config.php
API: Accept "redirect_uris" as both array and string
[friendica.git] / src / Console / Config.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, 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\Capability\IManageConfigValues;
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 IManageConfigValues
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, IManageConfigValues $config, array $argv = null)
98         {
99                 parent::__construct($argv);
100
101                 $this->appMode = $appMode;
102                 $this->config = $config;
103         }
104
105         protected function doExecute(): int
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 (count($this->args) == 3) {
119                         $cat = $this->getArgument(0);
120                         $key = $this->getArgument(1);
121                         $value = $this->getArgument(2);
122
123                         if (is_array($this->config->get($cat, $key))) {
124                                 throw new RuntimeException("$cat.$key is an array and can't be set using this command.");
125                         }
126
127                         if ($this->config->get($cat, $key) === $value) {
128                                 throw new RuntimeException("$cat.$key already set to $value.");
129                         }
130
131                         $result = $this->config->set($cat, $key, $value);
132                         if ($result) {
133                                 $this->out("{$cat}.{$key} <= " .
134                                            $this->config->get($cat, $key));
135                         } else {
136                                 $this->out("Unable to set {$cat}.{$key}");
137                         }
138                 }
139
140                 if (count($this->args) == 2) {
141                         $cat = $this->getArgument(0);
142                         $key = $this->getArgument(1);
143                         $value = $this->config->get($this->getArgument(0), $this->getArgument(1));
144
145                         if (is_array($value)) {
146                                 foreach ($value as $k => $v) {
147                                         $this->out("{$cat}.{$key}[{$k}] => " . (is_array($v) ? implode(', ', $v) : $v));
148                                 }
149                         } else {
150                                 $this->out("{$cat}.{$key} => " . ($value ?? 'NULL'));
151                         }
152                 }
153
154                 if (count($this->args) == 1) {
155                         $cat = $this->getArgument(0);
156                         $this->config->reload();
157                         $configCache = $this->config->getCache();
158
159                         if ($configCache->get($cat) !== null) {
160                                 $this->out("[{$cat}]");
161                                 $catVal = $configCache->get($cat);
162                                 foreach ($catVal as $key => $value) {
163                                         if (is_array($value)) {
164                                                 foreach ($value as $k => $v) {
165                                                         $this->out("{$key}[{$k}] => " . (is_array($v) ? implode(', ', $v) : $v));
166                                                 }
167                                         } else {
168                                                 $this->out("{$key} => " . $value);
169                                         }
170                                 }
171                         } else {
172                                 $this->out('Config section ' . $this->getArgument(0) . ' returned nothing');
173                         }
174                 }
175
176                 if (count($this->args) == 0) {
177                         $this->config->reload();
178
179                         $config = $this->config->getCache()->getAll();
180                         foreach ($config as $cat => $section) {
181                                 if (is_array($section)) {
182                                         foreach ($section as $key => $value) {
183                                                 if (is_array($value)) {
184                                                         foreach ($value as $k => $v) {
185                                                                 $this->out("{$cat}.{$key}[{$k}] => " . (is_array($v) ? implode(', ', $v) : $v));
186                                                         }
187                                                 } else {
188                                                         $this->out("{$cat}.{$key} => " . $value);
189                                                 }
190                                         }
191                                 } else {
192                                         $this->out("config.{$cat} => " . $section);
193                                 }
194                         }
195                 }
196
197                 return 0;
198         }
199 }