DeleteConfig.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. public function __construct(
  32. SystemConfig $systemConfig,
  33. ) {
  34. parent::__construct($systemConfig);
  35. }
  36. protected function configure() {
  37. parent::configure();
  38. $this
  39. ->setName('config:system:delete')
  40. ->setDescription('Delete a system config value')
  41. ->addArgument(
  42. 'name',
  43. InputArgument::REQUIRED | InputArgument::IS_ARRAY,
  44. 'Name of the config to delete, specify multiple for array parameter'
  45. )
  46. ->addOption(
  47. 'error-if-not-exists',
  48. null,
  49. InputOption::VALUE_NONE,
  50. 'Checks whether the config exists before deleting it'
  51. )
  52. ;
  53. }
  54. protected function execute(InputInterface $input, OutputInterface $output): int {
  55. $configNames = $input->getArgument('name');
  56. $configName = $configNames[0];
  57. if (count($configNames) > 1) {
  58. if ($input->hasParameterOption('--error-if-not-exists') && !in_array($configName, $this->systemConfig->getKeys())) {
  59. $output->writeln('<error>System config ' . implode(' => ', $configNames) . ' could not be deleted because it did not exist</error>');
  60. return 1;
  61. }
  62. $value = $this->systemConfig->getValue($configName);
  63. try {
  64. $value = $this->removeSubValue(array_slice($configNames, 1), $value, $input->hasParameterOption('--error-if-not-exists'));
  65. } catch (\UnexpectedValueException $e) {
  66. $output->writeln('<error>System config ' . implode(' => ', $configNames) . ' could not be deleted because it did not exist</error>');
  67. return 1;
  68. }
  69. $this->systemConfig->setValue($configName, $value);
  70. $output->writeln('<info>System config value ' . implode(' => ', $configNames) . ' deleted</info>');
  71. return 0;
  72. } else {
  73. if ($input->hasParameterOption('--error-if-not-exists') && !in_array($configName, $this->systemConfig->getKeys())) {
  74. $output->writeln('<error>System config ' . $configName . ' could not be deleted because it did not exist</error>');
  75. return 1;
  76. }
  77. $this->systemConfig->deleteValue($configName);
  78. $output->writeln('<info>System config value ' . $configName . ' deleted</info>');
  79. return 0;
  80. }
  81. }
  82. protected function removeSubValue($keys, $currentValue, $throwError) {
  83. $nextKey = array_shift($keys);
  84. if (is_array($currentValue)) {
  85. if (isset($currentValue[$nextKey])) {
  86. if (empty($keys)) {
  87. unset($currentValue[$nextKey]);
  88. } else {
  89. $currentValue[$nextKey] = $this->removeSubValue($keys, $currentValue[$nextKey], $throwError);
  90. }
  91. } elseif ($throwError) {
  92. throw new \UnexpectedValueException('Config parameter does not exist');
  93. }
  94. } elseif ($throwError) {
  95. throw new \UnexpectedValueException('Config parameter does not exist');
  96. }
  97. return $currentValue;
  98. }
  99. }