DeleteConfig.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. *
  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\SystemConfig;
  24. use Symfony\Component\Console\Input\InputArgument;
  25. use Symfony\Component\Console\Input\InputInterface;
  26. use Symfony\Component\Console\Input\InputOption;
  27. use Symfony\Component\Console\Output\OutputInterface;
  28. class DeleteConfig extends Base {
  29. /** * @var SystemConfig */
  30. protected $systemConfig;
  31. /**
  32. * @param SystemConfig $systemConfig
  33. */
  34. public function __construct(SystemConfig $systemConfig) {
  35. parent::__construct();
  36. $this->systemConfig = $systemConfig;
  37. }
  38. protected function configure() {
  39. parent::configure();
  40. $this
  41. ->setName('config:system:delete')
  42. ->setDescription('Delete a system config value')
  43. ->addArgument(
  44. 'name',
  45. InputArgument::REQUIRED | InputArgument::IS_ARRAY,
  46. 'Name of the config to delete, specify multiple for array parameter'
  47. )
  48. ->addOption(
  49. 'error-if-not-exists',
  50. null,
  51. InputOption::VALUE_NONE,
  52. 'Checks whether the config exists before deleting it'
  53. )
  54. ;
  55. }
  56. protected function execute(InputInterface $input, OutputInterface $output) {
  57. $configNames = $input->getArgument('name');
  58. $configName = $configNames[0];
  59. if (count($configNames) > 1) {
  60. if ($input->hasParameterOption('--error-if-not-exists') && !in_array($configName, $this->systemConfig->getKeys())) {
  61. $output->writeln('<error>System config ' . implode(' => ', $configNames) . ' could not be deleted because it did not exist</error>');
  62. return 1;
  63. }
  64. $value = $this->systemConfig->getValue($configName);
  65. try {
  66. $value = $this->removeSubValue(array_slice($configNames, 1), $value, $input->hasParameterOption('--error-if-not-exists'));
  67. }
  68. catch (\UnexpectedValueException $e) {
  69. $output->writeln('<error>System config ' . implode(' => ', $configNames) . ' could not be deleted because it did not exist</error>');
  70. return 1;
  71. }
  72. $this->systemConfig->setValue($configName, $value);
  73. $output->writeln('<info>System config value ' . implode(' => ', $configNames) . ' deleted</info>');
  74. return 0;
  75. } else {
  76. if ($input->hasParameterOption('--error-if-not-exists') && !in_array($configName, $this->systemConfig->getKeys())) {
  77. $output->writeln('<error>System config ' . $configName . ' could not be deleted because it did not exist</error>');
  78. return 1;
  79. }
  80. $this->systemConfig->deleteValue($configName);
  81. $output->writeln('<info>System config value ' . $configName . ' deleted</info>');
  82. return 0;
  83. }
  84. }
  85. protected function removeSubValue($keys, $currentValue, $throwError) {
  86. $nextKey = array_shift($keys);
  87. if (is_array($currentValue)) {
  88. if (isset($currentValue[$nextKey])) {
  89. if (empty($keys)) {
  90. unset($currentValue[$nextKey]);
  91. } else {
  92. $currentValue[$nextKey] = $this->removeSubValue($keys, $currentValue[$nextKey], $throwError);
  93. }
  94. } else if ($throwError) {
  95. throw new \UnexpectedValueException('Config parameter does not exist');
  96. }
  97. } else if ($throwError) {
  98. throw new \UnexpectedValueException('Config parameter does not exist');
  99. }
  100. return $currentValue;
  101. }
  102. }