Disable.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Daniel Kesselberg <mail@danielkesselberg.de>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author John Molakvoæ <skjnldsv@protonmail.com>
  8. * @author Robin Appelman <robin@icewind.nl>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OC\Core\Command\App;
  26. use OCP\App\IAppManager;
  27. use Stecman\Component\Symfony\Console\BashCompletion\Completion\CompletionAwareInterface;
  28. use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext;
  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\Output\OutputInterface;
  33. class Disable extends Command implements CompletionAwareInterface {
  34. protected IAppManager $appManager;
  35. protected int $exitCode = 0;
  36. public function __construct(IAppManager $appManager) {
  37. parent::__construct();
  38. $this->appManager = $appManager;
  39. }
  40. protected function configure(): void {
  41. $this
  42. ->setName('app:disable')
  43. ->setDescription('disable an app')
  44. ->addArgument(
  45. 'app-id',
  46. InputArgument::REQUIRED | InputArgument::IS_ARRAY,
  47. 'disable the specified app'
  48. );
  49. }
  50. protected function execute(InputInterface $input, OutputInterface $output): int {
  51. $appIds = $input->getArgument('app-id');
  52. foreach ($appIds as $appId) {
  53. $this->disableApp($appId, $output);
  54. }
  55. return $this->exitCode;
  56. }
  57. private function disableApp(string $appId, OutputInterface $output): void {
  58. if ($this->appManager->isInstalled($appId) === false) {
  59. $output->writeln('No such app enabled: ' . $appId);
  60. return;
  61. }
  62. try {
  63. $this->appManager->disableApp($appId);
  64. $appVersion = \OC_App::getAppVersion($appId);
  65. $output->writeln($appId . ' ' . $appVersion . ' disabled');
  66. } catch (\Exception $e) {
  67. $output->writeln($e->getMessage());
  68. $this->exitCode = 2;
  69. }
  70. }
  71. /**
  72. * @param string $optionName
  73. * @param CompletionContext $context
  74. * @return string[]
  75. */
  76. public function completeOptionValues($optionName, CompletionContext $context) {
  77. return [];
  78. }
  79. /**
  80. * @param string $argumentName
  81. * @param CompletionContext $context
  82. * @return string[]
  83. */
  84. public function completeArgumentValues($argumentName, CompletionContext $context) {
  85. if ($argumentName === 'app-id') {
  86. return array_diff(\OC_App::getEnabledApps(true, true), $this->appManager->getAlwaysEnabledApps());
  87. }
  88. return [];
  89. }
  90. }