DefaultPolicy.php 8.4 KB

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