LicensesCommand.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 Command
  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. foreach ($packages as $package) {
  79. $dependencies[$package->getPrettyName()] = array(
  80. 'version' => $package->getFullPrettyVersion(),
  81. 'license' => $package->getLicense(),
  82. );
  83. }
  84. $io->write(JsonFile::encode(array(
  85. 'name' => $root->getPrettyName(),
  86. 'version' => $root->getFullPrettyVersion(),
  87. 'license' => $root->getLicense(),
  88. 'dependencies' => $dependencies,
  89. )));
  90. break;
  91. default:
  92. throw new \RuntimeException(sprintf('Unsupported format "%s". See help for supported formats.', $format));
  93. }
  94. }
  95. /**
  96. * Find package requires and child requires
  97. *
  98. * @param RepositoryInterface $repo
  99. * @param PackageInterface $package
  100. */
  101. private function filterRequiredPackages(RepositoryInterface $repo, PackageInterface $package, $bucket = array())
  102. {
  103. $requires = array_keys($package->getRequires());
  104. $packageListNames = array_keys($bucket);
  105. $packages = array_filter(
  106. $repo->getPackages(),
  107. function ($package) use ($requires, $packageListNames) {
  108. return in_array($package->getName(), $requires) && !in_array($package->getName(), $packageListNames);
  109. }
  110. );
  111. $bucket = $this->appendPackages($packages, $bucket);
  112. foreach ($packages as $package) {
  113. $bucket = $this->filterRequiredPackages($repo, $package, $bucket);
  114. }
  115. return $bucket;
  116. }
  117. /**
  118. * Adds packages to the package list
  119. *
  120. * @param array $packages the list of packages to add
  121. * @param array $bucket the list to add packages to
  122. * @return array
  123. */
  124. public function appendPackages(array $packages, array $bucket)
  125. {
  126. foreach ($packages as $package) {
  127. $bucket[$package->getName()] = $package;
  128. }
  129. return $bucket;
  130. }
  131. }