ShowCommand.php 8.4 KB

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