SetConfig.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Robin McCorkell <robin@mccorkell.me.uk>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OC\Core\Command\Config\System;
  26. use OC\SystemConfig;
  27. use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext;
  28. use Symfony\Component\Console\Input\InputArgument;
  29. use Symfony\Component\Console\Input\InputInterface;
  30. use Symfony\Component\Console\Input\InputOption;
  31. use Symfony\Component\Console\Output\OutputInterface;
  32. class SetConfig extends Base {
  33. public function __construct(SystemConfig $systemConfig) {
  34. parent::__construct($systemConfig);
  35. }
  36. protected function configure() {
  37. parent::configure();
  38. $this
  39. ->setName('config:system:set')
  40. ->setDescription('Set a system config value')
  41. ->addArgument(
  42. 'name',
  43. InputArgument::REQUIRED | InputArgument::IS_ARRAY,
  44. 'Name of the config parameter, specify multiple for array parameter'
  45. )
  46. ->addOption(
  47. 'type',
  48. null,
  49. InputOption::VALUE_REQUIRED,
  50. 'Value type [string, integer, double, boolean]',
  51. 'string'
  52. )
  53. ->addOption(
  54. 'value',
  55. null,
  56. InputOption::VALUE_REQUIRED,
  57. 'The new value of the config'
  58. )
  59. ->addOption(
  60. 'update-only',
  61. null,
  62. InputOption::VALUE_NONE,
  63. 'Only updates the value, if it is not set before, it is not being added'
  64. )
  65. ;
  66. }
  67. protected function execute(InputInterface $input, OutputInterface $output): int {
  68. $configNames = $input->getArgument('name');
  69. $configName = $configNames[0];
  70. $configValue = $this->castValue($input->getOption('value'), $input->getOption('type'));
  71. $updateOnly = $input->getOption('update-only');
  72. if (count($configNames) > 1) {
  73. $existingValue = $this->systemConfig->getValue($configName);
  74. $newValue = $this->mergeArrayValue(
  75. array_slice($configNames, 1), $existingValue, $configValue['value'], $updateOnly
  76. );
  77. $this->systemConfig->setValue($configName, $newValue);
  78. } else {
  79. if ($updateOnly && !in_array($configName, $this->systemConfig->getKeys(), true)) {
  80. throw new \UnexpectedValueException('Config parameter does not exist');
  81. }
  82. $this->systemConfig->setValue($configName, $configValue['value']);
  83. }
  84. $output->writeln('<info>System config value ' . implode(' => ', $configNames) . ' set to ' . $configValue['readable-value'] . '</info>');
  85. return 0;
  86. }
  87. /**
  88. * @param string $value
  89. * @param string $type
  90. * @return mixed
  91. * @throws \InvalidArgumentException
  92. */
  93. protected function castValue($value, $type) {
  94. switch ($type) {
  95. case 'integer':
  96. case 'int':
  97. if (!is_numeric($value)) {
  98. throw new \InvalidArgumentException('Non-numeric value specified');
  99. }
  100. return [
  101. 'value' => (int) $value,
  102. 'readable-value' => 'integer ' . (int) $value,
  103. ];
  104. case 'double':
  105. case 'float':
  106. if (!is_numeric($value)) {
  107. throw new \InvalidArgumentException('Non-numeric value specified');
  108. }
  109. return [
  110. 'value' => (double) $value,
  111. 'readable-value' => 'double ' . (double) $value,
  112. ];
  113. case 'boolean':
  114. case 'bool':
  115. $value = strtolower($value);
  116. switch ($value) {
  117. case 'true':
  118. return [
  119. 'value' => true,
  120. 'readable-value' => 'boolean ' . $value,
  121. ];
  122. case 'false':
  123. return [
  124. 'value' => false,
  125. 'readable-value' => 'boolean ' . $value,
  126. ];
  127. default:
  128. throw new \InvalidArgumentException('Unable to parse value as boolean');
  129. }
  130. // no break
  131. case 'null':
  132. return [
  133. 'value' => null,
  134. 'readable-value' => 'null',
  135. ];
  136. case 'string':
  137. $value = (string) $value;
  138. return [
  139. 'value' => $value,
  140. 'readable-value' => ($value === '') ? 'empty string' : 'string ' . $value,
  141. ];
  142. default:
  143. throw new \InvalidArgumentException('Invalid type');
  144. }
  145. }
  146. /**
  147. * @param array $configNames
  148. * @param mixed $existingValues
  149. * @param mixed $value
  150. * @param bool $updateOnly
  151. * @return array merged value
  152. * @throws \UnexpectedValueException
  153. */
  154. protected function mergeArrayValue(array $configNames, $existingValues, $value, $updateOnly) {
  155. $configName = array_shift($configNames);
  156. if (!is_array($existingValues)) {
  157. $existingValues = [];
  158. }
  159. if (!empty($configNames)) {
  160. if (isset($existingValues[$configName])) {
  161. $existingValue = $existingValues[$configName];
  162. } else {
  163. $existingValue = [];
  164. }
  165. $existingValues[$configName] = $this->mergeArrayValue($configNames, $existingValue, $value, $updateOnly);
  166. } else {
  167. if (!isset($existingValues[$configName]) && $updateOnly) {
  168. throw new \UnexpectedValueException('Config parameter does not exist');
  169. }
  170. $existingValues[$configName] = $value;
  171. }
  172. return $existingValues;
  173. }
  174. /**
  175. * @param string $optionName
  176. * @param CompletionContext $context
  177. * @return string[]
  178. */
  179. public function completeOptionValues($optionName, CompletionContext $context) {
  180. if ($optionName === 'type') {
  181. return ['string', 'integer', 'double', 'boolean'];
  182. }
  183. return parent::completeOptionValues($optionName, $context);
  184. }
  185. }