DeleteConfig.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. *
  9. * @license AGPL-3.0
  10. *
  11. * This code is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License, version 3,
  13. * as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License, version 3,
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>
  22. *
  23. */
  24. namespace OC\Core\Command\Config\System;
  25. use OC\SystemConfig;
  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 DeleteConfig 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:delete')
  44. ->setDescription('Delete a system config value')
  45. ->addArgument(
  46. 'name',
  47. InputArgument::REQUIRED | InputArgument::IS_ARRAY,
  48. 'Name of the config to delete, specify multiple for array parameter'
  49. )
  50. ->addOption(
  51. 'error-if-not-exists',
  52. null,
  53. InputOption::VALUE_NONE,
  54. 'Checks whether the config exists before deleting it'
  55. )
  56. ;
  57. }
  58. protected function execute(InputInterface $input, OutputInterface $output): int {
  59. $configNames = $input->getArgument('name');
  60. $configName = $configNames[0];
  61. if (count($configNames) > 1) {
  62. if ($input->hasParameterOption('--error-if-not-exists') && !in_array($configName, $this->systemConfig->getKeys())) {
  63. $output->writeln('<error>System config ' . implode(' => ', $configNames) . ' could not be deleted because it did not exist</error>');
  64. return 1;
  65. }
  66. $value = $this->systemConfig->getValue($configName);
  67. try {
  68. $value = $this->removeSubValue(array_slice($configNames, 1), $value, $input->hasParameterOption('--error-if-not-exists'));
  69. } catch (\UnexpectedValueException $e) {
  70. $output->writeln('<error>System config ' . implode(' => ', $configNames) . ' could not be deleted because it did not exist</error>');
  71. return 1;
  72. }
  73. $this->systemConfig->setValue($configName, $value);
  74. $output->writeln('<info>System config value ' . implode(' => ', $configNames) . ' deleted</info>');
  75. return 0;
  76. } else {
  77. if ($input->hasParameterOption('--error-if-not-exists') && !in_array($configName, $this->systemConfig->getKeys())) {
  78. $output->writeln('<error>System config ' . $configName . ' could not be deleted because it did not exist</error>');
  79. return 1;
  80. }
  81. $this->systemConfig->deleteValue($configName);
  82. $output->writeln('<info>System config value ' . $configName . ' deleted</info>');
  83. return 0;
  84. }
  85. }
  86. protected function removeSubValue($keys, $currentValue, $throwError) {
  87. $nextKey = array_shift($keys);
  88. if (is_array($currentValue)) {
  89. if (isset($currentValue[$nextKey])) {
  90. if (empty($keys)) {
  91. unset($currentValue[$nextKey]);
  92. } else {
  93. $currentValue[$nextKey] = $this->removeSubValue($keys, $currentValue[$nextKey], $throwError);
  94. }
  95. } elseif ($throwError) {
  96. throw new \UnexpectedValueException('Config parameter does not exist');
  97. }
  98. } elseif ($throwError) {
  99. throw new \UnexpectedValueException('Config parameter does not exist');
  100. }
  101. return $currentValue;
  102. }
  103. }