ShowCommand.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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\Composer;
  13. use Composer\Factory;
  14. use Composer\Package\CompletePackageInterface;
  15. use Composer\Package\Version\VersionParser;
  16. use Symfony\Component\Console\Input\InputInterface;
  17. use Symfony\Component\Console\Input\InputArgument;
  18. use Symfony\Component\Console\Input\InputOption;
  19. use Symfony\Component\Console\Output\OutputInterface;
  20. use Composer\Repository\ArrayRepository;
  21. use Composer\Repository\CompositeRepository;
  22. use Composer\Repository\PlatformRepository;
  23. use Composer\Repository\RepositoryInterface;
  24. /**
  25. * @author Robert Schönthal <seroscho@googlemail.com>
  26. * @author Jordi Boggiano <j.boggiano@seld.be>
  27. */
  28. class ShowCommand extends Command
  29. {
  30. protected function configure()
  31. {
  32. $this
  33. ->setName('show')
  34. ->setDescription('Show information about packages')
  35. ->setDefinition(array(
  36. new InputArgument('package', InputArgument::OPTIONAL, 'Package to inspect'),
  37. new InputArgument('version', InputArgument::OPTIONAL, 'Version to inspect'),
  38. new InputOption('installed', 'i', InputOption::VALUE_NONE, 'List installed packages only'),
  39. new InputOption('platform', 'p', InputOption::VALUE_NONE, 'List platform packages only'),
  40. new InputOption('self', 's', InputOption::VALUE_NONE, 'Show the root package information'),
  41. ))
  42. ->setHelp(<<<EOT
  43. The show command displays detailed information about a package, or
  44. lists all packages available.
  45. EOT
  46. )
  47. ;
  48. }
  49. protected function execute(InputInterface $input, OutputInterface $output)
  50. {
  51. // init repos
  52. $platformRepo = new PlatformRepository;
  53. if ($input->getOption('self')) {
  54. $package = $this->getComposer(false)->getPackage();
  55. $repos = $installedRepo = new ArrayRepository(array($package));
  56. } elseif ($input->getOption('platform')) {
  57. $repos = $installedRepo = $platformRepo;
  58. } elseif ($input->getOption('installed')) {
  59. $composer = $this->getComposer();
  60. $repos = $installedRepo = $composer->getRepositoryManager()->getLocalRepository();
  61. } elseif ($composer = $this->getComposer(false)) {
  62. $localRepo = $composer->getRepositoryManager()->getLocalRepository();
  63. $installedRepo = new CompositeRepository(array($localRepo, $platformRepo));
  64. $repos = new CompositeRepository(array_merge(array($installedRepo), $composer->getRepositoryManager()->getRepositories()));
  65. } else {
  66. $defaultRepos = Factory::createDefaultRepositories($this->getIO());
  67. $output->writeln('No composer.json found in the current directory, showing packages from ' . implode(', ', array_keys($defaultRepos)));
  68. $installedRepo = $platformRepo;
  69. $repos = new CompositeRepository(array_merge(array($installedRepo), $defaultRepos));
  70. }
  71. // show single package or single version
  72. if ($input->getArgument('package') || !empty($package)) {
  73. $versions = array();
  74. if (empty($package)) {
  75. list($package, $versions) = $this->getPackage($installedRepo, $repos, $input->getArgument('package'), $input->getArgument('version'));
  76. if (!$package) {
  77. throw new \InvalidArgumentException('Package '.$input->getArgument('package').' not found');
  78. }
  79. }
  80. $this->printMeta($input, $output, $package, $versions, $installedRepo, $repos);
  81. $this->printLinks($input, $output, $package, 'requires');
  82. $this->printLinks($input, $output, $package, 'devRequires', 'requires (dev)');
  83. if ($package->getSuggests()) {
  84. $output->writeln("\n<info>suggests</info>");
  85. foreach ($package->getSuggests() as $suggested => $reason) {
  86. $output->writeln($suggested . ' <comment>' . $reason . '</comment>');
  87. }
  88. }
  89. $this->printLinks($input, $output, $package, 'provides');
  90. $this->printLinks($input, $output, $package, 'conflicts');
  91. $this->printLinks($input, $output, $package, 'replaces');
  92. return;
  93. }
  94. // list packages
  95. $packages = array();
  96. $repos->filterPackages(function ($package) use (&$packages, $platformRepo, $installedRepo) {
  97. if ($platformRepo->hasPackage($package)) {
  98. $type = '<info>platform</info>:';
  99. } elseif ($installedRepo->hasPackage($package)) {
  100. $type = '<info>installed</info>:';
  101. } else {
  102. $type = '<comment>available</comment>:';
  103. }
  104. if (!isset($packages[$type][$package->getName()])
  105. || version_compare($packages[$type][$package->getName()]->getVersion(), $package->getVersion(), '<')
  106. ) {
  107. $packages[$type][$package->getName()] = $package;
  108. }
  109. }, 'Composer\Package\CompletePackage');
  110. foreach (array('<info>platform</info>:' => true, '<comment>available</comment>:' => false, '<info>installed</info>:' => true) as $type => $showVersion) {
  111. if (isset($packages[$type])) {
  112. $output->writeln($type);
  113. ksort($packages[$type]);
  114. foreach ($packages[$type] as $package) {
  115. $output->writeln(' '.$package->getPrettyName() .' '.($showVersion ? '['.$package->getPrettyVersion().']' : '').' <comment>:</comment> '. strtok($package->getDescription(), "\r\n"));
  116. }
  117. $output->writeln('');
  118. }
  119. }
  120. }
  121. /**
  122. * finds a package by name and version if provided
  123. *
  124. * @return array array(CompletePackageInterface, array of versions)
  125. * @throws \InvalidArgumentException
  126. */
  127. protected function getPackage(RepositoryInterface $installedRepo, RepositoryInterface $repos, $name, $version = null)
  128. {
  129. $name = strtolower($name);
  130. if ($version) {
  131. $parser = new VersionParser();
  132. $version = $parser->normalize($version);
  133. }
  134. $match = null;
  135. $matches = array();
  136. $repos->filterPackages(function ($package) use ($name, $version, &$matches) {
  137. if ($package->getName() === $name) {
  138. $matches[] = $package;
  139. }
  140. }, 'Composer\Package\CompletePackage');
  141. if (null === $version) {
  142. // search for a locally installed version
  143. foreach ($matches as $package) {
  144. if ($installedRepo->hasPackage($package)) {
  145. $match = $package;
  146. break;
  147. }
  148. }
  149. if (!$match) {
  150. // fallback to the highest version
  151. foreach ($matches as $package) {
  152. if (null === $match || version_compare($package->getVersion(), $match->getVersion(), '>=')) {
  153. $match = $package;
  154. }
  155. }
  156. }
  157. } else {
  158. // select the specified version
  159. foreach ($matches as $package) {
  160. if ($package->getVersion() === $version) {
  161. $match = $package;
  162. }
  163. }
  164. }
  165. // build versions array
  166. $versions = array();
  167. foreach ($matches as $package) {
  168. $versions[$package->getPrettyVersion()] = $package->getVersion();
  169. }
  170. return array($match, $versions);
  171. }
  172. /**
  173. * prints package meta data
  174. */
  175. protected function printMeta(InputInterface $input, OutputInterface $output, CompletePackageInterface $package, array $versions, RepositoryInterface $installedRepo, RepositoryInterface $repos)
  176. {
  177. $output->writeln('<info>name</info> : ' . $package->getPrettyName());
  178. $output->writeln('<info>descrip.</info> : ' . $package->getDescription());
  179. $output->writeln('<info>keywords</info> : ' . join(', ', $package->getKeywords() ?: array()));
  180. $this->printVersions($input, $output, $package, $versions, $installedRepo, $repos);
  181. $output->writeln('<info>type</info> : ' . $package->getType());
  182. $output->writeln('<info>license</info> : ' . implode(', ', $package->getLicense()));
  183. $output->writeln('<info>source</info> : ' . sprintf('[%s] <comment>%s</comment> %s', $package->getSourceType(), $package->getSourceUrl(), $package->getSourceReference()));
  184. $output->writeln('<info>dist</info> : ' . sprintf('[%s] <comment>%s</comment> %s', $package->getDistType(), $package->getDistUrl(), $package->getDistReference()));
  185. $output->writeln('<info>names</info> : ' . implode(', ', $package->getNames()));
  186. if ($package->getSupport()) {
  187. $output->writeln("\n<info>support</info>");
  188. foreach ($package->getSupport() as $type => $value) {
  189. $output->writeln('<comment>' . $type . '</comment> : '.$value);
  190. }
  191. }
  192. if ($package->getAutoload()) {
  193. $output->writeln("\n<info>autoload</info>");
  194. foreach ($package->getAutoload() as $type => $autoloads) {
  195. $output->writeln('<comment>' . $type . '</comment>');
  196. if ($type === 'psr-0') {
  197. foreach ($autoloads as $name => $path) {
  198. $output->writeln(($name ?: '*') . ' => ' . ($path ?: '.'));
  199. }
  200. } elseif ($type === 'classmap') {
  201. $output->writeln(implode(', ', $autoloads));
  202. }
  203. }
  204. if ($package->getIncludePaths()) {
  205. $output->writeln('<comment>include-path</comment>');
  206. $output->writeln(implode(', ', $package->getIncludePaths()));
  207. }
  208. }
  209. }
  210. /**
  211. * prints all available versions of this package and highlights the installed one if any
  212. */
  213. protected function printVersions(InputInterface $input, OutputInterface $output, CompletePackageInterface $package, array $versions, RepositoryInterface $installedRepo, RepositoryInterface $repos)
  214. {
  215. if ($input->getArgument('version')) {
  216. $output->writeln('<info>version</info> : ' . $package->getPrettyVersion());
  217. return;
  218. }
  219. uasort($versions, 'version_compare');
  220. $versions = implode(', ', array_keys(array_reverse($versions)));
  221. // highlight installed version
  222. if ($installedRepo->hasPackage($package)) {
  223. $versions = str_replace($package->getPrettyVersion(), '<info>* ' . $package->getPrettyVersion() . '</info>', $versions);
  224. }
  225. $output->writeln('<info>versions</info> : ' . $versions);
  226. }
  227. /**
  228. * print link objects
  229. *
  230. * @param string $linkType
  231. */
  232. protected function printLinks(InputInterface $input, OutputInterface $output, CompletePackageInterface $package, $linkType, $title = null)
  233. {
  234. $title = $title ?: $linkType;
  235. if ($links = $package->{'get'.ucfirst($linkType)}()) {
  236. $output->writeln("\n<info>" . $title . "</info>");
  237. foreach ($links as $link) {
  238. $output->writeln($link->getTarget() . ' <comment>' . $link->getPrettyConstraint() . '</comment>');
  239. }
  240. }
  241. }
  242. }