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 */
25namespace OC\Core\Command\App;
26
27use OCP\App\IAppManager;
28use Stecman\Component\Symfony\Console\BashCompletion\Completion\CompletionAwareInterface;
29use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext;
30use Symfony\Component\Console\Command\Command;
31use Symfony\Component\Console\Input\InputArgument;
32use Symfony\Component\Console\Input\InputInterface;
33use Symfony\Component\Console\Output\OutputInterface;
34
35class Disable extends Command implements CompletionAwareInterface {
36
37	/** @var IAppManager */
38	protected $appManager;
39
40	/** @var int */
41	protected $exitCode = 0;
42
43	/**
44	 * @param IAppManager $appManager
45	 */
46	public function __construct(IAppManager $appManager) {
47		parent::__construct();
48		$this->appManager = $appManager;
49	}
50
51	protected function configure(): void {
52		$this
53			->setName('app:disable')
54			->setDescription('disable an app')
55			->addArgument(
56				'app-id',
57				InputArgument::REQUIRED | InputArgument::IS_ARRAY,
58				'disable the specified app'
59			);
60	}
61
62	protected function execute(InputInterface $input, OutputInterface $output): int {
63		$appIds = $input->getArgument('app-id');
64
65		foreach ($appIds as $appId) {
66			$this->disableApp($appId, $output);
67		}
68
69		return $this->exitCode;
70	}
71
72	private function disableApp(string $appId, OutputInterface $output): void {
73		if ($this->appManager->isInstalled($appId) === false) {
74			$output->writeln('No such app enabled: ' . $appId);
75			return;
76		}
77
78		try {
79			$this->appManager->disableApp($appId);
80			$appVersion = \OC_App::getAppVersion($appId);
81			$output->writeln($appId . ' ' . $appVersion . ' disabled');
82		} catch (\Exception $e) {
83			$output->writeln($e->getMessage());
84			$this->exitCode = 2;
85		}
86	}
87
88	/**
89	 * @param string $optionName
90	 * @param CompletionContext $context
91	 * @return string[]
92	 */
93	public function completeOptionValues($optionName, CompletionContext $context) {
94		return [];
95	}
96
97	/**
98	 * @param string $argumentName
99	 * @param CompletionContext $context
100	 * @return string[]
101	 */
102	public function completeArgumentValues($argumentName, CompletionContext $context) {
103		if ($argumentName === 'app-id') {
104			return array_diff(\OC_App::getEnabledApps(true, true), $this->appManager->getAlwaysEnabledApps());
105		}
106		return [];
107	}
108}
109