Pool.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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\AliasPackage;
  13. use Composer\Package\Version\VersionParser;
  14. use Composer\Semver\Constraint\ConstraintInterface;
  15. use Composer\Semver\Constraint\Constraint;
  16. use Composer\Semver\Constraint\EmptyConstraint;
  17. use Composer\Package\PackageInterface;
  18. /**
  19. * A package pool contains all packages for dependency resolution
  20. *
  21. * @author Nils Adermann <naderman@naderman.de>
  22. * @author Jordi Boggiano <j.boggiano@seld.be>
  23. */
  24. class Pool implements \Countable
  25. {
  26. const MATCH_NONE = 0;
  27. const MATCH = 1;
  28. const MATCH_PROVIDE = 2;
  29. const MATCH_REPLACE = 3;
  30. protected $packages = array();
  31. protected $packageByName = array();
  32. protected $packageByExactName = array();
  33. protected $versionParser;
  34. protected $providerCache = array();
  35. protected $unacceptableFixedPackages;
  36. public function __construct(array $packages = array(), array $unacceptableFixedPackages = array())
  37. {
  38. $this->versionParser = new VersionParser;
  39. $this->setPackages($packages);
  40. $this->unacceptableFixedPackages = $unacceptableFixedPackages;
  41. }
  42. private function setPackages(array $packages)
  43. {
  44. $id = 1;
  45. foreach ($packages as $package) {
  46. $this->packages[] = $package;
  47. $package->id = $id++;
  48. $this->packageByExactName[$package->getName()][$package->id] = $package;
  49. foreach ($package->getNames() as $provided) {
  50. $this->packageByName[$provided][] = $package;
  51. }
  52. }
  53. }
  54. /**
  55. * Retrieves the package object for a given package id.
  56. *
  57. * @param int $id
  58. * @return PackageInterface
  59. */
  60. public function packageById($id)
  61. {
  62. return $this->packages[$id - 1];
  63. }
  64. /**
  65. * Returns how many packages have been loaded into the pool
  66. */
  67. public function count()
  68. {
  69. return count($this->packages);
  70. }
  71. /**
  72. * Searches all packages providing the given package name and match the constraint
  73. *
  74. * @param string $name The package name to be searched for
  75. * @param ConstraintInterface $constraint A constraint that all returned
  76. * packages must match or null to return all
  77. * @param bool $mustMatchName Whether the name of returned packages
  78. * must match the given name
  79. * @return PackageInterface[] A set of packages
  80. */
  81. public function whatProvides($name, ConstraintInterface $constraint = null, $mustMatchName = false)
  82. {
  83. $key = ((int) $mustMatchName).$constraint;
  84. if (isset($this->providerCache[$name][$key])) {
  85. return $this->providerCache[$name][$key];
  86. }
  87. return $this->providerCache[$name][$key] = $this->computeWhatProvides($name, $constraint, $mustMatchName);
  88. }
  89. /**
  90. * @see whatProvides
  91. */
  92. private function computeWhatProvides($name, $constraint, $mustMatchName = false)
  93. {
  94. $candidates = array();
  95. if ($mustMatchName) {
  96. if (isset($this->packageByExactName[$name])) {
  97. $candidates = $this->packageByExactName[$name];
  98. }
  99. } elseif (isset($this->packageByName[$name])) {
  100. $candidates = $this->packageByName[$name];
  101. }
  102. $matches = array();
  103. foreach ($candidates as $candidate) {
  104. switch ($this->match($candidate, $name, $constraint)) {
  105. case self::MATCH_NONE:
  106. break;
  107. case self::MATCH:
  108. case self::MATCH_PROVIDE:
  109. case self::MATCH_REPLACE:
  110. $matches[] = $candidate;
  111. break;
  112. default:
  113. throw new \UnexpectedValueException('Unexpected match type');
  114. }
  115. }
  116. return $matches;
  117. }
  118. public function literalToPackage($literal)
  119. {
  120. $packageId = abs($literal);
  121. return $this->packageById($packageId);
  122. }
  123. public function literalToPrettyString($literal, $installedMap)
  124. {
  125. $package = $this->literalToPackage($literal);
  126. if (isset($installedMap[$package->id])) {
  127. $prefix = ($literal > 0 ? 'keep' : 'remove');
  128. } else {
  129. $prefix = ($literal > 0 ? 'install' : 'don\'t install');
  130. }
  131. return $prefix.' '.$package->getPrettyString();
  132. }
  133. /**
  134. * Checks if the package matches the given constraint directly or through
  135. * provided or replaced packages
  136. *
  137. * @param PackageInterface $candidate
  138. * @param string $name Name of the package to be matched
  139. * @param ConstraintInterface $constraint The constraint to verify
  140. * @return int One of the MATCH* constants of this class or 0 if there is no match
  141. */
  142. public function match($candidate, $name, ConstraintInterface $constraint = null)
  143. {
  144. $candidateName = $candidate->getName();
  145. $candidateVersion = $candidate->getVersion();
  146. if ($candidateName === $name) {
  147. $pkgConstraint = new Constraint('==', $candidateVersion);
  148. if ($constraint === null || $constraint->matches($pkgConstraint)) {
  149. return self::MATCH;
  150. }
  151. return self::MATCH_NONE;
  152. }
  153. $provides = $candidate->getProvides();
  154. $replaces = $candidate->getReplaces();
  155. // aliases create multiple replaces/provides for one target so they can not use the shortcut below
  156. if (isset($replaces[0]) || isset($provides[0])) {
  157. foreach ($provides as $link) {
  158. if ($link->getTarget() === $name && ($constraint === null || $constraint->matches($link->getConstraint()))) {
  159. return self::MATCH_PROVIDE;
  160. }
  161. }
  162. foreach ($replaces as $link) {
  163. if ($link->getTarget() === $name && ($constraint === null || $constraint->matches($link->getConstraint()))) {
  164. return self::MATCH_REPLACE;
  165. }
  166. }
  167. return self::MATCH_NONE;
  168. }
  169. if (isset($provides[$name]) && ($constraint === null || $constraint->matches($provides[$name]->getConstraint()))) {
  170. return self::MATCH_PROVIDE;
  171. }
  172. if (isset($replaces[$name]) && ($constraint === null || $constraint->matches($replaces[$name]->getConstraint()))) {
  173. return self::MATCH_REPLACE;
  174. }
  175. return self::MATCH_NONE;
  176. }
  177. public function isUnacceptableFixedPackage(PackageInterface $package)
  178. {
  179. return in_array($package, $this->unacceptableFixedPackages, true);
  180. }
  181. }