diff --git a/src/Composer/Command/CheckPlatformReqsCommand.php b/src/Composer/Command/CheckPlatformReqsCommand.php new file mode 100644 index 000000000..c03ed0804 --- /dev/null +++ b/src/Composer/Command/CheckPlatformReqsCommand.php @@ -0,0 +1,119 @@ +setName('check-platform-reqs') + ->setDescription('Check platform requirements of your project.') + ->setHelp(<<php composer.phar check-platform-reqs +EOT + ); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $composer = $this->getComposer(); + + $repos = $composer->getRepositoryManager()->getLocalRepository(); + + $allPackages = array_merge($repos->getPackages(), array($composer->getPackage())); + $requires = array(); + + /** + * @var PackageInterface $package + */ + foreach ($allPackages as $package) { + $requires = array_merge($requires, $package->getRequires()); + } + + $platformRepo = new PlatformRepository(array(), array()); + + $currentPlatformPackages = $platformRepo->getPackages(); + + $currentPlatformPackageMap = array(); + + /** + * @var PackageInterface $currentPlatformPackage + */ + foreach ($currentPlatformPackages as $currentPlatformPackage) { + $currentPlatformPackageMap[$currentPlatformPackage->getName()] = $currentPlatformPackage; + } + + $results = array(); + + /** + * @var Link $require + */ + foreach ($requires as $key => $require) { + if (preg_match(PlatformRepository::PLATFORM_PACKAGE_REGEX, $key)) { + if (isset($currentPlatformPackageMap[$key])) { + // 检查版本 + $version = $currentPlatformPackageMap[$key]->getVersion(); + if (!$require->getConstraint()->matches(new Constraint('<=', $version))) { + $results[] = array( + $require, + $currentPlatformPackageMap[$key], + 'failed', + ); + } else { + $results[] = array( + $require, + $currentPlatformPackageMap[$key], + 'success', + ); + } + } else { + $results[] = array( + $require, + null, + 'miss', + ); + } + } + } + + $this->printTable($output, $results); + + } + + protected function printTable(OutputInterface $output, $results) + { + $table = array(); + $rows = array(); + foreach ($results as $result) { + /** + * @var PackageInterface $platformPackage + * @var Link $require + */ + list($require, $platformPackage, $reason) = $result; + $version = (strpos($platformPackage->getPrettyVersion(), 'No version set') === 0) ? '-' : $platformPackage->getPrettyVersion(); + $rows[] = array( + $platformPackage->getPrettyName(), + $version, + $require->getDescription(), + sprintf('%s (%s)', $require->getTarget(), $require->getPrettyConstraint()), + $reason + ); + } + $table = array_merge($rows, $table); + + // Render table + $renderer = new Table($output); + $renderer->setStyle('compact'); + $renderer->getStyle()->setVerticalBorderChar(''); + $renderer->getStyle()->setCellRowContentFormat('%s '); + $renderer->setRows($table)->render(); + } +} \ No newline at end of file diff --git a/src/Composer/Console/Application.php b/src/Composer/Console/Application.php index 6828feec9..d8ab8eb76 100644 --- a/src/Composer/Console/Application.php +++ b/src/Composer/Console/Application.php @@ -398,6 +398,7 @@ class Application extends BaseApplication new Command\HomeCommand(), new Command\ExecCommand(), new Command\OutdatedCommand(), + new Command\CheckPlatformReqsCommand(), )); if ('phar:' === substr(__FILE__, 0, 5)) {