DeleteConfig.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OC\Core\Command\Config\System;
  8. use OC\SystemConfig;
  9. use Symfony\Component\Console\Input\InputArgument;
  10. use Symfony\Component\Console\Input\InputInterface;
  11. use Symfony\Component\Console\Input\InputOption;
  12. use Symfony\Component\Console\Output\OutputInterface;
  13. class DeleteConfig extends Base {
  14. public function __construct(
  15. SystemConfig $systemConfig,
  16. ) {
  17. parent::__construct($systemConfig);
  18. }
  19. protected function configure() {
  20. parent::configure();
  21. $this
  22. ->setName('config:system:delete')
  23. ->setDescription('Delete a system config value')
  24. ->addArgument(
  25. 'name',
  26. InputArgument::REQUIRED | InputArgument::IS_ARRAY,
  27. 'Name of the config to delete, specify multiple for array parameter'
  28. )
  29. ->addOption(
  30. 'error-if-not-exists',
  31. null,
  32. InputOption::VALUE_NONE,
  33. 'Checks whether the config exists before deleting it'
  34. )
  35. ;
  36. }
  37. protected function execute(InputInterface $input, OutputInterface $output): int {
  38. $configNames = $input->getArgument('name');
  39. $configName = $configNames[0];
  40. if (count($configNames) > 1) {
  41. if ($input->hasParameterOption('--error-if-not-exists') && !in_array($configName, $this->systemConfig->getKeys())) {
  42. $output->writeln('<error>System config ' . implode(' => ', $configNames) . ' could not be deleted because it did not exist</error>');
  43. return 1;
  44. }
  45. $value = $this->systemConfig->getValue($configName);
  46. try {
  47. $value = $this->removeSubValue(array_slice($configNames, 1), $value, $input->hasParameterOption('--error-if-not-exists'));
  48. } catch (\UnexpectedValueException $e) {
  49. $output->writeln('<error>System config ' . implode(' => ', $configNames) . ' could not be deleted because it did not exist</error>');
  50. return 1;
  51. }
  52. $this->systemConfig->setValue($configName, $value);
  53. $output->writeln('<info>System config value ' . implode(' => ', $configNames) . ' deleted</info>');
  54. return 0;
  55. } else {
  56. if ($input->hasParameterOption('--error-if-not-exists') && !in_array($configName, $this->systemConfig->getKeys())) {
  57. $output->writeln('<error>System config ' . $configName . ' could not be deleted because it did not exist</error>');
  58. return 1;
  59. }
  60. $this->systemConfig->deleteValue($configName);
  61. $output->writeln('<info>System config value ' . $configName . ' deleted</info>');
  62. return 0;
  63. }
  64. }
  65. protected function removeSubValue($keys, $currentValue, $throwError) {
  66. $nextKey = array_shift($keys);
  67. if (is_array($currentValue)) {
  68. if (isset($currentValue[$nextKey])) {
  69. if (empty($keys)) {
  70. unset($currentValue[$nextKey]);
  71. } else {
  72. $currentValue[$nextKey] = $this->removeSubValue($keys, $currentValue[$nextKey], $throwError);
  73. }
  74. } elseif ($throwError) {
  75. throw new \UnexpectedValueException('Config parameter does not exist');
  76. }
  77. } elseif ($throwError) {
  78. throw new \UnexpectedValueException('Config parameter does not exist');
  79. }
  80. return $currentValue;
  81. }
  82. }