ShowCommand.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  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\Package\CompletePackageInterface;
  15. use Composer\Package\Version\VersionParser;
  16. use Composer\Package\BasePackage;
  17. use Composer\Package\Version\VersionSelector;
  18. use Composer\Plugin\CommandEvent;
  19. use Composer\Plugin\PluginEvents;
  20. use Composer\Package\PackageInterface;
  21. use Composer\Semver\Constraint\ConstraintInterface;
  22. use Composer\Util\Platform;
  23. use Symfony\Component\Console\Formatter\OutputFormatterStyle;
  24. use Symfony\Component\Console\Input\InputInterface;
  25. use Symfony\Component\Console\Input\InputArgument;
  26. use Symfony\Component\Console\Input\InputOption;
  27. use Symfony\Component\Console\Output\OutputInterface;
  28. use Composer\Repository\ArrayRepository;
  29. use Composer\Repository\CompositeRepository;
  30. use Composer\Repository\ComposerRepository;
  31. use Composer\Repository\PlatformRepository;
  32. use Composer\Repository\RepositoryInterface;
  33. use Composer\Repository\RepositoryFactory;
  34. use Composer\Spdx\SpdxLicenses;
  35. use Composer\Composer;
  36. use Composer\Semver\Semver;
  37. /**
  38. * @author Robert Schönthal <seroscho@googlemail.com>
  39. * @author Jordi Boggiano <j.boggiano@seld.be>
  40. * @author Jérémy Romey <jeremyFreeAgent>
  41. */
  42. class ShowCommand extends BaseCommand
  43. {
  44. /** @var VersionParser */
  45. protected $versionParser;
  46. protected $colors;
  47. /** @var Pool */
  48. private $pool;
  49. protected function configure()
  50. {
  51. $this
  52. ->setName('show')
  53. ->setAliases(array('info'))
  54. ->setDescription('Show information about packages')
  55. ->setDefinition(array(
  56. new InputArgument('package', InputArgument::OPTIONAL, 'Package to inspect. Or a name including a wildcard (*) to filter lists of packages instead.'),
  57. new InputArgument('version', InputArgument::OPTIONAL, 'Version or version constraint to inspect'),
  58. new InputOption('all', null, InputOption::VALUE_NONE, 'List all packages'),
  59. new InputOption('installed', 'i', InputOption::VALUE_NONE, 'List installed packages only (enabled by default, only present for BC).'),
  60. new InputOption('platform', 'p', InputOption::VALUE_NONE, 'List platform packages only'),
  61. new InputOption('available', 'a', InputOption::VALUE_NONE, 'List available packages only'),
  62. new InputOption('self', 's', InputOption::VALUE_NONE, 'Show the root package information'),
  63. new InputOption('name-only', 'N', InputOption::VALUE_NONE, 'List package names only'),
  64. new InputOption('path', 'P', InputOption::VALUE_NONE, 'Show package paths'),
  65. new InputOption('tree', 't', InputOption::VALUE_NONE, 'List the dependencies as a tree'),
  66. new InputOption('latest', 'l', InputOption::VALUE_NONE, 'Show the latest version'),
  67. new InputOption('outdated', 'o', InputOption::VALUE_NONE, 'Show the latest version but only for packages that are outdated'),
  68. new InputOption('direct', 'D', InputOption::VALUE_NONE, 'Shows only packages that are directly required by the root package'),
  69. ))
  70. ->setHelp(<<<EOT
  71. The show command displays detailed information about a package, or
  72. lists all packages available.
  73. EOT
  74. )
  75. ;
  76. }
  77. protected function execute(InputInterface $input, OutputInterface $output)
  78. {
  79. $this->versionParser = new VersionParser;
  80. if ($input->getOption('tree')) {
  81. $this->initStyles($output);
  82. }
  83. $composer = $this->getComposer(false);
  84. $io = $this->getIO();
  85. if ($input->getOption('installed')) {
  86. $io->writeError('<warning>You are using the deprecated option "installed". Only installed packages are shown by default now. The --all option can be used to show all packages.</warning>');
  87. }
  88. if ($input->getOption('outdated')) {
  89. $input->setOption('latest', true);
  90. }
  91. if ($input->getOption('direct') && ($input->getOption('all') || $input->getOption('available') || $input->getOption('platform'))) {
  92. $io->writeError('The --direct (-D) option is not usable in combination with --all, --platform (-p) or --available (-a)');
  93. return 1;
  94. }
  95. if ($input->getOption('tree') && ($input->getOption('all') || $input->getOption('available'))) {
  96. $io->writeError('The --tree (-t) option is not usable in combination with --all or --available (-a)');
  97. return 1;
  98. }
  99. // init repos
  100. $platformOverrides = array();
  101. if ($composer) {
  102. $platformOverrides = $composer->getConfig()->get('platform') ?: array();
  103. }
  104. $platformRepo = new PlatformRepository(array(), $platformOverrides);
  105. $phpVersion = $platformRepo->findPackage('php', '*')->getVersion();
  106. if ($input->getOption('self')) {
  107. $package = $this->getComposer()->getPackage();
  108. $repos = $installedRepo = new ArrayRepository(array($package));
  109. } elseif ($input->getOption('platform')) {
  110. $repos = $installedRepo = $platformRepo;
  111. } elseif ($input->getOption('available')) {
  112. $installedRepo = $platformRepo;
  113. if ($composer) {
  114. $repos = new CompositeRepository($composer->getRepositoryManager()->getRepositories());
  115. } else {
  116. $defaultRepos = RepositoryFactory::defaultRepos($io);
  117. $repos = new CompositeRepository($defaultRepos);
  118. $io->writeError('No composer.json found in the current directory, showing available packages from ' . implode(', ', array_keys($defaultRepos)));
  119. }
  120. } elseif ($input->getOption('all') && $composer) {
  121. $localRepo = $composer->getRepositoryManager()->getLocalRepository();
  122. $installedRepo = new CompositeRepository(array($localRepo, $platformRepo));
  123. $repos = new CompositeRepository(array_merge(array($installedRepo), $composer->getRepositoryManager()->getRepositories()));
  124. } elseif ($input->getOption('all')) {
  125. $defaultRepos = RepositoryFactory::defaultRepos($io);
  126. $io->writeError('No composer.json found in the current directory, showing available packages from ' . implode(', ', array_keys($defaultRepos)));
  127. $installedRepo = $platformRepo;
  128. $repos = new CompositeRepository(array_merge(array($installedRepo), $defaultRepos));
  129. } else {
  130. $repos = $installedRepo = $this->getComposer()->getRepositoryManager()->getLocalRepository();
  131. }
  132. if ($composer) {
  133. $commandEvent = new CommandEvent(PluginEvents::COMMAND, 'show', $input, $output);
  134. $composer->getEventDispatcher()->dispatch($commandEvent->getName(), $commandEvent);
  135. }
  136. if ($input->getOption('latest') && null === $composer) {
  137. $io->writeError('No composer.json found in the current directory, disabling "latest" option');
  138. $input->setOption('latest', false);
  139. }
  140. $packageFilter = $input->getArgument('package');
  141. // show single package or single version
  142. if (($packageFilter && false === strpos($packageFilter, '*')) || !empty($package)) {
  143. if (empty($package)) {
  144. list($package, $versions) = $this->getPackage($installedRepo, $repos, $input->getArgument('package'), $input->getArgument('version'));
  145. if (!$package) {
  146. throw new \InvalidArgumentException('Package '.$input->getArgument('package').' not found');
  147. }
  148. } else {
  149. $versions = array($package->getPrettyVersion() => $package->getVersion());
  150. }
  151. if ($input->getOption('tree')) {
  152. $this->displayPackageTree($package, $installedRepo, $repos);
  153. } else {
  154. $latestPackage = null;
  155. if ($input->getOption('latest')) {
  156. $latestPackage = $this->findLatestPackage($package->getName(), $composer, $phpVersion);
  157. }
  158. $this->printMeta($package, $versions, $installedRepo, $latestPackage);
  159. $this->printLinks($package, 'requires');
  160. $this->printLinks($package, 'devRequires', 'requires (dev)');
  161. if ($package->getSuggests()) {
  162. $io->write("\n<info>suggests</info>");
  163. foreach ($package->getSuggests() as $suggested => $reason) {
  164. $io->write($suggested . ' <comment>' . $reason . '</comment>');
  165. }
  166. }
  167. $this->printLinks($package, 'provides');
  168. $this->printLinks($package, 'conflicts');
  169. $this->printLinks($package, 'replaces');
  170. }
  171. return;
  172. }
  173. // show tree view if requested
  174. if ($input->getOption('tree')) {
  175. $rootRequires = $this->getRootRequires();
  176. foreach ($installedRepo->getPackages() as $package) {
  177. if (in_array($package->getName(), $rootRequires, true)) {
  178. $this->displayPackageTree($package, $installedRepo, $repos);
  179. }
  180. }
  181. return 0;
  182. }
  183. if ($repos instanceof CompositeRepository) {
  184. $repos = $repos->getRepositories();
  185. } elseif (!is_array($repos)) {
  186. $repos = array($repos);
  187. }
  188. // list packages
  189. $packages = array();
  190. if (null !== $packageFilter) {
  191. $packageFilter = '{^'.str_replace('\\*', '.*?', preg_quote($packageFilter)).'$}i';
  192. }
  193. $packageListFilter = array();
  194. if ($input->getOption('direct')) {
  195. $packageListFilter = $this->getRootRequires();
  196. }
  197. foreach ($repos as $repo) {
  198. if ($repo === $platformRepo) {
  199. $type = '<info>platform</info>:';
  200. } elseif (
  201. $repo === $installedRepo
  202. || ($installedRepo instanceof CompositeRepository && in_array($repo, $installedRepo->getRepositories(), true))
  203. ) {
  204. $type = '<info>installed</info>:';
  205. } else {
  206. $type = '<comment>available</comment>:';
  207. }
  208. if ($repo instanceof ComposerRepository && $repo->hasProviders()) {
  209. foreach ($repo->getProviderNames() as $name) {
  210. if (!$packageFilter || preg_match($packageFilter, $name)) {
  211. $packages[$type][$name] = $name;
  212. }
  213. }
  214. } else {
  215. foreach ($repo->getPackages() as $package) {
  216. if (!isset($packages[$type][$package->getName()])
  217. || !is_object($packages[$type][$package->getName()])
  218. || version_compare($packages[$type][$package->getName()]->getVersion(), $package->getVersion(), '<')
  219. ) {
  220. if (!$packageFilter || preg_match($packageFilter, $package->getName())) {
  221. if (!$packageListFilter || in_array($package->getName(), $packageListFilter, true)) {
  222. $packages[$type][$package->getName()] = $package;
  223. }
  224. }
  225. }
  226. }
  227. }
  228. }
  229. $showAllTypes = $input->getOption('all');
  230. $showLatest = $input->getOption('latest');
  231. $indent = $showAllTypes ? ' ' : '';
  232. $latestPackages = array();
  233. foreach (array('<info>platform</info>:' => true, '<comment>available</comment>:' => false, '<info>installed</info>:' => true) as $type => $showVersion) {
  234. if (isset($packages[$type])) {
  235. if ($showAllTypes) {
  236. $io->write($type);
  237. }
  238. ksort($packages[$type]);
  239. $nameLength = $versionLength = $latestLength = 0;
  240. foreach ($packages[$type] as $package) {
  241. if (is_object($package)) {
  242. $nameLength = max($nameLength, strlen($package->getPrettyName()));
  243. if ($showVersion) {
  244. $versionLength = max($versionLength, strlen($package->getFullPrettyVersion()));
  245. if ($showLatest) {
  246. $latestPackage = $this->findLatestPackage($package->getName(), $composer, $phpVersion);
  247. $latestPackages[$package->getPrettyName()] = $latestPackage;
  248. $latestLength = max($latestLength, strlen($latestPackage->getFullPrettyVersion()));
  249. }
  250. }
  251. } else {
  252. $nameLength = max($nameLength, $package);
  253. }
  254. }
  255. list($width) = $this->getApplication()->getTerminalDimensions();
  256. if (null === $width) {
  257. // In case the width is not detected, we're probably running the command
  258. // outside of a real terminal, use space without a limit
  259. $width = PHP_INT_MAX;
  260. }
  261. if (Platform::isWindows()) {
  262. $width--;
  263. }
  264. if ($input->getOption('path') && null === $composer) {
  265. $io->writeError('No composer.json found in the current directory, disabling "path" option');
  266. $input->setOption('path', false);
  267. }
  268. $writePath = !$input->getOption('name-only') && $input->getOption('path');
  269. $writeVersion = !$input->getOption('name-only') && !$input->getOption('path') && $showVersion && ($nameLength + $versionLength + 3 <= $width);
  270. $writeLatest = $writeVersion && $showLatest && ($nameLength + $versionLength + $latestLength + 3 <= $width);
  271. $writeDescription = !$input->getOption('name-only') && !$input->getOption('path') && ($nameLength + $versionLength + $latestLength + 24 <= $width);
  272. foreach ($packages[$type] as $package) {
  273. if (is_object($package)) {
  274. $latestPackackage = null;
  275. if ($showLatest && isset($latestPackages[$package->getPrettyName()])) {
  276. $latestPackackage = $latestPackages[$package->getPrettyName()];
  277. }
  278. if ($input->getOption('outdated') && $latestPackackage && $latestPackackage->getFullPrettyVersion() === $package->getFullPrettyVersion()) {
  279. continue;
  280. }
  281. $io->write($indent . str_pad($package->getPrettyName(), $nameLength, ' '), false);
  282. if ($writeVersion) {
  283. $io->write(' ' . str_pad($package->getFullPrettyVersion(), $versionLength, ' '), false);
  284. }
  285. if ($writeLatest && $latestPackackage) {
  286. $latestVersion = $latestPackackage->getFullPrettyVersion();
  287. $style = $this->getVersionStyle($latestVersion, $package);
  288. $io->write(' <'.$style.'>' . str_pad($latestVersion, $latestLength, ' ') . '</'.$style.'>', false);
  289. }
  290. if ($writeDescription) {
  291. $description = strtok($package->getDescription(), "\r\n");
  292. $remaining = $width - $nameLength - $versionLength - 4;
  293. if ($writeLatest) {
  294. $remaining -= $latestLength;
  295. }
  296. if (strlen($description) > $remaining) {
  297. $description = substr($description, 0, $remaining - 3) . '...';
  298. }
  299. $io->write(' ' . $description, false);
  300. }
  301. if ($writePath) {
  302. $path = strtok(realpath($composer->getInstallationManager()->getInstallPath($package)), "\r\n");
  303. $io->write(' ' . $path, false);
  304. }
  305. if ($latestPackackage && $latestPackackage->isAbandoned()) {
  306. $replacement = (is_string($latestPackackage->getReplacementPackage()))
  307. ? 'Use ' . $latestPackackage->getReplacementPackage() . ' instead'
  308. : 'No replacement was suggested';
  309. $io->writeError('');
  310. $io->writeError(
  311. sprintf(
  312. "<warning>Package %s is abandoned, you should avoid using it. %s.</warning>",
  313. $package->getPrettyName(),
  314. $replacement
  315. ),
  316. false
  317. );
  318. }
  319. } else {
  320. $io->write($indent . $package, false);
  321. }
  322. $io->write('');
  323. }
  324. if ($showAllTypes) {
  325. $io->write('');
  326. }
  327. }
  328. }
  329. }
  330. protected function getRootRequires()
  331. {
  332. $rootPackage = $this->getComposer()->getPackage();
  333. return array_map(
  334. 'strtolower',
  335. array_keys(array_merge($rootPackage->getRequires(), $rootPackage->getDevRequires()))
  336. );
  337. }
  338. protected function getVersionStyle($latestVersion, $package)
  339. {
  340. if ($latestVersion === $package->getFullPrettyVersion()) {
  341. // print green as it's up to date
  342. return 'info';
  343. }
  344. if ($latestVersion && Semver::satisfies($latestVersion, '^'.$package->getVersion())) {
  345. // print red as it needs an immediate semver-compliant upgrade
  346. return 'highlight';
  347. }
  348. // print yellow as it needs an upgrade but has potential BC breaks so is not urgent
  349. return 'comment';
  350. }
  351. /**
  352. * finds a package by name and version if provided
  353. *
  354. * @param RepositoryInterface $installedRepo
  355. * @param RepositoryInterface $repos
  356. * @param string $name
  357. * @param ConstraintInterface|string $version
  358. * @throws \InvalidArgumentException
  359. * @return array array(CompletePackageInterface, array of versions)
  360. */
  361. protected function getPackage(RepositoryInterface $installedRepo, RepositoryInterface $repos, $name, $version = null)
  362. {
  363. $name = strtolower($name);
  364. $constraint = is_string($version) ? $this->versionParser->parseConstraints($version) : $version;
  365. $policy = new DefaultPolicy();
  366. $pool = new Pool('dev');
  367. $pool->addRepository($repos);
  368. $matchedPackage = null;
  369. $versions = array();
  370. $matches = $pool->whatProvides($name, $constraint);
  371. foreach ($matches as $index => $package) {
  372. // skip providers/replacers
  373. if ($package->getName() !== $name) {
  374. unset($matches[$index]);
  375. continue;
  376. }
  377. // select an exact match if it is in the installed repo and no specific version was required
  378. if (null === $version && $installedRepo->hasPackage($package)) {
  379. $matchedPackage = $package;
  380. }
  381. $versions[$package->getPrettyVersion()] = $package->getVersion();
  382. $matches[$index] = $package->getId();
  383. }
  384. // select preferred package according to policy rules
  385. if (!$matchedPackage && $matches && $preferred = $policy->selectPreferredPackages($pool, array(), $matches)) {
  386. $matchedPackage = $pool->literalToPackage($preferred[0]);
  387. }
  388. return array($matchedPackage, $versions);
  389. }
  390. /**
  391. * Prints package metadata.
  392. *
  393. * @param CompletePackageInterface $package
  394. * @param array $versions
  395. * @param RepositoryInterface $installedRepo
  396. */
  397. protected function printMeta(CompletePackageInterface $package, array $versions, RepositoryInterface $installedRepo, PackageInterface $latestPackage = null)
  398. {
  399. $io = $this->getIO();
  400. $io->write('<info>name</info> : ' . $package->getPrettyName());
  401. $io->write('<info>descrip.</info> : ' . $package->getDescription());
  402. $io->write('<info>keywords</info> : ' . join(', ', $package->getKeywords() ?: array()));
  403. $this->printVersions($package, $versions, $installedRepo);
  404. if ($latestPackage) {
  405. $style = $this->getVersionStyle($latestPackage->getPrettyVersion(), $package);
  406. $io->write('<info>latest</info> : <'.$style.'>' . $latestPackage->getPrettyVersion() . '</'.$style.'>');
  407. } else {
  408. $latestPackage = $package;
  409. }
  410. $io->write('<info>type</info> : ' . $package->getType());
  411. $this->printLicenses($package);
  412. $io->write('<info>source</info> : ' . sprintf('[%s] <comment>%s</comment> %s', $package->getSourceType(), $package->getSourceUrl(), $package->getSourceReference()));
  413. $io->write('<info>dist</info> : ' . sprintf('[%s] <comment>%s</comment> %s', $package->getDistType(), $package->getDistUrl(), $package->getDistReference()));
  414. $io->write('<info>names</info> : ' . implode(', ', $package->getNames()));
  415. if ($latestPackage->isAbandoned()) {
  416. $replacement = ($latestPackage->getReplacementPackage() !== null)
  417. ? ' The author suggests using the ' . $latestPackage->getReplacementPackage(). ' package instead.'
  418. : null;
  419. $io->writeError(
  420. sprintf('<warning>Attention: This package is abandoned and no longer maintained.%s</warning>', $replacement)
  421. );
  422. }
  423. if ($package->getSupport()) {
  424. $io->write("\n<info>support</info>");
  425. foreach ($package->getSupport() as $type => $value) {
  426. $io->write('<comment>' . $type . '</comment> : '.$value);
  427. }
  428. }
  429. if ($package->getAutoload()) {
  430. $io->write("\n<info>autoload</info>");
  431. foreach ($package->getAutoload() as $type => $autoloads) {
  432. $io->write('<comment>' . $type . '</comment>');
  433. if ($type === 'psr-0') {
  434. foreach ($autoloads as $name => $path) {
  435. $io->write(($name ?: '*') . ' => ' . (is_array($path) ? implode(', ', $path) : ($path ?: '.')));
  436. }
  437. } elseif ($type === 'psr-4') {
  438. foreach ($autoloads as $name => $path) {
  439. $io->write(($name ?: '*') . ' => ' . (is_array($path) ? implode(', ', $path) : ($path ?: '.')));
  440. }
  441. } elseif ($type === 'classmap') {
  442. $io->write(implode(', ', $autoloads));
  443. }
  444. }
  445. if ($package->getIncludePaths()) {
  446. $io->write('<comment>include-path</comment>');
  447. $io->write(implode(', ', $package->getIncludePaths()));
  448. }
  449. }
  450. }
  451. /**
  452. * Prints all available versions of this package and highlights the installed one if any.
  453. *
  454. * @param CompletePackageInterface $package
  455. * @param array $versions
  456. * @param RepositoryInterface $installedRepo
  457. */
  458. protected function printVersions(CompletePackageInterface $package, array $versions, RepositoryInterface $installedRepo)
  459. {
  460. uasort($versions, 'version_compare');
  461. $versions = array_keys(array_reverse($versions));
  462. // highlight installed version
  463. if ($installedRepo->hasPackage($package)) {
  464. $installedVersion = $package->getPrettyVersion();
  465. $key = array_search($installedVersion, $versions);
  466. if (false !== $key) {
  467. $versions[$key] = '<info>* ' . $installedVersion . '</info>';
  468. }
  469. }
  470. $versions = implode(', ', $versions);
  471. $this->getIO()->write('<info>versions</info> : ' . $versions);
  472. }
  473. /**
  474. * print link objects
  475. *
  476. * @param CompletePackageInterface $package
  477. * @param string $linkType
  478. * @param string $title
  479. */
  480. protected function printLinks(CompletePackageInterface $package, $linkType, $title = null)
  481. {
  482. $title = $title ?: $linkType;
  483. $io = $this->getIO();
  484. if ($links = $package->{'get'.ucfirst($linkType)}()) {
  485. $io->write("\n<info>" . $title . "</info>");
  486. foreach ($links as $link) {
  487. $io->write($link->getTarget() . ' <comment>' . $link->getPrettyConstraint() . '</comment>');
  488. }
  489. }
  490. }
  491. /**
  492. * Prints the licenses of a package with metadata
  493. *
  494. * @param CompletePackageInterface $package
  495. */
  496. protected function printLicenses(CompletePackageInterface $package)
  497. {
  498. $spdxLicenses = new SpdxLicenses();
  499. $licenses = $package->getLicense();
  500. $io = $this->getIO();
  501. foreach ($licenses as $licenseId) {
  502. $license = $spdxLicenses->getLicenseByIdentifier($licenseId); // keys: 0 fullname, 1 osi, 2 url
  503. if (!$license) {
  504. $out = $licenseId;
  505. } else {
  506. // is license OSI approved?
  507. if ($license[1] === true) {
  508. $out = sprintf('%s (%s) (OSI approved) %s', $license[0], $licenseId, $license[2]);
  509. } else {
  510. $out = sprintf('%s (%s) %s', $license[0], $licenseId, $license[2]);
  511. }
  512. }
  513. $io->write('<info>license</info> : ' . $out);
  514. }
  515. }
  516. /**
  517. * Init styles for tree
  518. *
  519. * @param OutputInterface $output
  520. */
  521. protected function initStyles(OutputInterface $output)
  522. {
  523. $this->colors = array(
  524. 'green',
  525. 'yellow',
  526. 'cyan',
  527. 'magenta',
  528. 'blue',
  529. );
  530. foreach ($this->colors as $color) {
  531. $style = new OutputFormatterStyle($color);
  532. $output->getFormatter()->setStyle($color, $style);
  533. }
  534. }
  535. /**
  536. * Display the tree
  537. *
  538. * @param PackageInterface|string $package
  539. * @param RepositoryInterface $installedRepo
  540. * @param RepositoryInterface $distantRepos
  541. */
  542. protected function displayPackageTree(PackageInterface $package, RepositoryInterface $installedRepo, RepositoryInterface $distantRepos)
  543. {
  544. $io = $this->getIO();
  545. $io->write(sprintf('<info>%s</info>', $package->getPrettyName()), false);
  546. $io->write(' ' . $package->getPrettyVersion(), false);
  547. $io->write(' ' . strtok($package->getDescription(), "\r\n"));
  548. if (is_object($package)) {
  549. $requires = $package->getRequires();
  550. $treeBar = '├';
  551. $j = 0;
  552. $total = count($requires);
  553. foreach ($requires as $requireName => $require) {
  554. $j++;
  555. if ($j == 0) {
  556. $this->writeTreeLine($treeBar);
  557. }
  558. if ($j == $total) {
  559. $treeBar = '└';
  560. }
  561. $level = 1;
  562. $color = $this->colors[$level];
  563. $info = sprintf('%s──<%s>%s</%s> %s', $treeBar, $color, $requireName, $color, $require->getPrettyConstraint());
  564. $this->writeTreeLine($info);
  565. $treeBar = str_replace('└', ' ', $treeBar);
  566. $packagesInTree = array($package->getName(), $requireName);
  567. $this->displayTree($requireName, $require, $installedRepo, $distantRepos, $packagesInTree, $treeBar, $level + 1);
  568. }
  569. }
  570. }
  571. /**
  572. * Display a package tree
  573. *
  574. * @param string $name
  575. * @param PackageInterface|string $package
  576. * @param RepositoryInterface $installedRepo
  577. * @param RepositoryInterface $distantRepos
  578. * @param array $packagesInTree
  579. * @param string $previousTreeBar
  580. * @param int $level
  581. */
  582. protected function displayTree($name, $package, RepositoryInterface $installedRepo, RepositoryInterface $distantRepos, array $packagesInTree, $previousTreeBar = '├', $level = 1)
  583. {
  584. $previousTreeBar = str_replace('├', '│', $previousTreeBar);
  585. list($package, $versions) = $this->getPackage($installedRepo, $distantRepos, $name, $package->getPrettyConstraint() === 'self.version' ? $package->getConstraint() : $package->getPrettyConstraint());
  586. if (is_object($package)) {
  587. $requires = $package->getRequires();
  588. $treeBar = $previousTreeBar . ' ├';
  589. $i = 0;
  590. $total = count($requires);
  591. foreach ($requires as $requireName => $require) {
  592. $currentTree = $packagesInTree;
  593. $i++;
  594. if ($i == $total) {
  595. $treeBar = $previousTreeBar . ' └';
  596. }
  597. $colorIdent = $level % count($this->colors);
  598. $color = $this->colors[$colorIdent];
  599. $circularWarn = in_array($requireName, $currentTree) ? '(circular dependency aborted here)' : '';
  600. $info = rtrim(sprintf('%s──<%s>%s</%s> %s %s', $treeBar, $color, $requireName, $color, $require->getPrettyConstraint(), $circularWarn));
  601. $this->writeTreeLine($info);
  602. $treeBar = str_replace('└', ' ', $treeBar);
  603. if (!in_array($requireName, $currentTree)) {
  604. $currentTree[] = $requireName;
  605. $this->displayTree($requireName, $require, $installedRepo, $distantRepos, $currentTree, $treeBar, $level + 1);
  606. }
  607. }
  608. }
  609. }
  610. private function writeTreeLine($line)
  611. {
  612. $io = $this->getIO();
  613. if (!$io->isDecorated()) {
  614. $line = str_replace(array('└', '├', '──', '│'), array('`-', '|-', '-', '|'), $line);
  615. }
  616. $io->write($line);
  617. }
  618. /**
  619. * Given a package name, this finds the latest package matching it
  620. *
  621. * @param string $name
  622. * @param Composer $composer
  623. * @param string $phpVersion
  624. *
  625. * @return PackageInterface|null
  626. */
  627. private function findLatestPackage($name, Composer $composer, $phpVersion)
  628. {
  629. // find the latest version allowed in this pool
  630. $versionSelector = new VersionSelector($this->getPool($composer));
  631. $stability = $composer->getPackage()->getMinimumStability();
  632. $flags = $composer->getPackage()->getStabilityFlags();
  633. if (isset($flags[$name])) {
  634. $stability = array_search($flags[$name], BasePackage::$stabilities, true);
  635. }
  636. return $versionSelector->findBestCandidate($name, null, $phpVersion, $stability);
  637. }
  638. private function getPool(Composer $composer)
  639. {
  640. if (!$this->pool) {
  641. $this->pool = new Pool($composer->getPackage()->getMinimumStability(), $composer->getPackage()->getStabilityFlags());
  642. $this->pool->addRepository(new CompositeRepository($composer->getRepositoryManager()->getRepositories()));
  643. }
  644. return $this->pool;
  645. }
  646. }