]> git.mxchange.org Git - friendica.git/blob - src/Core/Console/Config.php
Remove obsolete lines from Console\Config
[friendica.git] / src / Core / Console / Config.php
1 <?php
2
3 namespace Friendica\Core\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.ini.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->mode & 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}] => " . $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 (!is_null($a->config[$cat])) {
128                                 $this->out("[{$cat}]");
129                                 foreach ($a->config[$cat] as $key => $value) {
130                                         if (is_array($value)) {
131                                                 foreach ($value as $k => $v) {
132                                                         $this->out("{$key}[{$k}] => " . $v);
133                                                 }
134                                         } else {
135                                                 $this->out("{$key} => " . $value);
136                                         }
137                                 }
138                         } else {
139                                 $this->out('Config section ' . $this->getArgument(0) . ' returned nothing');
140                         }
141                 }
142
143                 if (count($this->args) == 0) {
144                         Core\Config::load();
145
146                         if (Core\Config::get('system', 'config_adapter') == 'jit' && $a->mode & App::MODE_DBCONFIGAVAILABLE) {
147                                 $this->out('Warning: The JIT (Just In Time) Config adapter doesn\'t support loading the entire configuration, showing file config only');
148                         }
149
150                         foreach ($a->config as $cat => $section) {
151                                 if (is_array($section)) {
152                                         foreach ($section as $key => $value) {
153                                                 if (is_array($value)) {
154                                                         foreach ($value as $k => $v) {
155                                                                 $this->out("{$cat}.{$key}[{$k}] => " . $v);
156                                                         }
157                                                 } else {
158                                                         $this->out("{$cat}.{$key} => " . $value);
159                                                 }
160                                         }
161                                 } else {
162                                         $this->out("config.{$cat} => " . $section);
163                                 }
164                         }
165                 }
166
167                 return 0;
168         }
169 }