UpdateConfig.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\Theming\Command;
  7. use OCA\Theming\ImageManager;
  8. use OCA\Theming\ThemingDefaults;
  9. use OCP\IConfig;
  10. use Symfony\Component\Console\Command\Command;
  11. use Symfony\Component\Console\Input\InputArgument;
  12. use Symfony\Component\Console\Input\InputInterface;
  13. use Symfony\Component\Console\Input\InputOption;
  14. use Symfony\Component\Console\Output\OutputInterface;
  15. class UpdateConfig extends Command {
  16. public const SUPPORTED_KEYS = [
  17. 'name', 'url', 'imprintUrl', 'privacyUrl', 'slogan', 'color', 'primary_color', 'disable-user-theming'
  18. ];
  19. public function __construct(
  20. private ThemingDefaults $themingDefaults,
  21. private ImageManager $imageManager,
  22. private IConfig $config,
  23. ) {
  24. parent::__construct();
  25. }
  26. protected function configure() {
  27. $this
  28. ->setName('theming:config')
  29. ->setDescription('Set theming app config values')
  30. ->addArgument(
  31. 'key',
  32. InputArgument::OPTIONAL,
  33. 'Key to update the theming app configuration (leave empty to get a list of all configured values)' . PHP_EOL .
  34. 'One of: ' . implode(', ', self::SUPPORTED_KEYS)
  35. )
  36. ->addArgument(
  37. 'value',
  38. InputArgument::OPTIONAL,
  39. 'Value to set (leave empty to obtain the current value)'
  40. )
  41. ->addOption(
  42. 'reset',
  43. 'r',
  44. InputOption::VALUE_NONE,
  45. 'Reset the given config key to default'
  46. );
  47. }
  48. protected function execute(InputInterface $input, OutputInterface $output): int {
  49. $key = $input->getArgument('key');
  50. $value = $input->getArgument('value');
  51. assert(is_string($value) || $value === null, 'At most one value should be provided.');
  52. if ($key === null) {
  53. $output->writeln('Current theming config:');
  54. foreach (self::SUPPORTED_KEYS as $key) {
  55. $value = $this->config->getAppValue('theming', $key, '');
  56. $output->writeln('- ' . $key . ': ' . $value . '');
  57. }
  58. foreach (ImageManager::SUPPORTED_IMAGE_KEYS as $key) {
  59. $value = $this->config->getAppValue('theming', $key . 'Mime', '');
  60. $output->writeln('- ' . $key . ': ' . $value . '');
  61. }
  62. return 0;
  63. }
  64. if (!in_array($key, self::SUPPORTED_KEYS, true) && !in_array($key, ImageManager::SUPPORTED_IMAGE_KEYS, true)) {
  65. $output->writeln('<error>Invalid config key provided</error>');
  66. return 1;
  67. }
  68. if ($input->getOption('reset')) {
  69. $defaultValue = $this->themingDefaults->undo($key);
  70. $output->writeln('<info>Reset ' . $key . ' to ' . $defaultValue . '</info>');
  71. return 0;
  72. }
  73. if ($value === null) {
  74. $value = $this->config->getAppValue('theming', $key, '');
  75. if ($value !== '') {
  76. $output->writeln('<info>' . $key . ' is currently set to ' . $value . '</info>');
  77. } else {
  78. $output->writeln('<info>' . $key . ' is currently not set</info>');
  79. }
  80. return 0;
  81. }
  82. if ($key === 'background' && $value === 'backgroundColor') {
  83. $this->themingDefaults->undo($key);
  84. $key = $key . 'Mime';
  85. }
  86. if (in_array($key, ImageManager::SUPPORTED_IMAGE_KEYS, true)) {
  87. if (!str_starts_with($value, '/')) {
  88. $output->writeln('<error>The image file needs to be provided as an absolute path: ' . $value . '.</error>');
  89. return 1;
  90. }
  91. if (!file_exists($value)) {
  92. $output->writeln('<error>File could not be found: ' . $value . '.</error>');
  93. return 1;
  94. }
  95. $value = $this->imageManager->updateImage($key, $value);
  96. $key = $key . 'Mime';
  97. }
  98. if ($key === 'color') {
  99. $output->writeln('<comment>Using "color" is deprecated, use "primary_color" instead</comment>');
  100. $key = 'primary_color';
  101. }
  102. if ($key === 'primary_color' && !preg_match('/^\#([0-9a-f]{3}|[0-9a-f]{6})$/i', $value)) {
  103. $output->writeln('<error>The given color is invalid: ' . $value . '</error>');
  104. return 1;
  105. }
  106. $this->themingDefaults->set($key, $value);
  107. $output->writeln('<info>Updated ' . $key . ' to ' . $value . '</info>');
  108. return 0;
  109. }
  110. }