ShowCommand.php 10 KB

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