DefaultPolicy.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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\Package\LinkConstraint\VersionConstraint;
  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 VersionConstraint($operator, $b->getVersion());
  35. $version = new VersionConstraint('==', $a->getVersion());
  36. return $constraint->matchSpecific($version, true);
  37. }
  38. public function findUpdatePackages(Pool $pool, array $installedMap, PackageInterface $package, $mustMatchName = false)
  39. {
  40. $packages = array();
  41. foreach ($pool->whatProvides($package->getName(), null, $mustMatchName) as $candidate) {
  42. if ($candidate !== $package) {
  43. $packages[] = $candidate;
  44. }
  45. }
  46. return $packages;
  47. }
  48. public function getPriority(Pool $pool, PackageInterface $package)
  49. {
  50. return $pool->getPriority($package->getRepository());
  51. }
  52. public function selectPreferredPackages(Pool $pool, array $installedMap, array $literals, $requiredPackage = null)
  53. {
  54. $packages = $this->groupLiteralsByNamePreferInstalled($pool, $installedMap, $literals);
  55. foreach ($packages as &$literals) {
  56. $policy = $this;
  57. usort($literals, function ($a, $b) use ($policy, $pool, $installedMap, $requiredPackage) {
  58. return $policy->compareByPriorityPreferInstalled($pool, $installedMap, $pool->literalToPackage($a), $pool->literalToPackage($b), $requiredPackage, true);
  59. });
  60. }
  61. foreach ($packages as &$literals) {
  62. $literals = $this->pruneToBestVersion($pool, $literals);
  63. $literals = $this->pruneToHighestPriorityOrInstalled($pool, $installedMap, $literals);
  64. $literals = $this->pruneRemoteAliases($pool, $literals);
  65. }
  66. $selected = call_user_func_array('array_merge', $packages);
  67. // now sort the result across all packages to respect replaces across packages
  68. usort($selected, function ($a, $b) use ($policy, $pool, $installedMap, $requiredPackage) {
  69. return $policy->compareByPriorityPreferInstalled($pool, $installedMap, $pool->literalToPackage($a), $pool->literalToPackage($b), $requiredPackage);
  70. });
  71. return $selected;
  72. }
  73. protected function groupLiteralsByNamePreferInstalled(Pool $pool, array $installedMap, $literals)
  74. {
  75. $packages = array();
  76. foreach ($literals as $literal) {
  77. $packageName = $pool->literalToPackage($literal)->getName();
  78. if (!isset($packages[$packageName])) {
  79. $packages[$packageName] = array();
  80. }
  81. if (isset($installedMap[abs($literal)])) {
  82. array_unshift($packages[$packageName], $literal);
  83. } else {
  84. $packages[$packageName][] = $literal;
  85. }
  86. }
  87. return $packages;
  88. }
  89. /**
  90. * @protected
  91. */
  92. public function compareByPriorityPreferInstalled(Pool $pool, array $installedMap, PackageInterface $a, PackageInterface $b, $requiredPackage = null, $ignoreReplace = false)
  93. {
  94. if ($a->getRepository() === $b->getRepository()) {
  95. // prefer aliases to the original package
  96. if ($a->getName() === $b->getName()) {
  97. $aAliased = $a instanceof AliasPackage;
  98. $bAliased = $b instanceof AliasPackage;
  99. if ($aAliased && !$bAliased) {
  100. return -1; // use a
  101. }
  102. if (!$aAliased && $bAliased) {
  103. return 1; // use b
  104. }
  105. }
  106. if (!$ignoreReplace) {
  107. // return original, not replaced
  108. if ($this->replaces($a, $b)) {
  109. return 1; // use b
  110. }
  111. if ($this->replaces($b, $a)) {
  112. return -1; // use a
  113. }
  114. // for replacers not replacing each other, put a higher prio on replacing
  115. // packages with the same vendor as the required package
  116. if ($requiredPackage && false !== ($pos = strpos($requiredPackage, '/'))) {
  117. $requiredVendor = substr($requiredPackage, 0, $pos);
  118. $aIsSameVendor = substr($a->getName(), 0, $pos) === $requiredVendor;
  119. $bIsSameVendor = substr($b->getName(), 0, $pos) === $requiredVendor;
  120. if ($bIsSameVendor !== $aIsSameVendor) {
  121. return $aIsSameVendor ? -1 : 1;
  122. }
  123. }
  124. }
  125. // priority equal, sort by package id to make reproducible
  126. if ($a->id === $b->id) {
  127. return 0;
  128. }
  129. return ($a->id < $b->id) ? -1 : 1;
  130. }
  131. if (isset($installedMap[$a->id])) {
  132. return -1;
  133. }
  134. if (isset($installedMap[$b->id])) {
  135. return 1;
  136. }
  137. return ($this->getPriority($pool, $a) > $this->getPriority($pool, $b)) ? -1 : 1;
  138. }
  139. /**
  140. * Checks if source replaces a package with the same name as target.
  141. *
  142. * Replace constraints are ignored. This method should only be used for
  143. * prioritisation, not for actual constraint verification.
  144. *
  145. * @param PackageInterface $source
  146. * @param PackageInterface $target
  147. * @return bool
  148. */
  149. protected function replaces(PackageInterface $source, PackageInterface $target)
  150. {
  151. foreach ($source->getReplaces() as $link) {
  152. if ($link->getTarget() === $target->getName()
  153. // && (null === $link->getConstraint() ||
  154. // $link->getConstraint()->matches(new VersionConstraint('==', $target->getVersion())))) {
  155. ) {
  156. return true;
  157. }
  158. }
  159. return false;
  160. }
  161. protected function pruneToBestVersion(Pool $pool, $literals)
  162. {
  163. $operator = $this->preferLowest ? '<' : '>';
  164. $bestLiterals = array($literals[0]);
  165. $bestPackage = $pool->literalToPackage($literals[0]);
  166. foreach ($literals as $i => $literal) {
  167. if (0 === $i) {
  168. continue;
  169. }
  170. $package = $pool->literalToPackage($literal);
  171. if ($this->versionCompare($package, $bestPackage, $operator)) {
  172. $bestPackage = $package;
  173. $bestLiterals = array($literal);
  174. } elseif ($this->versionCompare($package, $bestPackage, '==')) {
  175. $bestLiterals[] = $literal;
  176. }
  177. }
  178. return $bestLiterals;
  179. }
  180. /**
  181. * Assumes that installed packages come first and then all highest priority packages
  182. */
  183. protected function pruneToHighestPriorityOrInstalled(Pool $pool, array $installedMap, array $literals)
  184. {
  185. $selected = array();
  186. $priority = null;
  187. foreach ($literals as $literal) {
  188. $package = $pool->literalToPackage($literal);
  189. if (isset($installedMap[$package->id])) {
  190. $selected[] = $literal;
  191. continue;
  192. }
  193. if (null === $priority) {
  194. $priority = $this->getPriority($pool, $package);
  195. }
  196. if ($this->getPriority($pool, $package) != $priority) {
  197. break;
  198. }
  199. $selected[] = $literal;
  200. }
  201. return $selected;
  202. }
  203. /**
  204. * Assumes that locally aliased (in root package requires) packages take priority over branch-alias ones
  205. *
  206. * If no package is a local alias, nothing happens
  207. */
  208. protected function pruneRemoteAliases(Pool $pool, array $literals)
  209. {
  210. $hasLocalAlias = false;
  211. foreach ($literals as $literal) {
  212. $package = $pool->literalToPackage($literal);
  213. if ($package instanceof AliasPackage && $package->isRootPackageAlias()) {
  214. $hasLocalAlias = true;
  215. break;
  216. }
  217. }
  218. if (!$hasLocalAlias) {
  219. return $literals;
  220. }
  221. $selected = array();
  222. foreach ($literals as $literal) {
  223. $package = $pool->literalToPackage($literal);
  224. if ($package instanceof AliasPackage && $package->isRootPackageAlias()) {
  225. $selected[] = $literal;
  226. }
  227. }
  228. return $selected;
  229. }
  230. }