]> git.mxchange.org Git - friendica.git/blob - src/Console/Config.php
Merge pull request #7072 from nupplaphil/task/mod_directory
[friendica.git] / src / Console / Config.php
1 <?php
2
3 namespace Friendica\Console;
4
5 use Asika\SimpleConsole\CommandArgsException;
6 use Friendica\App;
7 use Friendica\Core;
8 use RuntimeException;
9
10 /**
11  * @brief tool to access the system config from the CLI
12  *
13  * With this script you can access the system configuration of your node from
14  * the CLI. You can do both, reading current values stored in the database and
15  * set new values to config variables.
16  *
17  * Usage:
18  *   If you specify no parameters at the CLI, the script will list all config
19  *   variables defined.
20  *
21  *   If you specify one parameter, the script will list all config variables
22  *   defined in this section of the configuration (e.g. "system").
23  *
24  *   If you specify two parameters, the script will show you the current value
25  *   of the named configuration setting. (e.g. "system loglevel")
26  *
27  *   If you specify three parameters, the named configuration setting will be
28  *   set to the value of the last parameter. (e.g. "system loglevel 0" will
29  *   disable logging)
30  *
31  * @author Tobias Diekershoff <tobias.diekershoff@gmx.net>
32  * @author Hypolite Petovan <hypolite@mrpetovan.com>
33  */
34 class Config extends \Asika\SimpleConsole\Console
35 {
36         protected $helpOptions = ['h', 'help', '?'];
37
38         protected function getHelp()
39         {
40                 $help = <<<HELP
41 console config - Manage site configuration
42 Synopsis
43         bin/console config [-h|--help|-?] [-v]
44         bin/console config <category> [-h|--help|-?] [-v]
45         bin/console config <category> <key> [-h|--help|-?] [-v]
46         bin/console config <category> <key> <value> [-h|--help|-?] [-v]
47
48 Description
49         bin/console config
50                 Lists all config values
51
52         bin/console config <category>
53                 Lists all config values in the provided category
54
55         bin/console config <category> <key>
56                 Shows the value of the provided key in the category
57
58         bin/console config <category> <key> <value>
59                 Sets the value of the provided key in the category
60
61 Notes:
62         Setting config entries which are manually set in config/local.config.php may result in
63         conflict between database settings and the manual startup settings.
64
65 Options
66     -h|--help|-? Show help information
67     -v           Show more debug information.
68 HELP;
69                 return $help;
70         }
71
72         protected function doExecute()
73         {
74                 $a = \Friendica\BaseObject::getApp();
75
76                 if ($this->getOption('v')) {
77                         $this->out('Executable: ' . $this->executable);
78                         $this->out('Class: ' . __CLASS__);
79                         $this->out('Arguments: ' . var_export($this->args, true));
80                         $this->out('Options: ' . var_export($this->options, true));
81                 }
82
83                 if (count($this->args) > 3) {
84                         throw new CommandArgsException('Too many arguments');
85                 }
86
87                 if (!$a->getMode()->has(App\Mode::DBCONFIGAVAILABLE)) {
88                         $this->out('Database isn\'t ready or populated yet, showing file config only');
89                 }
90
91                 if (count($this->args) == 3) {
92                         $cat = $this->getArgument(0);
93                         $key = $this->getArgument(1);
94                         $value = $this->getArgument(2);
95
96                         if (is_array(Core\Config::get($cat, $key))) {
97                                 throw new RuntimeException("$cat.$key is an array and can't be set using this command.");
98                         }
99
100                         $result = Core\Config::set($cat, $key, $value);
101                         if ($result) {
102                                 $this->out("{$cat}.{$key} <= " .
103                                         Core\Config::get($cat, $key));
104                         } else {
105                                 $this->out("Unable to set {$cat}.{$key}");
106                         }
107                 }
108
109                 if (count($this->args) == 2) {
110                         $cat = $this->getArgument(0);
111                         $key = $this->getArgument(1);
112                         $value = Core\Config::get($this->getArgument(0), $this->getArgument(1));
113
114                         if (is_array($value)) {
115                                 foreach ($value as $k => $v) {
116                                         $this->out("{$cat}.{$key}[{$k}] => " . (is_array($v) ? implode(', ', $v) : $v));
117                                 }
118                         } else {
119                                 $this->out("{$cat}.{$key} => " . $value);
120                         }
121                 }
122
123                 if (count($this->args) == 1) {
124                         $cat = $this->getArgument(0);
125                         Core\Config::load($cat);
126
127                         if ($a->getConfigCache()->get($cat) !== null) {
128                                 $this->out("[{$cat}]");
129                                 $catVal = $a->getConfigCache()->get($cat);
130                                 foreach ($catVal as $key => $value) {
131                                         if (is_array($value)) {
132                                                 foreach ($value as $k => $v) {
133                                                         $this->out("{$key}[{$k}] => " . (is_array($v) ? implode(', ', $v) : $v));
134                                                 }
135                                         } else {
136                                                 $this->out("{$key} => " . $value);
137                                         }
138                                 }
139                         } else {
140                                 $this->out('Config section ' . $this->getArgument(0) . ' returned nothing');
141                         }
142                 }
143
144                 if (count($this->args) == 0) {
145                         Core\Config::load();
146
147                         if (Core\Config::get('system', 'config_adapter') == 'jit' && $a->getMode()->has(App\Mode::DBCONFIGAVAILABLE)) {
148                                 $this->out('Warning: The JIT (Just In Time) Config adapter doesn\'t support loading the entire configuration, showing file config only');
149                         }
150
151                         $config = $a->getConfigCache()->getAll();
152                         foreach ($config as $cat => $section) {
153                                 if (is_array($section)) {
154                                         foreach ($section as $key => $value) {
155                                                 if (is_array($value)) {
156                                                         foreach ($value as $k => $v) {
157                                                                 $this->out("{$cat}.{$key}[{$k}] => " . (is_array($v) ? implode(', ', $v) : $v));
158                                                         }
159                                                 } else {
160                                                         $this->out("{$cat}.{$key} => " . $value);
161                                                 }
162                                         }
163                                 } else {
164                                         $this->out("config.{$cat} => " . $section);
165                                 }
166                         }
167                 }
168
169                 return 0;
170         }
171 }