Disable.php 3.1 KB

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