]> git.mxchange.org Git - friendica.git/blob - src/Console/Config.php
The value is used twice, so use a variable
[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\Config\Configuration;
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         /**
39          * @var App\Mode
40          */
41         private $appMode;
42         /**
43          * @var Configuration
44          */
45         private $config;
46
47         protected function getHelp()
48         {
49                 $help = <<<HELP
50 console config - Manage site configuration
51 Synopsis
52         bin/console config [-h|--help|-?] [-v]
53         bin/console config <category> [-h|--help|-?] [-v]
54         bin/console config <category> <key> [-h|--help|-?] [-v]
55         bin/console config <category> <key> <value> [-h|--help|-?] [-v]
56
57 Description
58         bin/console config
59                 Lists all config values
60
61         bin/console config <category>
62                 Lists all config values in the provided category
63
64         bin/console config <category> <key>
65                 Shows the value of the provided key in the category
66
67         bin/console config <category> <key> <value>
68                 Sets the value of the provided key in the category
69
70 Notes:
71         Setting config entries which are manually set in config/local.config.php may result in
72         conflict between database settings and the manual startup settings.
73
74 Options
75     -h|--help|-? Show help information
76     -v           Show more debug information.
77 HELP;
78                 return $help;
79         }
80
81         public function __construct(App\Mode $appMode, Configuration $config, array $argv = null)
82         {
83                 parent::__construct($argv);
84
85                 $this->appMode = $appMode;
86                 $this->config = $config;
87         }
88
89         protected function doExecute()
90         {
91                 if ($this->getOption('v')) {
92                         $this->out('Executable: ' . $this->executable);
93                         $this->out('Class: ' . __CLASS__);
94                         $this->out('Arguments: ' . var_export($this->args, true));
95                         $this->out('Options: ' . var_export($this->options, true));
96                 }
97
98                 if (count($this->args) > 3) {
99                         throw new CommandArgsException('Too many arguments');
100                 }
101
102                 if (!$this->appMode->has(App\Mode::DBCONFIGAVAILABLE)) {
103                         $this->out('Database isn\'t ready or populated yet, showing file config only');
104                 }
105
106                 if (count($this->args) == 3) {
107                         $cat = $this->getArgument(0);
108                         $key = $this->getArgument(1);
109                         $value = $this->getArgument(2);
110
111                         if (is_array($this->config->get($cat, $key))) {
112                                 throw new RuntimeException("$cat.$key is an array and can't be set using this command.");
113                         }
114
115                         $result = $this->config->set($cat, $key, $value);
116                         if ($result) {
117                                 $this->out("{$cat}.{$key} <= " .
118                                            $this->config->get($cat, $key));
119                         } else {
120                                 $this->out("Unable to set {$cat}.{$key}");
121                         }
122                 }
123
124                 if (count($this->args) == 2) {
125                         $cat = $this->getArgument(0);
126                         $key = $this->getArgument(1);
127                         $value = $this->config->get($this->getArgument(0), $this->getArgument(1));
128
129                         if (is_array($value)) {
130                                 foreach ($value as $k => $v) {
131                                         $this->out("{$cat}.{$key}[{$k}] => " . (is_array($v) ? implode(', ', $v) : $v));
132                                 }
133                         } else {
134                                 $this->out("{$cat}.{$key} => " . $value);
135                         }
136                 }
137
138                 if (count($this->args) == 1) {
139                         $cat = $this->getArgument(0);
140                         $this->config->load($cat);
141                         $configCache = $this->config->getCache();
142
143                         if ($configCache->get($cat) !== null) {
144                                 $this->out("[{$cat}]");
145                                 $catVal = $configCache->get($cat);
146                                 foreach ($catVal as $key => $value) {
147                                         if (is_array($value)) {
148                                                 foreach ($value as $k => $v) {
149                                                         $this->out("{$key}[{$k}] => " . (is_array($v) ? implode(', ', $v) : $v));
150                                                 }
151                                         } else {
152                                                 $this->out("{$key} => " . $value);
153                                         }
154                                 }
155                         } else {
156                                 $this->out('Config section ' . $this->getArgument(0) . ' returned nothing');
157                         }
158                 }
159
160                 if (count($this->args) == 0) {
161                         $this->config->load();
162
163                         if ($this->config->get('system', 'config_adapter') == 'jit' && $this->appMode->has(App\Mode::DBCONFIGAVAILABLE)) {
164                                 $this->out('Warning: The JIT (Just In Time) Config adapter doesn\'t support loading the entire configuration, showing file config only');
165                         }
166
167                         $config = $this->config->getCache()->getAll();
168                         foreach ($config as $cat => $section) {
169                                 if (is_array($section)) {
170                                         foreach ($section as $key => $value) {
171                                                 if (is_array($value)) {
172                                                         foreach ($value as $k => $v) {
173                                                                 $this->out("{$cat}.{$key}[{$k}] => " . (is_array($v) ? implode(', ', $v) : $v));
174                                                         }
175                                                 } else {
176                                                         $this->out("{$cat}.{$key} => " . $value);
177                                                 }
178                                         }
179                                 } else {
180                                         $this->out("config.{$cat} => " . $section);
181                                 }
182                         }
183                 }
184
185                 return 0;
186         }
187 }