SetConfig.php 5.3 KB

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