SetConfig.php 5.7 KB

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