Pool.php 5.7 KB

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