ShowCommand.php 17 KB

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