DefaultPolicy.php 7.5 KB

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