DefaultPolicy.php 7.1 KB

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