ShowCommand.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. <?php
  2. /*
  3. * This file is part of Composer.
  4. *
  5. * (c) Nils Adermann <naderman@naderman.de>
  6. * Jordi Boggiano <j.boggiano@seld.be>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Composer\Command;
  12. use Composer\DependencyResolver\Pool;
  13. use Composer\DependencyResolver\DefaultPolicy;
  14. use Composer\Factory;
  15. use Composer\Package\CompletePackageInterface;
  16. use Composer\Package\Version\VersionParser;
  17. use Composer\Plugin\CommandEvent;
  18. use Composer\Plugin\PluginEvents;
  19. use Symfony\Component\Console\Input\InputInterface;
  20. use Symfony\Component\Console\Input\InputArgument;
  21. use Symfony\Component\Console\Input\InputOption;
  22. use Symfony\Component\Console\Output\OutputInterface;
  23. use Composer\Repository\ArrayRepository;
  24. use Composer\Repository\CompositeRepository;
  25. use Composer\Repository\ComposerRepository;
  26. use Composer\Repository\PlatformRepository;
  27. use Composer\Repository\RepositoryInterface;
  28. /**
  29. * @author Robert Schönthal <seroscho@googlemail.com>
  30. * @author Jordi Boggiano <j.boggiano@seld.be>
  31. */
  32. class ShowCommand extends Command
  33. {
  34. protected $versionParser;
  35. protected function configure()
  36. {
  37. $this
  38. ->setName('show')
  39. ->setDescription('Show information about packages')
  40. ->setDefinition(array(
  41. new InputArgument('package', InputArgument::OPTIONAL, 'Package to inspect'),
  42. new InputArgument('version', InputArgument::OPTIONAL, 'Version or version constraint to inspect'),
  43. new InputOption('installed', 'i', InputOption::VALUE_NONE, 'List installed packages only'),
  44. new InputOption('platform', 'p', InputOption::VALUE_NONE, 'List platform packages only'),
  45. new InputOption('available', 'a', InputOption::VALUE_NONE, 'List available packages only'),
  46. new InputOption('self', 's', InputOption::VALUE_NONE, 'Show the root package information'),
  47. new InputOption('name-only', 'N', InputOption::VALUE_NONE, 'List package names only'),
  48. ))
  49. ->setHelp(<<<EOT
  50. The show command displays detailed information about a package, or
  51. lists all packages available.
  52. EOT
  53. )
  54. ;
  55. }
  56. protected function execute(InputInterface $input, OutputInterface $output)
  57. {
  58. $this->versionParser = new VersionParser;
  59. // init repos
  60. $platformRepo = new PlatformRepository;
  61. $composer = $this->getComposer(false);
  62. if ($input->getOption('self')) {
  63. $package = $this->getComposer()->getPackage();
  64. $repos = $installedRepo = new ArrayRepository(array($package));
  65. } elseif ($input->getOption('platform')) {
  66. $repos = $installedRepo = $platformRepo;
  67. } elseif ($input->getOption('installed')) {
  68. $repos = $installedRepo = $this->getComposer()->getRepositoryManager()->getLocalRepository();
  69. } elseif ($input->getOption('available')) {
  70. $installedRepo = $platformRepo;
  71. if ($composer) {
  72. $repos = new CompositeRepository($composer->getRepositoryManager()->getRepositories());
  73. } else {
  74. $defaultRepos = Factory::createDefaultRepositories($this->getIO());
  75. $repos = new CompositeRepository($defaultRepos);
  76. $output->writeln('No composer.json found in the current directory, showing available packages from ' . implode(', ', array_keys($defaultRepos)));
  77. }
  78. } elseif ($composer) {
  79. $localRepo = $composer->getRepositoryManager()->getLocalRepository();
  80. $installedRepo = new CompositeRepository(array($localRepo, $platformRepo));
  81. $repos = new CompositeRepository(array_merge(array($installedRepo), $composer->getRepositoryManager()->getRepositories()));
  82. } else {
  83. $defaultRepos = Factory::createDefaultRepositories($this->getIO());
  84. $output->writeln('No composer.json found in the current directory, showing available packages from ' . implode(', ', array_keys($defaultRepos)));
  85. $installedRepo = $platformRepo;
  86. $repos = new CompositeRepository(array_merge(array($installedRepo), $defaultRepos));
  87. }
  88. if ($composer) {
  89. $commandEvent = new CommandEvent(PluginEvents::COMMAND, 'show', $input, $output);
  90. $composer->getEventDispatcher()->dispatch($commandEvent->getName(), $commandEvent);
  91. }
  92. // show single package or single version
  93. if ($input->getArgument('package') || !empty($package)) {
  94. $versions = array();
  95. if (empty($package)) {
  96. list($package, $versions) = $this->getPackage($installedRepo, $repos, $input->getArgument('package'), $input->getArgument('version'));
  97. if (!$package) {
  98. throw new \InvalidArgumentException('Package '.$input->getArgument('package').' not found');
  99. }
  100. } else {
  101. $versions = array($package->getPrettyVersion() => $package->getVersion());
  102. }
  103. $this->printMeta($input, $output, $package, $versions, $installedRepo, $repos);
  104. $this->printLinks($input, $output, $package, 'requires');
  105. $this->printLinks($input, $output, $package, 'devRequires', 'requires (dev)');
  106. if ($package->getSuggests()) {
  107. $output->writeln("\n<info>suggests</info>");
  108. foreach ($package->getSuggests() as $suggested => $reason) {
  109. $output->writeln($suggested . ' <comment>' . $reason . '</comment>');
  110. }
  111. }
  112. $this->printLinks($input, $output, $package, 'provides');
  113. $this->printLinks($input, $output, $package, 'conflicts');
  114. $this->printLinks($input, $output, $package, 'replaces');
  115. return;
  116. }
  117. // list packages
  118. $packages = array();
  119. if ($repos instanceof CompositeRepository) {
  120. $repos = $repos->getRepositories();
  121. } elseif (!is_array($repos)) {
  122. $repos = array($repos);
  123. }
  124. foreach ($repos as $repo) {
  125. if ($repo === $platformRepo) {
  126. $type = '<info>platform</info>:';
  127. } elseif (
  128. $repo === $installedRepo
  129. || ($installedRepo instanceof CompositeRepository && in_array($repo, $installedRepo->getRepositories(), true))
  130. ) {
  131. $type = '<info>installed</info>:';
  132. } else {
  133. $type = '<comment>available</comment>:';
  134. }
  135. if ($repo instanceof ComposerRepository && $repo->hasProviders()) {
  136. foreach ($repo->getProviderNames() as $name) {
  137. $packages[$type][$name] = $name;
  138. }
  139. } else {
  140. foreach ($repo->getPackages() as $package) {
  141. if (!isset($packages[$type][$package->getName()])
  142. || !is_object($packages[$type][$package->getName()])
  143. || version_compare($packages[$type][$package->getName()]->getVersion(), $package->getVersion(), '<')
  144. ) {
  145. $packages[$type][$package->getName()] = $package;
  146. }
  147. }
  148. }
  149. }
  150. $tree = !$input->getOption('platform') && !$input->getOption('installed') && !$input->getOption('available');
  151. $indent = $tree ? ' ' : '';
  152. foreach (array('<info>platform</info>:' => true, '<comment>available</comment>:' => false, '<info>installed</info>:' => true) as $type => $showVersion) {
  153. if (isset($packages[$type])) {
  154. if ($tree) {
  155. $output->writeln($type);
  156. }
  157. ksort($packages[$type]);
  158. $nameLength = $versionLength = 0;
  159. foreach ($packages[$type] as $package) {
  160. if (is_object($package)) {
  161. $nameLength = max($nameLength, strlen($package->getPrettyName()));
  162. $versionLength = max($versionLength, strlen($this->versionParser->formatVersion($package)));
  163. } else {
  164. $nameLength = max($nameLength, $package);
  165. }
  166. }
  167. list($width) = $this->getApplication()->getTerminalDimensions();
  168. if (null === $width) {
  169. // In case the width is not detected, we're probably running the command
  170. // outside of a real terminal, use space without a limit
  171. $width = PHP_INT_MAX;
  172. }
  173. if (defined('PHP_WINDOWS_VERSION_BUILD')) {
  174. $width--;
  175. }
  176. $writeVersion = !$input->getOption('name-only') && $showVersion && ($nameLength + $versionLength + 3 <= $width);
  177. $writeDescription = !$input->getOption('name-only') && ($nameLength + ($showVersion ? $versionLength : 0) + 24 <= $width);
  178. foreach ($packages[$type] as $package) {
  179. if (is_object($package)) {
  180. $output->write($indent . str_pad($package->getPrettyName(), $nameLength, ' '), false);
  181. if ($writeVersion) {
  182. $output->write(' ' . str_pad($this->versionParser->formatVersion($package), $versionLength, ' '), false);
  183. }
  184. if ($writeDescription) {
  185. $description = strtok($package->getDescription(), "\r\n");
  186. $remaining = $width - $nameLength - $versionLength - 4;
  187. if (strlen($description) > $remaining) {
  188. $description = substr($description, 0, $remaining - 3) . '...';
  189. }
  190. $output->write(' ' . $description);
  191. }
  192. } else {
  193. $output->write($indent . $package);
  194. }
  195. $output->writeln('');
  196. }
  197. if ($tree) {
  198. $output->writeln('');
  199. }
  200. }
  201. }
  202. }
  203. /**
  204. * finds a package by name and version if provided
  205. *
  206. * @param RepositoryInterface $installedRepo
  207. * @param RepositoryInterface $repos
  208. * @param string $name
  209. * @param string $version
  210. * @return array array(CompletePackageInterface, array of versions)
  211. * @throws \InvalidArgumentException
  212. */
  213. protected function getPackage(RepositoryInterface $installedRepo, RepositoryInterface $repos, $name, $version = null)
  214. {
  215. $name = strtolower($name);
  216. $constraint = null;
  217. if ($version) {
  218. $constraint = $this->versionParser->parseConstraints($version);
  219. }
  220. $policy = new DefaultPolicy();
  221. $pool = new Pool('dev');
  222. $pool->addRepository($repos);
  223. $matchedPackage = null;
  224. $versions = array();
  225. $matches = $pool->whatProvides($name, $constraint);
  226. foreach ($matches as $index => $package) {
  227. // skip providers/replacers
  228. if ($package->getName() !== $name) {
  229. unset($matches[$index]);
  230. continue;
  231. }
  232. // select an exact match if it is in the installed repo and no specific version was required
  233. if (null === $version && $installedRepo->hasPackage($package)) {
  234. $matchedPackage = $package;
  235. }
  236. $versions[$package->getPrettyVersion()] = $package->getVersion();
  237. $matches[$index] = $package->getId();
  238. }
  239. // select prefered package according to policy rules
  240. if (!$matchedPackage && $matches && $prefered = $policy->selectPreferedPackages($pool, array(), $matches)) {
  241. $matchedPackage = $pool->literalToPackage($prefered[0]);
  242. }
  243. return array($matchedPackage, $versions);
  244. }
  245. /**
  246. * prints package meta data
  247. */
  248. protected function printMeta(InputInterface $input, OutputInterface $output, CompletePackageInterface $package, array $versions, RepositoryInterface $installedRepo, RepositoryInterface $repos)
  249. {
  250. $output->writeln('<info>name</info> : ' . $package->getPrettyName());
  251. $output->writeln('<info>descrip.</info> : ' . $package->getDescription());
  252. $output->writeln('<info>keywords</info> : ' . join(', ', $package->getKeywords() ?: array()));
  253. $this->printVersions($input, $output, $package, $versions, $installedRepo, $repos);
  254. $output->writeln('<info>type</info> : ' . $package->getType());
  255. $output->writeln('<info>license</info> : ' . implode(', ', $package->getLicense()));
  256. $output->writeln('<info>source</info> : ' . sprintf('[%s] <comment>%s</comment> %s', $package->getSourceType(), $package->getSourceUrl(), $package->getSourceReference()));
  257. $output->writeln('<info>dist</info> : ' . sprintf('[%s] <comment>%s</comment> %s', $package->getDistType(), $package->getDistUrl(), $package->getDistReference()));
  258. $output->writeln('<info>names</info> : ' . implode(', ', $package->getNames()));
  259. if ($package->getSupport()) {
  260. $output->writeln("\n<info>support</info>");
  261. foreach ($package->getSupport() as $type => $value) {
  262. $output->writeln('<comment>' . $type . '</comment> : '.$value);
  263. }
  264. }
  265. if ($package->getAutoload()) {
  266. $output->writeln("\n<info>autoload</info>");
  267. foreach ($package->getAutoload() as $type => $autoloads) {
  268. $output->writeln('<comment>' . $type . '</comment>');
  269. if ($type === 'psr-0') {
  270. foreach ($autoloads as $name => $path) {
  271. $output->writeln(($name ?: '*') . ' => ' . (is_array($path) ? implode(', ', $path) : ($path ?: '.')));
  272. }
  273. } elseif ($type === 'psr-4') {
  274. foreach ($autoloads as $name => $path) {
  275. $output->writeln(($name ?: '*') . ' => ' . (is_array($path) ? implode(', ', $path) : ($path ?: '.')));
  276. }
  277. } elseif ($type === 'classmap') {
  278. $output->writeln(implode(', ', $autoloads));
  279. }
  280. }
  281. if ($package->getIncludePaths()) {
  282. $output->writeln('<comment>include-path</comment>');
  283. $output->writeln(implode(', ', $package->getIncludePaths()));
  284. }
  285. }
  286. }
  287. /**
  288. * prints all available versions of this package and highlights the installed one if any
  289. */
  290. protected function printVersions(InputInterface $input, OutputInterface $output, CompletePackageInterface $package, array $versions, RepositoryInterface $installedRepo, RepositoryInterface $repos)
  291. {
  292. uasort($versions, 'version_compare');
  293. $versions = array_keys(array_reverse($versions));
  294. // highlight installed version
  295. if ($installedRepo->hasPackage($package)) {
  296. $installedVersion = $package->getPrettyVersion();
  297. $key = array_search($installedVersion, $versions);
  298. if (false !== $key) {
  299. $versions[$key] = '<info>* ' . $installedVersion . '</info>';
  300. }
  301. }
  302. $versions = implode(', ', $versions);
  303. $output->writeln('<info>versions</info> : ' . $versions);
  304. }
  305. /**
  306. * print link objects
  307. *
  308. * @param InputInterface $input
  309. * @param OutputInterface $output
  310. * @param CompletePackageInterface $package
  311. * @param string $linkType
  312. * @param string $title
  313. */
  314. protected function printLinks(InputInterface $input, OutputInterface $output, CompletePackageInterface $package, $linkType, $title = null)
  315. {
  316. $title = $title ?: $linkType;
  317. if ($links = $package->{'get'.ucfirst($linkType)}()) {
  318. $output->writeln("\n<info>" . $title . "</info>");
  319. foreach ($links as $link) {
  320. $output->writeln($link->getTarget() . ' <comment>' . $link->getPrettyConstraint() . '</comment>');
  321. }
  322. }
  323. }
  324. }