CheckPlatformReqsCommand.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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\Package\Link;
  13. use Composer\Package\PackageInterface;
  14. use Composer\Semver\Constraint\Constraint;
  15. use Symfony\Component\Console\Helper\Table;
  16. use Symfony\Component\Console\Input\InputInterface;
  17. use Symfony\Component\Console\Input\InputOption;
  18. use Symfony\Component\Console\Output\OutputInterface;
  19. use Composer\Repository\PlatformRepository;
  20. class CheckPlatformReqsCommand extends BaseCommand
  21. {
  22. protected function configure()
  23. {
  24. $this->setName('check-platform-reqs')
  25. ->setDescription('Check that platform requirements are satisfied.')
  26. ->setDefinition(array(
  27. new InputOption('no-dev', null, InputOption::VALUE_NONE, 'Disables checking of require-dev packages requirements.'),
  28. ))
  29. ->setHelp(
  30. <<<EOT
  31. Checks that your PHP and extensions versions match the platform requirements of the installed packages.
  32. Unlike update/install, this command will ignore config.platform settings and check the real platform packages so you can be certain you have the required platform dependencies.
  33. <info>php composer.phar check-platform-reqs</info>
  34. EOT
  35. );
  36. }
  37. protected function execute(InputInterface $input, OutputInterface $output)
  38. {
  39. $composer = $this->getComposer();
  40. $requires = $composer->getPackage()->getRequires();
  41. if ($input->getOption('no-dev')) {
  42. $dependencies = $composer->getLocker()->getLockedRepository(!$input->getOption('no-dev'))->getPackages();
  43. } else {
  44. $dependencies = $composer->getRepositoryManager()->getLocalRepository()->getPackages();
  45. // fallback to lockfile if installed repo is empty
  46. if (!$dependencies) {
  47. $dependencies = $composer->getLocker()->getLockedRepository(true)->getPackages();
  48. }
  49. $requires += $composer->getPackage()->getDevRequires();
  50. }
  51. foreach ($requires as $require => $link) {
  52. $requires[$require] = array($link);
  53. }
  54. foreach ($dependencies as $package) {
  55. foreach ($package->getRequires() as $require => $link) {
  56. $requires[$require][] = $link;
  57. }
  58. }
  59. ksort($requires);
  60. $platformRepo = new PlatformRepository(array(), array());
  61. $currentPlatformPackages = $platformRepo->getPackages();
  62. $currentPlatformPackageMap = array();
  63. /**
  64. * @var PackageInterface $currentPlatformPackage
  65. */
  66. foreach ($currentPlatformPackages as $currentPlatformPackage) {
  67. $currentPlatformPackageMap[$currentPlatformPackage->getName()] = $currentPlatformPackage;
  68. }
  69. $results = array();
  70. $exitCode = 0;
  71. /**
  72. * @var Link[] $links
  73. */
  74. foreach ($requires as $require => $links) {
  75. if (preg_match(PlatformRepository::PLATFORM_PACKAGE_REGEX, $require)) {
  76. if (isset($currentPlatformPackageMap[$require])) {
  77. $pass = true;
  78. $version = $currentPlatformPackageMap[$require]->getVersion();
  79. foreach ($links as $link) {
  80. if (!$link->getConstraint()->matches(new Constraint('=', $version))) {
  81. $results[] = array(
  82. $currentPlatformPackageMap[$require]->getPrettyName(),
  83. $currentPlatformPackageMap[$require]->getPrettyVersion(),
  84. $link,
  85. '<error>failed</error>',
  86. );
  87. $pass = false;
  88. $exitCode = max($exitCode, 1);
  89. }
  90. }
  91. if ($pass) {
  92. $results[] = array(
  93. $currentPlatformPackageMap[$require]->getPrettyName(),
  94. $currentPlatformPackageMap[$require]->getPrettyVersion(),
  95. null,
  96. '<info>success</info>',
  97. );
  98. }
  99. } else {
  100. $results[] = array(
  101. $require,
  102. 'n/a',
  103. $links[0],
  104. '<error>missing</error>',
  105. );
  106. $exitCode = max($exitCode, 2);
  107. }
  108. }
  109. }
  110. $this->printTable($output, $results);
  111. return $exitCode;
  112. }
  113. protected function printTable(OutputInterface $output, $results)
  114. {
  115. $table = array();
  116. $rows = array();
  117. foreach ($results as $result) {
  118. /**
  119. * @var Link|null $link
  120. */
  121. list($platformPackage, $version, $link, $status) = $result;
  122. $rows[] = array(
  123. $platformPackage,
  124. $version,
  125. $link ? sprintf('%s %s %s (%s)', $link->getSource(), $link->getDescription(), $link->getTarget(), $link->getPrettyConstraint()) : '',
  126. $status,
  127. );
  128. }
  129. $table = array_merge($rows, $table);
  130. // Render table
  131. $renderer = new Table($output);
  132. $renderer->setStyle('compact');
  133. $rendererStyle = $renderer->getStyle();
  134. $rendererStyle->setVerticalBorderChar('');
  135. $rendererStyle->setCellRowContentFormat('%s ');
  136. $renderer->setRows($table)->render();
  137. }
  138. }