123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- <?php
- namespace Composer\Package\Version;
- use Composer\DependencyResolver\Pool;
- use Composer\Package\BasePackage;
- use Composer\Package\PackageInterface;
- use Composer\Package\Loader\ArrayLoader;
- use Composer\Package\Dumper\ArrayDumper;
- use Composer\Repository\RepositorySet;
- use Composer\Semver\Constraint\Constraint;
- class VersionSelector
- {
- private $repositorySet;
- private $parser;
- public function __construct(RepositorySet $repositorySet)
- {
- $this->repositorySet = $repositorySet;
- }
-
- public function findBestCandidate($packageName, $targetPackageVersion = null, $targetPhpVersion = null, $preferredStability = 'stable')
- {
- $constraint = $targetPackageVersion ? $this->getParser()->parseConstraints($targetPackageVersion) : null;
- $candidates = $this->repositorySet->findPackages(strtolower($packageName), $constraint);
- if ($targetPhpVersion) {
- $phpConstraint = new Constraint('==', $this->getParser()->normalize($targetPhpVersion));
- $candidates = array_filter($candidates, function ($pkg) use ($phpConstraint) {
- $reqs = $pkg->getRequires();
- return !isset($reqs['php']) || $reqs['php']->getConstraint()->matches($phpConstraint);
- });
- }
- if (!$candidates) {
- return false;
- }
-
- $package = reset($candidates);
- $minPriority = BasePackage::$stabilities[$preferredStability];
- foreach ($candidates as $candidate) {
- $candidatePriority = $candidate->getStabilityPriority();
- $currentPriority = $package->getStabilityPriority();
-
-
- if ($minPriority < $candidatePriority && $currentPriority < $candidatePriority) {
- continue;
- }
-
-
- if ($minPriority < $candidatePriority && $candidatePriority < $currentPriority) {
- $package = $candidate;
- continue;
- }
-
-
- if ($minPriority >= $candidatePriority && $minPriority < $currentPriority) {
- $package = $candidate;
- continue;
- }
-
- if (version_compare($package->getVersion(), $candidate->getVersion(), '<')) {
- $package = $candidate;
- }
- }
- return $package;
- }
-
- public function findRecommendedRequireVersion(PackageInterface $package)
- {
- $version = $package->getVersion();
- if (!$package->isDev()) {
- return $this->transformVersion($version, $package->getPrettyVersion(), $package->getStability());
- }
- $loader = new ArrayLoader($this->getParser());
- $dumper = new ArrayDumper();
- $extra = $loader->getBranchAlias($dumper->dump($package));
- if ($extra) {
- $extra = preg_replace('{^(\d+\.\d+\.\d+)(\.9999999)-dev$}', '$1.0', $extra, -1, $count);
- if ($count) {
- $extra = str_replace('.9999999', '.0', $extra);
- return $this->transformVersion($extra, $extra, 'dev');
- }
- }
- return $package->getPrettyVersion();
- }
- private function transformVersion($version, $prettyVersion, $stability)
- {
-
-
- $semanticVersionParts = explode('.', $version);
-
- if (count($semanticVersionParts) == 4 && preg_match('{^0\D?}', $semanticVersionParts[3])) {
-
- if ($semanticVersionParts[0] === '0') {
- unset($semanticVersionParts[3]);
- } else {
- unset($semanticVersionParts[2], $semanticVersionParts[3]);
- }
- $version = implode('.', $semanticVersionParts);
- } else {
- return $prettyVersion;
- }
-
- if ($stability != 'stable') {
- $version .= '@'.$stability;
- }
-
- return '^' . $version;
- }
- private function getParser()
- {
- if ($this->parser === null) {
- $this->parser = new VersionParser();
- }
- return $this->parser;
- }
- }
|