Pool.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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\BasePackage;
  13. use Composer\Package\LinkConstraint\LinkConstraintInterface;
  14. use Composer\Repository\RepositoryInterface;
  15. use Composer\Repository\CompositeRepository;
  16. use Composer\Repository\InstalledRepositoryInterface;
  17. use Composer\Repository\PlatformRepository;
  18. /**
  19. * A package pool contains repositories that provide packages.
  20. *
  21. * @author Nils Adermann <naderman@naderman.de>
  22. * @author Jordi Boggiano <j.boggiano@seld.be>
  23. */
  24. class Pool
  25. {
  26. protected $repositories = array();
  27. protected $packages = array();
  28. protected $packageByName = array();
  29. protected $acceptableStabilities;
  30. protected $stabilityFlags;
  31. public function __construct($minimumStability = 'stable', array $stabilityFlags = array())
  32. {
  33. $stabilities = BasePackage::$stabilities;
  34. $this->acceptableStabilities = array();
  35. foreach (BasePackage::$stabilities as $stability => $value) {
  36. if ($value <= BasePackage::$stabilities[$minimumStability]) {
  37. $this->acceptableStabilities[$stability] = $value;
  38. }
  39. }
  40. $this->stabilityFlags = $stabilityFlags;
  41. }
  42. /**
  43. * Adds a repository and its packages to this package pool
  44. *
  45. * @param RepositoryInterface $repo A package repository
  46. */
  47. public function addRepository(RepositoryInterface $repo)
  48. {
  49. if ($repo instanceof CompositeRepository) {
  50. $repos = $repo->getRepositories();
  51. } else {
  52. $repos = array($repo);
  53. }
  54. $id = count($this->packages) + 1;
  55. foreach ($repos as $repo) {
  56. $this->repositories[] = $repo;
  57. $exempt = $repo instanceof PlatformRepository || $repo instanceof InstalledRepositoryInterface;
  58. foreach ($repo->getPackages() as $package) {
  59. $name = $package->getName();
  60. $stability = $package->getStability();
  61. if (
  62. // always allow exempt repos
  63. $exempt
  64. // allow if package matches the global stability requirement and has no exception
  65. || (!isset($this->stabilityFlags[$name])
  66. && isset($this->acceptableStabilities[$stability]))
  67. // allow if package matches the package-specific stability flag
  68. || (isset($this->stabilityFlags[$name])
  69. && BasePackage::$stabilities[$stability] <= $this->stabilityFlags[$name]
  70. )
  71. ) {
  72. $package->setId($id++);
  73. $this->packages[] = $package;
  74. foreach ($package->getNames() as $name) {
  75. $this->packageByName[$name][] = $package;
  76. }
  77. }
  78. }
  79. }
  80. }
  81. public function getPriority(RepositoryInterface $repo)
  82. {
  83. $priority = array_search($repo, $this->repositories, true);
  84. if (false === $priority) {
  85. throw new \RuntimeException("Could not determine repository priority. The repository was not registered in the pool.");
  86. }
  87. return -$priority;
  88. }
  89. /**
  90. * Retrieves the package object for a given package id.
  91. *
  92. * @param int $id
  93. * @return PackageInterface
  94. */
  95. public function packageById($id)
  96. {
  97. return $this->packages[$id - 1];
  98. }
  99. /**
  100. * Retrieves the highest id assigned to a package in this pool
  101. *
  102. * @return int Highest package id
  103. */
  104. public function getMaxId()
  105. {
  106. return count($this->packages);
  107. }
  108. /**
  109. * Searches all packages providing the given package name and match the constraint
  110. *
  111. * @param string $name The package name to be searched for
  112. * @param LinkConstraintInterface $constraint A constraint that all returned
  113. * packages must match or null to return all
  114. * @return array A set of packages
  115. */
  116. public function whatProvides($name, LinkConstraintInterface $constraint = null)
  117. {
  118. if (!isset($this->packageByName[$name])) {
  119. return array();
  120. }
  121. $candidates = $this->packageByName[$name];
  122. if (null === $constraint) {
  123. return $candidates;
  124. }
  125. $result = array();
  126. foreach ($candidates as $candidate) {
  127. if ($candidate->matches($name, $constraint)) {
  128. $result[] = $candidate;
  129. }
  130. }
  131. return $result;
  132. }
  133. public function literalToPackage($literal)
  134. {
  135. $packageId = abs($literal);
  136. return $this->packageById($packageId);
  137. }
  138. public function literalToString($literal)
  139. {
  140. return ($literal > 0 ? '+' : '-') . $this->literalToPackage($literal);
  141. }
  142. public function literalToPrettyString($literal, $installedMap)
  143. {
  144. $package = $this->literalToPackage($literal);
  145. if (isset($installedMap[$package->getId()])) {
  146. $prefix = ($literal > 0 ? 'keep' : 'remove');
  147. } else {
  148. $prefix = ($literal > 0 ? 'install' : 'don\'t install');
  149. }
  150. return $prefix.' '.$package->getPrettyString();
  151. }
  152. }