ListApps.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. * @author Robin Appelman <robin@icewind.nl>
  8. * @author Victor Dubiniuk <dubiniuk@owncloud.com>
  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 OC\Core\Command\Base;
  27. use OCP\App\IAppManager;
  28. use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext;
  29. use Symfony\Component\Console\Input\InputInterface;
  30. use Symfony\Component\Console\Input\InputOption;
  31. use Symfony\Component\Console\Output\OutputInterface;
  32. class ListApps extends Base {
  33. /** @var IAppManager */
  34. protected $manager;
  35. /**
  36. * @param IAppManager $manager
  37. */
  38. public function __construct(IAppManager $manager) {
  39. parent::__construct();
  40. $this->manager = $manager;
  41. }
  42. protected function configure() {
  43. parent::configure();
  44. $this
  45. ->setName('app:list')
  46. ->setDescription('List all available apps')
  47. ->addOption(
  48. 'shipped',
  49. null,
  50. InputOption::VALUE_REQUIRED,
  51. 'true - limit to shipped apps only, false - limit to non-shipped apps only'
  52. )
  53. ;
  54. }
  55. protected function execute(InputInterface $input, OutputInterface $output) {
  56. if ($input->getOption('shipped') === 'true' || $input->getOption('shipped') === 'false'){
  57. $shippedFilter = $input->getOption('shipped') === 'true';
  58. } else {
  59. $shippedFilter = null;
  60. }
  61. $apps = \OC_App::getAllApps();
  62. $enabledApps = $disabledApps = [];
  63. $versions = \OC_App::getAppVersions();
  64. //sort enabled apps above disabled apps
  65. foreach ($apps as $app) {
  66. if ($shippedFilter !== null && $this->manager->isShipped($app) !== $shippedFilter){
  67. continue;
  68. }
  69. if ($this->manager->isInstalled($app)) {
  70. $enabledApps[] = $app;
  71. } else {
  72. $disabledApps[] = $app;
  73. }
  74. }
  75. $apps = ['enabled' => [], 'disabled' => []];
  76. sort($enabledApps);
  77. foreach ($enabledApps as $app) {
  78. $apps['enabled'][$app] = isset($versions[$app]) ? $versions[$app] : true;
  79. }
  80. sort($disabledApps);
  81. foreach ($disabledApps as $app) {
  82. $apps['disabled'][$app] = null;
  83. }
  84. $this->writeAppList($input, $output, $apps);
  85. }
  86. /**
  87. * @param InputInterface $input
  88. * @param OutputInterface $output
  89. * @param array $items
  90. */
  91. protected function writeAppList(InputInterface $input, OutputInterface $output, $items) {
  92. switch ($input->getOption('output')) {
  93. case self::OUTPUT_FORMAT_PLAIN:
  94. $output->writeln('Enabled:');
  95. parent::writeArrayInOutputFormat($input, $output, $items['enabled']);
  96. $output->writeln('Disabled:');
  97. parent::writeArrayInOutputFormat($input, $output, $items['disabled']);
  98. break;
  99. default:
  100. parent::writeArrayInOutputFormat($input, $output, $items);
  101. break;
  102. }
  103. }
  104. /**
  105. * @param string $optionName
  106. * @param CompletionContext $completionContext
  107. * @return array
  108. */
  109. public function completeOptionValues($optionName, CompletionContext $completionContext) {
  110. if ($optionName === 'shipped') {
  111. return ['true', 'false'];
  112. }
  113. return [];
  114. }
  115. /**
  116. * @param string $argumentName
  117. * @param CompletionContext $context
  118. * @return string[]
  119. */
  120. public function completeArgumentValues($argumentName, CompletionContext $context) {
  121. return [];
  122. }
  123. }