DeleteConfig.php 3.7 KB

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