GetConfig.php 2.9 KB

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