GetConfig.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. *
  8. * @license AGPL-3.0
  9. *
  10. * This code is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License, version 3,
  12. * as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License, version 3,
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>
  21. *
  22. */
  23. namespace OC\Core\Command\Config\System;
  24. use OC\SystemConfig;
  25. use Symfony\Component\Console\Input\InputArgument;
  26. use Symfony\Component\Console\Input\InputInterface;
  27. use Symfony\Component\Console\Input\InputOption;
  28. use Symfony\Component\Console\Output\OutputInterface;
  29. class GetConfig extends Base {
  30. public function __construct(
  31. SystemConfig $systemConfig,
  32. ) {
  33. parent::__construct($systemConfig);
  34. }
  35. protected function configure() {
  36. parent::configure();
  37. $this
  38. ->setName('config:system:get')
  39. ->setDescription('Get a system config value')
  40. ->addArgument(
  41. 'name',
  42. InputArgument::REQUIRED | InputArgument::IS_ARRAY,
  43. 'Name of the config to get, specify multiple for array parameter'
  44. )
  45. ->addOption(
  46. 'default-value',
  47. null,
  48. InputOption::VALUE_OPTIONAL,
  49. 'If no default value is set and the config does not exist, the command will exit with 1'
  50. )
  51. ;
  52. }
  53. /**
  54. * Executes the current command.
  55. *
  56. * @param InputInterface $input An InputInterface instance
  57. * @param OutputInterface $output An OutputInterface instance
  58. * @return int 0 if everything went fine, or an error code
  59. */
  60. protected function execute(InputInterface $input, OutputInterface $output): int {
  61. $configNames = $input->getArgument('name');
  62. $configName = array_shift($configNames);
  63. $defaultValue = $input->getOption('default-value');
  64. if (!in_array($configName, $this->systemConfig->getKeys()) && !$input->hasParameterOption('--default-value')) {
  65. return 1;
  66. }
  67. if (!in_array($configName, $this->systemConfig->getKeys())) {
  68. $configValue = $defaultValue;
  69. } else {
  70. $configValue = $this->systemConfig->getValue($configName);
  71. if (!empty($configNames)) {
  72. foreach ($configNames as $configName) {
  73. if (isset($configValue[$configName])) {
  74. $configValue = $configValue[$configName];
  75. } elseif (!$input->hasParameterOption('--default-value')) {
  76. return 1;
  77. } else {
  78. $configValue = $defaultValue;
  79. break;
  80. }
  81. }
  82. }
  83. }
  84. $this->writeMixedInOutputFormat($input, $output, $configValue);
  85. return 0;
  86. }
  87. }