ShowCommand.php 13 KB

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