DefaultPolicy.php 8.8 KB

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