|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * @copyright Copyright (c) 2020 Julius Härtl <jus@bitgrid.net> |
| 4 | + * |
| 5 | + * @author Julius Härtl <jus@bitgrid.net> |
| 6 | + * |
| 7 | + * @license GNU AGPL version 3 or any later version |
| 8 | + * |
| 9 | + * This program is free software: you can redistribute it and/or modify |
| 10 | + * it under the terms of the GNU Affero General Public License as |
| 11 | + * published by the Free Software Foundation, either version 3 of the |
| 12 | + * License, or (at your option) any later version. |
| 13 | + * |
| 14 | + * This program is distributed in the hope that it will be useful, |
| 15 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | + * GNU Affero General Public License for more details. |
| 18 | + * |
| 19 | + * You should have received a copy of the GNU Affero General Public License |
| 20 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 21 | + * |
| 22 | + */ |
| 23 | + |
| 24 | +namespace OCA\Theming\Command; |
| 25 | + |
| 26 | +use OCA\Theming\ImageManager; |
| 27 | +use OCA\Theming\ThemingDefaults; |
| 28 | +use OCP\IConfig; |
| 29 | +use Symfony\Component\Console\Command\Command; |
| 30 | +use Symfony\Component\Console\Input\InputArgument; |
| 31 | +use Symfony\Component\Console\Input\InputInterface; |
| 32 | +use Symfony\Component\Console\Input\InputOption; |
| 33 | +use Symfony\Component\Console\Output\OutputInterface; |
| 34 | + |
| 35 | +class UpdateConfig extends Command { |
| 36 | + public const SUPPORTED_KEYS = [ |
| 37 | + 'name', 'url', 'imprintUrl', 'privacyUrl', 'slogan', 'color' |
| 38 | + ]; |
| 39 | + |
| 40 | + public const SUPPORTED_IMAGE_KEYS = [ |
| 41 | + 'background', 'logo', 'favicon', 'logoheader' |
| 42 | + ]; |
| 43 | + |
| 44 | + private $themingDefaults; |
| 45 | + private $imageManager; |
| 46 | + private $config; |
| 47 | + |
| 48 | + public function __construct(ThemingDefaults $themingDefaults, ImageManager $imageManager, IConfig $config) { |
| 49 | + parent::__construct(); |
| 50 | + |
| 51 | + $this->themingDefaults = $themingDefaults; |
| 52 | + $this->imageManager = $imageManager; |
| 53 | + $this->config = $config; |
| 54 | + } |
| 55 | + |
| 56 | + protected function configure() { |
| 57 | + $this |
| 58 | + ->setName('theming:config') |
| 59 | + ->setDescription('Set theming app config values') |
| 60 | + ->addArgument( |
| 61 | + 'key', |
| 62 | + InputArgument::OPTIONAL, |
| 63 | + 'Key to update the theming app configuration (leave empty to get a list of all configured values)' . PHP_EOL . |
| 64 | + 'One of: ' . implode(', ', self::SUPPORTED_KEYS) |
| 65 | + ) |
| 66 | + ->addArgument( |
| 67 | + 'value', |
| 68 | + InputArgument::OPTIONAL, |
| 69 | + 'Value to set (leave empty to obtain the current value)' |
| 70 | + ) |
| 71 | + ->addOption( |
| 72 | + 'reset', |
| 73 | + 'r', |
| 74 | + InputOption::VALUE_NONE, |
| 75 | + 'Reset the given config key to default' |
| 76 | + ); |
| 77 | + } |
| 78 | + |
| 79 | + |
| 80 | + protected function execute(InputInterface $input, OutputInterface $output): int { |
| 81 | + $key = $input->getArgument('key'); |
| 82 | + $value = $input->getArgument('value'); |
| 83 | + |
| 84 | + if ($key === null) { |
| 85 | + $output->writeln('Current theming config:'); |
| 86 | + foreach (self::SUPPORTED_KEYS as $key) { |
| 87 | + $value = $this->config->getAppValue('theming', $key, ''); |
| 88 | + $output->writeln('- ' . $key . ': ' . $value . ''); |
| 89 | + } |
| 90 | + foreach (self::SUPPORTED_IMAGE_KEYS as $key) { |
| 91 | + $value = $this->config->getAppValue('theming', $key . 'Mime', ''); |
| 92 | + $output->writeln('- ' . $key . ': ' . $value . ''); |
| 93 | + } |
| 94 | + return 0; |
| 95 | + } |
| 96 | + |
| 97 | + if (!in_array($key, self::SUPPORTED_KEYS, true)) { |
| 98 | + $output->writeln('<error>Invalid config key provided</error>'); |
| 99 | + return 1; |
| 100 | + } |
| 101 | + |
| 102 | + if ($input->getOption('reset')) { |
| 103 | + $defaultValue = $this->themingDefaults->undo($key); |
| 104 | + $output->writeln('<info>Reset ' . $key . ' to ' . $defaultValue . '</info>'); |
| 105 | + return 0; |
| 106 | + } |
| 107 | + |
| 108 | + if ($value === null) { |
| 109 | + $value = $this->config->getAppValue('theming', $key, ''); |
| 110 | + if ($value !== '') { |
| 111 | + $output->writeln('<info>' . $key . ' is currently set to ' . $value . '</info>'); |
| 112 | + } else { |
| 113 | + $output->writeln('<info>' . $key . ' is currently not set</info>'); |
| 114 | + } |
| 115 | + return 0; |
| 116 | + } |
| 117 | + |
| 118 | + if (in_array($key, self::SUPPORTED_IMAGE_KEYS, true)) { |
| 119 | + if (file_exists(__DIR__ . $value)) { |
| 120 | + $value = __DIR__ . $value; |
| 121 | + } |
| 122 | + if (!file_exists($value)) { |
| 123 | + $output->writeln('<error>File could not be found: ' . $value . '</error>'); |
| 124 | + return 1; |
| 125 | + } |
| 126 | + $value = $this->imageManager->updateImage($key, $value); |
| 127 | + $key = $key . 'Mime'; |
| 128 | + } |
| 129 | + |
| 130 | + $this->themingDefaults->set($key, $value); |
| 131 | + $output->writeln('<info>Updated ' . $key . ' to ' . $value . '</info>'); |
| 132 | + |
| 133 | + return 0; |
| 134 | + } |
| 135 | +} |
0 commit comments