LicensesCommand.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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('Shows 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(
  36. <<<EOT
  37. The license command displays detailed information about the licenses of
  38. the installed dependencies.
  39. Read more at https://getcomposer.org/doc/03-cli.md#licenses
  40. EOT
  41. )
  42. ;
  43. }
  44. protected function execute(InputInterface $input, OutputInterface $output)
  45. {
  46. $composer = $this->getComposer();
  47. $commandEvent = new CommandEvent(PluginEvents::COMMAND, 'licenses', $input, $output);
  48. $composer->getEventDispatcher()->dispatch($commandEvent->getName(), $commandEvent);
  49. $root = $composer->getPackage();
  50. $repo = $composer->getRepositoryManager()->getLocalRepository();
  51. if ($input->getOption('no-dev')) {
  52. $packages = $this->filterRequiredPackages($repo, $root);
  53. } else {
  54. $packages = $this->appendPackages($repo->getPackages(), array());
  55. }
  56. ksort($packages);
  57. $io = $this->getIO();
  58. switch ($format = $input->getOption('format')) {
  59. case 'text':
  60. $io->write('Name: <comment>'.$root->getPrettyName().'</comment>');
  61. $io->write('Version: <comment>'.$root->getFullPrettyVersion().'</comment>');
  62. $io->write('Licenses: <comment>'.(implode(', ', $root->getLicense()) ?: 'none').'</comment>');
  63. $io->write('Dependencies:');
  64. $io->write('');
  65. $table = new Table($output);
  66. $table->setStyle('compact');
  67. $tableStyle = $table->getStyle();
  68. if (method_exists($tableStyle, 'setVerticalBorderChars')) {
  69. $tableStyle->setVerticalBorderChars('');
  70. } else {
  71. $tableStyle->setVerticalBorderChar('');
  72. }
  73. $tableStyle->setCellRowContentFormat('%s ');
  74. $table->setHeaders(array('Name', 'Version', 'License'));
  75. foreach ($packages as $package) {
  76. $table->addRow(array(
  77. $package->getPrettyName(),
  78. $package->getFullPrettyVersion(),
  79. implode(', ', $package->getLicense()) ?: 'none',
  80. ));
  81. }
  82. $table->render();
  83. break;
  84. case 'json':
  85. $dependencies = array();
  86. foreach ($packages as $package) {
  87. $dependencies[$package->getPrettyName()] = array(
  88. 'version' => $package->getFullPrettyVersion(),
  89. 'license' => $package->getLicense(),
  90. );
  91. }
  92. $io->write(JsonFile::encode(array(
  93. 'name' => $root->getPrettyName(),
  94. 'version' => $root->getFullPrettyVersion(),
  95. 'license' => $root->getLicense(),
  96. 'dependencies' => $dependencies,
  97. )));
  98. break;
  99. default:
  100. throw new \RuntimeException(sprintf('Unsupported format "%s". See help for supported formats.', $format));
  101. }
  102. return 0;
  103. }
  104. /**
  105. * Find package requires and child requires
  106. *
  107. * @param RepositoryInterface $repo
  108. * @param PackageInterface $package
  109. * @param array $bucket
  110. * @return array
  111. */
  112. private function filterRequiredPackages(RepositoryInterface $repo, PackageInterface $package, $bucket = array())
  113. {
  114. $requires = array_keys($package->getRequires());
  115. $packageListNames = array_keys($bucket);
  116. $packages = array_filter(
  117. $repo->getPackages(),
  118. function ($package) use ($requires, $packageListNames) {
  119. return in_array($package->getName(), $requires) && !in_array($package->getName(), $packageListNames);
  120. }
  121. );
  122. $bucket = $this->appendPackages($packages, $bucket);
  123. foreach ($packages as $package) {
  124. $bucket = $this->filterRequiredPackages($repo, $package, $bucket);
  125. }
  126. return $bucket;
  127. }
  128. /**
  129. * Adds packages to the package list
  130. *
  131. * @param array $packages the list of packages to add
  132. * @param array $bucket the list to add packages to
  133. * @return array
  134. */
  135. public function appendPackages(array $packages, array $bucket)
  136. {
  137. foreach ($packages as $package) {
  138. $bucket[$package->getName()] = $package;
  139. }
  140. return $bucket;
  141. }
  142. }