GetConfig.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OC\Core\Command\Config\System;
  8. use OC\SystemConfig;
  9. use Symfony\Component\Console\Input\InputArgument;
  10. use Symfony\Component\Console\Input\InputInterface;
  11. use Symfony\Component\Console\Input\InputOption;
  12. use Symfony\Component\Console\Output\OutputInterface;
  13. class GetConfig extends Base {
  14. public function __construct(
  15. SystemConfig $systemConfig,
  16. ) {
  17. parent::__construct($systemConfig);
  18. }
  19. protected function configure() {
  20. parent::configure();
  21. $this
  22. ->setName('config:system:get')
  23. ->setDescription('Get a system config value')
  24. ->addArgument(
  25. 'name',
  26. InputArgument::REQUIRED | InputArgument::IS_ARRAY,
  27. 'Name of the config to get, specify multiple for array parameter'
  28. )
  29. ->addOption(
  30. 'default-value',
  31. null,
  32. InputOption::VALUE_OPTIONAL,
  33. 'If no default value is set and the config does not exist, the command will exit with 1'
  34. )
  35. ;
  36. }
  37. /**
  38. * Executes the current command.
  39. *
  40. * @param InputInterface $input An InputInterface instance
  41. * @param OutputInterface $output An OutputInterface instance
  42. * @return int 0 if everything went fine, or an error code
  43. */
  44. protected function execute(InputInterface $input, OutputInterface $output): int {
  45. $configNames = $input->getArgument('name');
  46. $configName = array_shift($configNames);
  47. $defaultValue = $input->getOption('default-value');
  48. if (!in_array($configName, $this->systemConfig->getKeys()) && !$input->hasParameterOption('--default-value')) {
  49. return 1;
  50. }
  51. if (!in_array($configName, $this->systemConfig->getKeys())) {
  52. $configValue = $defaultValue;
  53. } else {
  54. $configValue = $this->systemConfig->getValue($configName);
  55. if (!empty($configNames)) {
  56. foreach ($configNames as $configName) {
  57. if (isset($configValue[$configName])) {
  58. $configValue = $configValue[$configName];
  59. } elseif (!$input->hasParameterOption('--default-value')) {
  60. return 1;
  61. } else {
  62. $configValue = $defaultValue;
  63. break;
  64. }
  65. }
  66. }
  67. }
  68. $this->writeMixedInOutputFormat($input, $output, $configValue);
  69. return 0;
  70. }
  71. }