GetConfig.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. /** * @var SystemConfig */
  31. protected $systemConfig;
  32. /**
  33. * @param SystemConfig $systemConfig
  34. */
  35. public function __construct(SystemConfig $systemConfig) {
  36. parent::__construct();
  37. $this->systemConfig = $systemConfig;
  38. }
  39. protected function configure() {
  40. parent::configure();
  41. $this
  42. ->setName('config:system:get')
  43. ->setDescription('Get a system config value')
  44. ->addArgument(
  45. 'name',
  46. InputArgument::REQUIRED | InputArgument::IS_ARRAY,
  47. 'Name of the config to get, specify multiple for array parameter'
  48. )
  49. ->addOption(
  50. 'default-value',
  51. null,
  52. InputOption::VALUE_OPTIONAL,
  53. 'If no default value is set and the config does not exist, the command will exit with 1'
  54. )
  55. ;
  56. }
  57. /**
  58. * Executes the current command.
  59. *
  60. * @param InputInterface $input An InputInterface instance
  61. * @param OutputInterface $output An OutputInterface instance
  62. * @return null|int null or 0 if everything went fine, or an error code
  63. */
  64. protected function execute(InputInterface $input, OutputInterface $output): int {
  65. $configNames = $input->getArgument('name');
  66. $configName = array_shift($configNames);
  67. $defaultValue = $input->getOption('default-value');
  68. if (!in_array($configName, $this->systemConfig->getKeys()) && !$input->hasParameterOption('--default-value')) {
  69. return 1;
  70. }
  71. if (!in_array($configName, $this->systemConfig->getKeys())) {
  72. $configValue = $defaultValue;
  73. } else {
  74. $configValue = $this->systemConfig->getValue($configName);
  75. if (!empty($configNames)) {
  76. foreach ($configNames as $configName) {
  77. if (isset($configValue[$configName])) {
  78. $configValue = $configValue[$configName];
  79. } elseif (!$input->hasParameterOption('--default-value')) {
  80. return 1;
  81. } else {
  82. $configValue = $defaultValue;
  83. break;
  84. }
  85. }
  86. }
  87. }
  88. $this->writeMixedInOutputFormat($input, $output, $configValue);
  89. return 0;
  90. }
  91. }