From f83883b2f69c7e93dfafdd0bc7f92ef6027bab28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=97=AB=E5=85=B4=E8=8C=82?= Date: Wed, 27 Sep 2017 17:20:51 +0800 Subject: [PATCH 1/2] Add command check-platform-reqs --- .../Command/CheckPlatformReqsCommand.php | 113 ++++++++++++++++++ src/Composer/Console/Application.php | 1 + 2 files changed, 114 insertions(+) create mode 100644 src/Composer/Command/CheckPlatformReqsCommand.php diff --git a/src/Composer/Command/CheckPlatformReqsCommand.php b/src/Composer/Command/CheckPlatformReqsCommand.php new file mode 100644 index 000000000..dc24e0634 --- /dev/null +++ b/src/Composer/Command/CheckPlatformReqsCommand.php @@ -0,0 +1,113 @@ +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[] = [ + $require, + $currentPlatformPackageMap[$key], + 'failed', + ]; + } else { + $results[] = [ + $require, + $currentPlatformPackageMap[$key], + 'success', + ]; + } + } else { + $results[] = [ + $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[] = [$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 1cff9241b..cc373a38d 100644 --- a/src/Composer/Console/Application.php +++ b/src/Composer/Console/Application.php @@ -397,6 +397,7 @@ class Application extends BaseApplication new Command\HomeCommand(), new Command\ExecCommand(), new Command\OutdatedCommand(), + new Command\CheckPlatformReqsCommand(), )); if ('phar:' === substr(__FILE__, 0, 5)) { From fb306b6eb746033d53aaabef90f1766ae68a915b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=97=AB=E5=85=B4=E8=8C=82?= Date: Wed, 27 Sep 2017 17:32:46 +0800 Subject: [PATCH 2/2] Update [] to array synax --- .../Command/CheckPlatformReqsCommand.php | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/Composer/Command/CheckPlatformReqsCommand.php b/src/Composer/Command/CheckPlatformReqsCommand.php index dc24e0634..c03ed0804 100644 --- a/src/Composer/Command/CheckPlatformReqsCommand.php +++ b/src/Composer/Command/CheckPlatformReqsCommand.php @@ -62,24 +62,24 @@ EOT // 检查版本 $version = $currentPlatformPackageMap[$key]->getVersion(); if (!$require->getConstraint()->matches(new Constraint('<=', $version))) { - $results[] = [ + $results[] = array( $require, $currentPlatformPackageMap[$key], 'failed', - ]; + ); } else { - $results[] = [ + $results[] = array( $require, $currentPlatformPackageMap[$key], 'success', - ]; + ); } } else { - $results[] = [ + $results[] = array( $require, null, 'miss', - ]; + ); } } } @@ -99,7 +99,13 @@ EOT */ list($require, $platformPackage, $reason) = $result; $version = (strpos($platformPackage->getPrettyVersion(), 'No version set') === 0) ? '-' : $platformPackage->getPrettyVersion(); - $rows[] = [$platformPackage->getPrettyName(), $version, $require->getDescription(), sprintf('%s (%s)', $require->getTarget(), $require->getPrettyConstraint()), $reason]; + $rows[] = array( + $platformPackage->getPrettyName(), + $version, + $require->getDescription(), + sprintf('%s (%s)', $require->getTarget(), $require->getPrettyConstraint()), + $reason + ); } $table = array_merge($rows, $table);