DefaultPolicy.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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, true);
  35. }
  36. public function findUpdatePackages(Pool $pool, array $installedMap, PackageInterface $package, $mustMatchName = false)
  37. {
  38. $packages = array();
  39. foreach ($pool->whatProvides($package->getName(), null, $mustMatchName) 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, $requiredPackage = null)
  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, $requiredPackage) {
  56. return $policy->compareByPriorityPreferInstalled($pool, $installedMap, $pool->literalToPackage($a), $pool->literalToPackage($b), $requiredPackage, 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, $requiredPackage) {
  67. return $policy->compareByPriorityPreferInstalled($pool, $installedMap, $pool->literalToPackage($a), $pool->literalToPackage($b), $requiredPackage);
  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. /**
  88. * @protected
  89. */
  90. public function compareByPriorityPreferInstalled(Pool $pool, array $installedMap, PackageInterface $a, PackageInterface $b, $requiredPackage = null, $ignoreReplace = false)
  91. {
  92. if ($a->getRepository() === $b->getRepository()) {
  93. // prefer aliases to the original package
  94. if ($a->getName() === $b->getName()) {
  95. $aAliased = $a instanceof AliasPackage;
  96. $bAliased = $b instanceof AliasPackage;
  97. if ($aAliased && !$bAliased) {
  98. return -1; // use a
  99. }
  100. if (!$aAliased && $bAliased) {
  101. return 1; // use b
  102. }
  103. }
  104. if (!$ignoreReplace) {
  105. // return original, not replaced
  106. if ($this->replaces($a, $b)) {
  107. return 1; // use b
  108. }
  109. if ($this->replaces($b, $a)) {
  110. return -1; // use a
  111. }
  112. // for replacers not replacing each other, put a higher prio on replacing
  113. // packages with the same vendor as the required package
  114. if ($requiredPackage && false !== ($pos = strpos($requiredPackage, '/'))) {
  115. $requiredVendor = substr($requiredPackage, 0, $pos);
  116. $aIsSameVendor = substr($a->getName(), 0, $pos) === $requiredVendor;
  117. $bIsSameVendor = substr($b->getName(), 0, $pos) === $requiredVendor;
  118. if ($bIsSameVendor !== $aIsSameVendor) {
  119. return $aIsSameVendor ? -1 : 1;
  120. }
  121. }
  122. }
  123. // priority equal, sort by package id to make reproducible
  124. if ($a->getId() === $b->getId()) {
  125. return 0;
  126. }
  127. return ($a->getId() < $b->getId()) ? -1 : 1;
  128. }
  129. if (isset($installedMap[$a->getId()])) {
  130. return -1;
  131. }
  132. if (isset($installedMap[$b->getId()])) {
  133. return 1;
  134. }
  135. return ($this->getPriority($pool, $a) > $this->getPriority($pool, $b)) ? -1 : 1;
  136. }
  137. /**
  138. * Checks if source replaces a package with the same name as target.
  139. *
  140. * Replace constraints are ignored. This method should only be used for
  141. * prioritisation, not for actual constraint verification.
  142. *
  143. * @param PackageInterface $source
  144. * @param PackageInterface $target
  145. * @return bool
  146. */
  147. protected function replaces(PackageInterface $source, PackageInterface $target)
  148. {
  149. foreach ($source->getReplaces() as $link) {
  150. if ($link->getTarget() === $target->getName()
  151. // && (null === $link->getConstraint() ||
  152. // $link->getConstraint()->matches(new VersionConstraint('==', $target->getVersion())))) {
  153. ) {
  154. return true;
  155. }
  156. }
  157. return false;
  158. }
  159. protected function pruneToBestVersion(Pool $pool, $literals)
  160. {
  161. $bestLiterals = array($literals[0]);
  162. $bestPackage = $pool->literalToPackage($literals[0]);
  163. foreach ($literals as $i => $literal) {
  164. if (0 === $i) {
  165. continue;
  166. }
  167. $package = $pool->literalToPackage($literal);
  168. if ($this->versionCompare($package, $bestPackage, '>')) {
  169. $bestPackage = $package;
  170. $bestLiterals = array($literal);
  171. } elseif ($this->versionCompare($package, $bestPackage, '==')) {
  172. $bestLiterals[] = $literal;
  173. }
  174. }
  175. return $bestLiterals;
  176. }
  177. protected function selectNewestPackages(array $installedMap, array $literals)
  178. {
  179. $maxLiterals = array($literals[0]);
  180. $maxPackage = $literals[0]->getPackage();
  181. foreach ($literals as $i => $literal) {
  182. if (0 === $i) {
  183. continue;
  184. }
  185. if ($this->versionCompare($literal->getPackage(), $maxPackage, '>')) {
  186. $maxPackage = $literal->getPackage();
  187. $maxLiterals = array($literal);
  188. } elseif ($this->versionCompare($literal->getPackage(), $maxPackage, '==')) {
  189. $maxLiterals[] = $literal;
  190. }
  191. }
  192. return $maxLiterals;
  193. }
  194. /**
  195. * Assumes that installed packages come first and then all highest priority packages
  196. */
  197. protected function pruneToHighestPriorityOrInstalled(Pool $pool, array $installedMap, array $literals)
  198. {
  199. $selected = array();
  200. $priority = null;
  201. foreach ($literals as $literal) {
  202. $package = $pool->literalToPackage($literal);
  203. if (isset($installedMap[$package->getId()])) {
  204. $selected[] = $literal;
  205. continue;
  206. }
  207. if (null === $priority) {
  208. $priority = $this->getPriority($pool, $package);
  209. }
  210. if ($this->getPriority($pool, $package) != $priority) {
  211. break;
  212. }
  213. $selected[] = $literal;
  214. }
  215. return $selected;
  216. }
  217. /**
  218. * Assumes that locally aliased (in root package requires) packages take priority over branch-alias ones
  219. *
  220. * If no package is a local alias, nothing happens
  221. */
  222. protected function pruneRemoteAliases(Pool $pool, array $literals)
  223. {
  224. $hasLocalAlias = false;
  225. foreach ($literals as $literal) {
  226. $package = $pool->literalToPackage($literal);
  227. if ($package instanceof AliasPackage && $package->isRootPackageAlias()) {
  228. $hasLocalAlias = true;
  229. break;
  230. }
  231. }
  232. if (!$hasLocalAlias) {
  233. return $literals;
  234. }
  235. $selected = array();
  236. foreach ($literals as $literal) {
  237. $package = $pool->literalToPackage($literal);
  238. if ($package instanceof AliasPackage && $package->isRootPackageAlias()) {
  239. $selected[] = $literal;
  240. }
  241. }
  242. return $selected;
  243. }
  244. }