SetConfig.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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(
  34. SystemConfig $systemConfig,
  35. ) {
  36. parent::__construct($systemConfig);
  37. }
  38. protected function configure() {
  39. parent::configure();
  40. $this
  41. ->setName('config:system:set')
  42. ->setDescription('Set a system config value')
  43. ->addArgument(
  44. 'name',
  45. InputArgument::REQUIRED | InputArgument::IS_ARRAY,
  46. 'Name of the config parameter, specify multiple for array parameter'
  47. )
  48. ->addOption(
  49. 'type',
  50. null,
  51. InputOption::VALUE_REQUIRED,
  52. 'Value type [string, integer, double, boolean]',
  53. 'string'
  54. )
  55. ->addOption(
  56. 'value',
  57. null,
  58. InputOption::VALUE_REQUIRED,
  59. 'The new value of the config'
  60. )
  61. ->addOption(
  62. 'update-only',
  63. null,
  64. InputOption::VALUE_NONE,
  65. 'Only updates the value, if it is not set before, it is not being added'
  66. )
  67. ;
  68. }
  69. protected function execute(InputInterface $input, OutputInterface $output): int {
  70. $configNames = $input->getArgument('name');
  71. $configName = $configNames[0];
  72. $configValue = $this->castValue($input->getOption('value'), $input->getOption('type'));
  73. $updateOnly = $input->getOption('update-only');
  74. if (count($configNames) > 1) {
  75. $existingValue = $this->systemConfig->getValue($configName);
  76. $newValue = $this->mergeArrayValue(
  77. array_slice($configNames, 1), $existingValue, $configValue['value'], $updateOnly
  78. );
  79. $this->systemConfig->setValue($configName, $newValue);
  80. } else {
  81. if ($updateOnly && !in_array($configName, $this->systemConfig->getKeys(), true)) {
  82. throw new \UnexpectedValueException('Config parameter does not exist');
  83. }
  84. $this->systemConfig->setValue($configName, $configValue['value']);
  85. }
  86. $output->writeln('<info>System config value ' . implode(' => ', $configNames) . ' set to ' . $configValue['readable-value'] . '</info>');
  87. return 0;
  88. }
  89. /**
  90. * @param string $value
  91. * @param string $type
  92. * @return mixed
  93. * @throws \InvalidArgumentException
  94. */
  95. protected function castValue($value, $type) {
  96. switch ($type) {
  97. case 'integer':
  98. case 'int':
  99. if (!is_numeric($value)) {
  100. throw new \InvalidArgumentException('Non-numeric value specified');
  101. }
  102. return [
  103. 'value' => (int) $value,
  104. 'readable-value' => 'integer ' . (int) $value,
  105. ];
  106. case 'double':
  107. case 'float':
  108. if (!is_numeric($value)) {
  109. throw new \InvalidArgumentException('Non-numeric value specified');
  110. }
  111. return [
  112. 'value' => (double) $value,
  113. 'readable-value' => 'double ' . (double) $value,
  114. ];
  115. case 'boolean':
  116. case 'bool':
  117. $value = strtolower($value);
  118. switch ($value) {
  119. case 'true':
  120. return [
  121. 'value' => true,
  122. 'readable-value' => 'boolean ' . $value,
  123. ];
  124. case 'false':
  125. return [
  126. 'value' => false,
  127. 'readable-value' => 'boolean ' . $value,
  128. ];
  129. default:
  130. throw new \InvalidArgumentException('Unable to parse value as boolean');
  131. }
  132. // no break
  133. case 'null':
  134. return [
  135. 'value' => null,
  136. 'readable-value' => 'null',
  137. ];
  138. case 'string':
  139. $value = (string) $value;
  140. return [
  141. 'value' => $value,
  142. 'readable-value' => ($value === '') ? 'empty string' : 'string ' . $value,
  143. ];
  144. default:
  145. throw new \InvalidArgumentException('Invalid type');
  146. }
  147. }
  148. /**
  149. * @param array $configNames
  150. * @param mixed $existingValues
  151. * @param mixed $value
  152. * @param bool $updateOnly
  153. * @return array merged value
  154. * @throws \UnexpectedValueException
  155. */
  156. protected function mergeArrayValue(array $configNames, $existingValues, $value, $updateOnly) {
  157. $configName = array_shift($configNames);
  158. if (!is_array($existingValues)) {
  159. $existingValues = [];
  160. }
  161. if (!empty($configNames)) {
  162. if (isset($existingValues[$configName])) {
  163. $existingValue = $existingValues[$configName];
  164. } else {
  165. $existingValue = [];
  166. }
  167. $existingValues[$configName] = $this->mergeArrayValue($configNames, $existingValue, $value, $updateOnly);
  168. } else {
  169. if (!isset($existingValues[$configName]) && $updateOnly) {
  170. throw new \UnexpectedValueException('Config parameter does not exist');
  171. }
  172. $existingValues[$configName] = $value;
  173. }
  174. return $existingValues;
  175. }
  176. /**
  177. * @param string $optionName
  178. * @param CompletionContext $context
  179. * @return string[]
  180. */
  181. public function completeOptionValues($optionName, CompletionContext $context) {
  182. if ($optionName === 'type') {
  183. return ['string', 'integer', 'double', 'boolean'];
  184. }
  185. return parent::completeOptionValues($optionName, $context);
  186. }
  187. }