SetConfig.php 5.8 KB

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