DefaultPolicy.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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\DependencyResolver;
  12. use Composer\Package\PackageInterface;
  13. use Composer\Package\AliasPackage;
  14. use Composer\Package\BasePackage;
  15. use Composer\Semver\Constraint\Constraint;
  16. /**
  17. * @author Nils Adermann <naderman@naderman.de>
  18. * @author Jordi Boggiano <j.boggiano@seld.be>
  19. */
  20. class DefaultPolicy implements PolicyInterface
  21. {
  22. private $preferStable;
  23. private $preferLowest;
  24. public function __construct($preferStable = false, $preferLowest = false)
  25. {
  26. $this->preferStable = $preferStable;
  27. $this->preferLowest = $preferLowest;
  28. }
  29. public function versionCompare(PackageInterface $a, PackageInterface $b, $operator)
  30. {
  31. if ($this->preferStable && ($stabA = $a->getStability()) !== ($stabB = $b->getStability())) {
  32. return BasePackage::$stabilities[$stabA] < BasePackage::$stabilities[$stabB];
  33. }
  34. $constraint = new Constraint($operator, $b->getVersion());
  35. $version = new Constraint('==', $a->getVersion());
  36. return $constraint->matchSpecific($version, true);
  37. }
  38. public function selectPreferredPackages(Pool $pool, array $literals, $requiredPackage = null)
  39. {
  40. $packages = $this->groupLiteralsByName($pool, $literals);
  41. foreach ($packages as &$nameLiterals) {
  42. $policy = $this;
  43. usort($nameLiterals, function ($a, $b) use ($policy, $pool, $requiredPackage) {
  44. return $policy->compareByPriority($pool, $pool->literalToPackage($a), $pool->literalToPackage($b), $requiredPackage, true);
  45. });
  46. }
  47. foreach ($packages as &$sortedLiterals) {
  48. $sortedLiterals = $this->pruneToBestVersion($pool, $sortedLiterals);
  49. $sortedLiterals = $this->pruneRemoteAliases($pool, $sortedLiterals);
  50. }
  51. $selected = call_user_func_array('array_merge', $packages);
  52. // now sort the result across all packages to respect replaces across packages
  53. usort($selected, function ($a, $b) use ($policy, $pool, $requiredPackage) {
  54. return $policy->compareByPriority($pool, $pool->literalToPackage($a), $pool->literalToPackage($b), $requiredPackage);
  55. });
  56. return $selected;
  57. }
  58. protected function groupLiteralsByName(Pool $pool, $literals)
  59. {
  60. $packages = array();
  61. foreach ($literals as $literal) {
  62. $packageName = $pool->literalToPackage($literal)->getName();
  63. if (!isset($packages[$packageName])) {
  64. $packages[$packageName] = array();
  65. }
  66. $packages[$packageName][] = $literal;
  67. }
  68. return $packages;
  69. }
  70. /**
  71. * @protected
  72. */
  73. public function compareByPriority(Pool $pool, PackageInterface $a, PackageInterface $b, $requiredPackage = null, $ignoreReplace = false)
  74. {
  75. // prefer aliases to the original package
  76. if ($a->getName() === $b->getName()) {
  77. $aAliased = $a instanceof AliasPackage;
  78. $bAliased = $b instanceof AliasPackage;
  79. if ($aAliased && !$bAliased) {
  80. return -1; // use a
  81. }
  82. if (!$aAliased && $bAliased) {
  83. return 1; // use b
  84. }
  85. }
  86. if (!$ignoreReplace) {
  87. // return original, not replaced
  88. if ($this->replaces($a, $b)) {
  89. return 1; // use b
  90. }
  91. if ($this->replaces($b, $a)) {
  92. return -1; // use a
  93. }
  94. // for replacers not replacing each other, put a higher prio on replacing
  95. // packages with the same vendor as the required package
  96. if ($requiredPackage && false !== ($pos = strpos($requiredPackage, '/'))) {
  97. $requiredVendor = substr($requiredPackage, 0, $pos);
  98. $aIsSameVendor = substr($a->getName(), 0, $pos) === $requiredVendor;
  99. $bIsSameVendor = substr($b->getName(), 0, $pos) === $requiredVendor;
  100. if ($bIsSameVendor !== $aIsSameVendor) {
  101. return $aIsSameVendor ? -1 : 1;
  102. }
  103. }
  104. }
  105. // priority equal, sort by package id to make reproducible
  106. if ($a->id === $b->id) {
  107. return 0;
  108. }
  109. return ($a->id < $b->id) ? -1 : 1;
  110. }
  111. /**
  112. * Checks if source replaces a package with the same name as target.
  113. *
  114. * Replace constraints are ignored. This method should only be used for
  115. * prioritisation, not for actual constraint verification.
  116. *
  117. * @param PackageInterface $source
  118. * @param PackageInterface $target
  119. * @return bool
  120. */
  121. protected function replaces(PackageInterface $source, PackageInterface $target)
  122. {
  123. foreach ($source->getReplaces() as $link) {
  124. if ($link->getTarget() === $target->getName()
  125. // && (null === $link->getConstraint() ||
  126. // $link->getConstraint()->matches(new Constraint('==', $target->getVersion())))) {
  127. ) {
  128. return true;
  129. }
  130. }
  131. return false;
  132. }
  133. protected function pruneToBestVersion(Pool $pool, $literals)
  134. {
  135. $operator = $this->preferLowest ? '<' : '>';
  136. $bestLiterals = array($literals[0]);
  137. $bestPackage = $pool->literalToPackage($literals[0]);
  138. foreach ($literals as $i => $literal) {
  139. if (0 === $i) {
  140. continue;
  141. }
  142. $package = $pool->literalToPackage($literal);
  143. if ($this->versionCompare($package, $bestPackage, $operator)) {
  144. $bestPackage = $package;
  145. $bestLiterals = array($literal);
  146. } elseif ($this->versionCompare($package, $bestPackage, '==')) {
  147. $bestLiterals[] = $literal;
  148. }
  149. }
  150. return $bestLiterals;
  151. }
  152. /**
  153. * Assumes that locally aliased (in root package requires) packages take priority over branch-alias ones
  154. *
  155. * If no package is a local alias, nothing happens
  156. */
  157. protected function pruneRemoteAliases(Pool $pool, array $literals)
  158. {
  159. $hasLocalAlias = false;
  160. foreach ($literals as $literal) {
  161. $package = $pool->literalToPackage($literal);
  162. if ($package instanceof AliasPackage && $package->isRootPackageAlias()) {
  163. $hasLocalAlias = true;
  164. break;
  165. }
  166. }
  167. if (!$hasLocalAlias) {
  168. return $literals;
  169. }
  170. $selected = array();
  171. foreach ($literals as $literal) {
  172. $package = $pool->literalToPackage($literal);
  173. if ($package instanceof AliasPackage && $package->isRootPackageAlias()) {
  174. $selected[] = $literal;
  175. }
  176. }
  177. return $selected;
  178. }
  179. }