LicensesCommand.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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\Json\JsonFile;
  13. use Composer\Plugin\CommandEvent;
  14. use Composer\Plugin\PluginEvents;
  15. use Composer\Package\PackageInterface;
  16. use Composer\Repository\RepositoryInterface;
  17. use Symfony\Component\Console\Helper\Table;
  18. use Symfony\Component\Console\Input\InputInterface;
  19. use Symfony\Component\Console\Input\InputOption;
  20. use Symfony\Component\Console\Output\OutputInterface;
  21. /**
  22. * @author Benoît Merlet <benoit.merlet@gmail.com>
  23. */
  24. class LicensesCommand extends BaseCommand
  25. {
  26. protected function configure()
  27. {
  28. $this
  29. ->setName('licenses')
  30. ->setDescription('Show information about licenses of dependencies')
  31. ->setDefinition(array(
  32. new InputOption('format', 'f', InputOption::VALUE_REQUIRED, 'Format of the output: text or json', 'text'),
  33. new InputOption('no-dev', null, InputOption::VALUE_NONE, 'Disables search in require-dev packages.'),
  34. ))
  35. ->setHelp(<<<EOT
  36. The license command displays detailed information about the licenses of
  37. the installed dependencies.
  38. EOT
  39. )
  40. ;
  41. }
  42. protected function execute(InputInterface $input, OutputInterface $output)
  43. {
  44. $composer = $this->getComposer();
  45. $commandEvent = new CommandEvent(PluginEvents::COMMAND, 'licenses', $input, $output);
  46. $composer->getEventDispatcher()->dispatch($commandEvent->getName(), $commandEvent);
  47. $root = $composer->getPackage();
  48. $repo = $composer->getRepositoryManager()->getLocalRepository();
  49. if ($input->getOption('no-dev')) {
  50. $packages = $this->filterRequiredPackages($repo, $root);
  51. } else {
  52. $packages = $this->appendPackages($repo->getPackages(), array());
  53. }
  54. ksort($packages);
  55. $io = $this->getIO();
  56. switch ($format = $input->getOption('format')) {
  57. case 'text':
  58. $io->write('Name: <comment>'.$root->getPrettyName().'</comment>');
  59. $io->write('Version: <comment>'.$root->getFullPrettyVersion().'</comment>');
  60. $io->write('Licenses: <comment>'.(implode(', ', $root->getLicense()) ?: 'none').'</comment>');
  61. $io->write('Dependencies:');
  62. $io->write('');
  63. $table = new Table($output);
  64. $table->setStyle('compact');
  65. $table->getStyle()->setVerticalBorderChar('');
  66. $table->getStyle()->setCellRowContentFormat('%s ');
  67. $table->setHeaders(array('Name', 'Version', 'License'));
  68. foreach ($packages as $package) {
  69. $table->addRow(array(
  70. $package->getPrettyName(),
  71. $package->getFullPrettyVersion(),
  72. implode(', ', $package->getLicense()) ?: 'none',
  73. ));
  74. }
  75. $table->render();
  76. break;
  77. case 'json':
  78. $dependencies = array();
  79. foreach ($packages as $package) {
  80. $dependencies[$package->getPrettyName()] = array(
  81. 'version' => $package->getFullPrettyVersion(),
  82. 'license' => $package->getLicense(),
  83. );
  84. }
  85. $io->write(JsonFile::encode(array(
  86. 'name' => $root->getPrettyName(),
  87. 'version' => $root->getFullPrettyVersion(),
  88. 'license' => $root->getLicense(),
  89. 'dependencies' => $dependencies,
  90. )));
  91. break;
  92. default:
  93. throw new \RuntimeException(sprintf('Unsupported format "%s". See help for supported formats.', $format));
  94. }
  95. }
  96. /**
  97. * Find package requires and child requires
  98. *
  99. * @param RepositoryInterface $repo
  100. * @param PackageInterface $package
  101. * @param array $bucket
  102. * @return array
  103. */
  104. private function filterRequiredPackages(RepositoryInterface $repo, PackageInterface $package, $bucket = array())
  105. {
  106. $requires = array_keys($package->getRequires());
  107. $packageListNames = array_keys($bucket);
  108. $packages = array_filter(
  109. $repo->getPackages(),
  110. function ($package) use ($requires, $packageListNames) {
  111. return in_array($package->getName(), $requires) && !in_array($package->getName(), $packageListNames);
  112. }
  113. );
  114. $bucket = $this->appendPackages($packages, $bucket);
  115. foreach ($packages as $package) {
  116. $bucket = $this->filterRequiredPackages($repo, $package, $bucket);
  117. }
  118. return $bucket;
  119. }
  120. /**
  121. * Adds packages to the package list
  122. *
  123. * @param array $packages the list of packages to add
  124. * @param array $bucket the list to add packages to
  125. * @return array
  126. */
  127. public function appendPackages(array $packages, array $bucket)
  128. {
  129. foreach ($packages as $package) {
  130. $bucket[$package->getName()] = $package;
  131. }
  132. return $bucket;
  133. }
  134. }