Pool.php 6.5 KB

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