[ Index ] |
PHP Cross Reference of Joomla 4.2.2 documentation |
[Summary view] [Print] [Text view]
1 <?php 2 3 /** 4 * Joomla! Content Management System 5 * 6 * @copyright (C) 2020 Open Source Matters, Inc. <https://www.joomla.org> 7 * @license GNU General Public License version 2 or later; see LICENSE.txt 8 */ 9 10 namespace Joomla\CMS\Console; 11 12 use Joomla\Console\Command\AbstractCommand; 13 use Symfony\Component\Console\Input\Input; 14 use Symfony\Component\Console\Input\InputInterface; 15 use Symfony\Component\Console\Input\InputOption; 16 use Symfony\Component\Console\Output\OutputInterface; 17 use Symfony\Component\Console\Style\SymfonyStyle; 18 19 // phpcs:disable PSR1.Files.SideEffects 20 \defined('JPATH_PLATFORM') or die; 21 // phpcs:enable PSR1.Files.SideEffects 22 23 /** 24 * Console command for displaying configuration options 25 * 26 * @since 4.0.0 27 */ 28 class GetConfigurationCommand extends AbstractCommand 29 { 30 /** 31 * The default command name 32 * 33 * @var string 34 * @since 4.0.0 35 */ 36 protected static $defaultName = 'config:get'; 37 38 /** 39 * Stores the Input Object 40 * @var Input 41 * @since 4.0.0 42 */ 43 private $cliInput; 44 45 /** 46 * SymfonyStyle Object 47 * @var SymfonyStyle 48 * @since 4.0.0 49 */ 50 private $ioStyle; 51 52 /** 53 * Constant defining the Database option group 54 * @var array 55 * @since 4.0.0 56 */ 57 public const DB_GROUP = [ 58 'name' => 'db', 59 'options' => [ 60 'dbtype', 61 'host', 62 'user', 63 'password', 64 'dbprefix', 65 'db', 66 'dbencryption', 67 'dbsslverifyservercert', 68 'dbsslkey', 69 'dbsslcert', 70 'dbsslca', 71 'dbsslcipher' 72 ] 73 ]; 74 75 /** 76 * Constant defining the Session option group 77 * @var array 78 * @since 4.0.0 79 */ 80 public const SESSION_GROUP = [ 81 'name' => 'session', 82 'options' => [ 83 'session_handler', 84 'shared_session', 85 'session_metadata' 86 ] 87 ]; 88 89 /** 90 * Constant defining the Mail option group 91 * @var array 92 * @since 4.0.0 93 */ 94 public const MAIL_GROUP = [ 95 'name' => 'mail', 96 'options' => [ 97 'mailonline', 98 'mailer', 99 'mailfrom', 100 'fromname', 101 'sendmail', 102 'smtpauth', 103 'smtpuser', 104 'smtppass', 105 'smtphost', 106 'smtpsecure', 107 'smtpport' 108 ] 109 ]; 110 111 /** 112 * Return code if configuration is get successfully 113 * @since 4.0.0 114 */ 115 public const CONFIG_GET_SUCCESSFUL = 0; 116 117 /** 118 * Return code if configuration group option is not found 119 * @since 4.0.0 120 */ 121 public const CONFIG_GET_GROUP_NOT_FOUND = 1; 122 123 /** 124 * Return code if configuration option is not found 125 * @since 4.0.0 126 */ 127 public const CONFIG_GET_OPTION_NOT_FOUND = 2; 128 129 /** 130 * Return code if the command has been invoked with wrong options 131 * @since 4.0.0 132 */ 133 public const CONFIG_GET_OPTION_FAILED = 3; 134 135 /** 136 * Configures the IO 137 * 138 * @param InputInterface $input Console Input 139 * @param OutputInterface $output Console Output 140 * 141 * @return void 142 * 143 * @since 4.0.0 144 * 145 */ 146 private function configureIO(InputInterface $input, OutputInterface $output) 147 { 148 $this->cliInput = $input; 149 $this->ioStyle = new SymfonyStyle($input, $output); 150 } 151 152 153 /** 154 * Displays logically grouped options 155 * 156 * @param string $group The group to be processed 157 * 158 * @return integer 159 * 160 * @since 4.0.0 161 */ 162 public function processGroupOptions($group): int 163 { 164 $configs = $this->getApplication()->getConfig()->toArray(); 165 $configs = $this->formatConfig($configs); 166 167 $groups = $this->getGroups(); 168 169 $foundGroup = false; 170 171 foreach ($groups as $key => $value) { 172 if ($value['name'] === $group) { 173 $foundGroup = true; 174 $options = []; 175 176 foreach ($value['options'] as $option) { 177 $options[] = [$option, $configs[$option]]; 178 } 179 180 $this->ioStyle->table(['Option', 'Value'], $options); 181 } 182 } 183 184 if (!$foundGroup) { 185 $this->ioStyle->error("Group *$group* not found"); 186 187 return self::CONFIG_GET_GROUP_NOT_FOUND; 188 } 189 190 return self::CONFIG_GET_SUCCESSFUL; 191 } 192 193 /** 194 * Gets the defined option groups 195 * 196 * @return array 197 * 198 * @since 4.0.0 199 */ 200 public function getGroups() 201 { 202 return [ 203 self::DB_GROUP, 204 self::MAIL_GROUP, 205 self::SESSION_GROUP 206 ]; 207 } 208 209 /** 210 * Formats the configuration array into desired format 211 * 212 * @param array $configs Array of the configurations 213 * 214 * @return array 215 * 216 * @since 4.0.0 217 */ 218 public function formatConfig(array $configs): array 219 { 220 $newConfig = []; 221 222 foreach ($configs as $key => $config) { 223 $config = $config === false ? "false" : $config; 224 $config = $config === true ? "true" : $config; 225 226 if (!in_array($key, ['cwd', 'execution'])) { 227 $newConfig[$key] = $config; 228 } 229 } 230 231 return $newConfig; 232 } 233 234 /** 235 * Handles the command when a single option is requested 236 * 237 * @param string $option The option we want to get its value 238 * 239 * @return integer 240 * 241 * @since 4.0.0 242 */ 243 public function processSingleOption($option): int 244 { 245 $configs = $this->getApplication()->getConfig()->toArray(); 246 247 if (!array_key_exists($option, $configs)) { 248 $this->ioStyle->error("Can't find option *$option* in configuration list"); 249 250 return self::CONFIG_GET_OPTION_NOT_FOUND; 251 } 252 253 $value = $this->formatConfigValue($this->getApplication()->get($option)); 254 255 $this->ioStyle->table(['Option', 'Value'], [[$option, $value]]); 256 257 return self::CONFIG_GET_SUCCESSFUL; 258 } 259 260 /** 261 * Formats the Configuration value 262 * 263 * @param mixed $value Value to be formatted 264 * 265 * @return string 266 * 267 * @since 4.0.0 268 */ 269 protected function formatConfigValue($value): string 270 { 271 if ($value === false) { 272 return 'false'; 273 } elseif ($value === true) { 274 return 'true'; 275 } elseif ($value === null) { 276 return 'Not Set'; 277 } elseif (\is_array($value)) { 278 return \json_encode($value); 279 } elseif (\is_object($value)) { 280 return \json_encode(\get_object_vars($value)); 281 } else { 282 return $value; 283 } 284 } 285 286 /** 287 * Initialise the command. 288 * 289 * @return void 290 * 291 * @since 4.0.0 292 */ 293 protected function configure(): void 294 { 295 $groups = $this->getGroups(); 296 297 foreach ($groups as $key => $group) { 298 $groupNames[] = $group['name']; 299 } 300 301 $groupNames = implode(', ', $groupNames); 302 303 $this->addArgument('option', null, 'Name of the option'); 304 $this->addOption('group', 'g', InputOption::VALUE_REQUIRED, 'Name of the option'); 305 306 $help = "<info>%command.name%</info> displays the current value of a configuration option 307 \nUsage: <info>php %command.full_name%</info> <option> 308 \nGroup usage: <info>php %command.full_name%</info> --group <groupname> 309 \nAvailable group names: $groupNames"; 310 311 $this->setDescription('Display the current value of a configuration option'); 312 $this->setHelp($help); 313 } 314 315 /** 316 * Internal function to execute the command. 317 * 318 * @param InputInterface $input The input to inject into the command. 319 * @param OutputInterface $output The output to inject into the command. 320 * 321 * @return integer The command exit code 322 * 323 * @since 4.0.0 324 */ 325 protected function doExecute(InputInterface $input, OutputInterface $output): int 326 { 327 $this->configureIO($input, $output); 328 329 $configs = $this->formatConfig($this->getApplication()->getConfig()->toArray()); 330 331 $option = $this->cliInput->getArgument('option'); 332 $group = $this->cliInput->getOption('group'); 333 334 if ($group) { 335 return $this->processGroupOptions($group); 336 } 337 338 if ($option) { 339 return $this->processSingleOption($option); 340 } 341 342 if (!$option && !$group) { 343 $options = []; 344 345 array_walk( 346 $configs, 347 function ($value, $key) use (&$options) { 348 $options[] = [$key, $this->formatConfigValue($value)]; 349 } 350 ); 351 352 $this->ioStyle->title("Current options in Configuration"); 353 $this->ioStyle->table(['Option', 'Value'], $options); 354 355 return self::CONFIG_GET_SUCCESSFUL; 356 } 357 358 return self::CONFIG_GET_OPTION_NOT_FOUND; 359 } 360 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Wed Sep 7 05:41:13 2022 | Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer |