Pool.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. // TODO BC change to stable end of june?
  32. public function __construct($minimumStability = 'dev', array $stabilityFlags = array())
  33. {
  34. $stabilities = BasePackage::$stabilities;
  35. $this->acceptableStabilities = array();
  36. foreach (BasePackage::$stabilities as $stability => $value) {
  37. if ($value <= BasePackage::$stabilities[$minimumStability]) {
  38. $this->acceptableStabilities[$stability] = $value;
  39. }
  40. }
  41. $this->stabilityFlags = $stabilityFlags;
  42. }
  43. /**
  44. * Adds a repository and its packages to this package pool
  45. *
  46. * @param RepositoryInterface $repo A package repository
  47. */
  48. public function addRepository(RepositoryInterface $repo)
  49. {
  50. if ($repo instanceof CompositeRepository) {
  51. $repos = $repo->getRepositories();
  52. } else {
  53. $repos = array($repo);
  54. }
  55. $id = count($this->packages) + 1;
  56. foreach ($repos as $repo) {
  57. $this->repositories[] = $repo;
  58. $exempt = $repo instanceof PlatformRepository || $repo instanceof InstalledRepositoryInterface;
  59. foreach ($repo->getPackages() as $package) {
  60. $name = $package->getName();
  61. $stability = $package->getStability();
  62. if (
  63. // always allow exempt repos
  64. $exempt
  65. // allow if package matches the global stability requirement and has no exception
  66. || (!isset($this->stabilityFlags[$name])
  67. && isset($this->acceptableStabilities[$stability]))
  68. // allow if package matches the package-specific stability flag
  69. || (isset($this->stabilityFlags[$name])
  70. && BasePackage::$stabilities[$stability] <= $this->stabilityFlags[$name]
  71. )
  72. ) {
  73. $package->setId($id++);
  74. $this->packages[] = $package;
  75. foreach ($package->getNames() as $name) {
  76. $this->packageByName[$name][] = $package;
  77. }
  78. }
  79. }
  80. }
  81. }
  82. public function getPriority(RepositoryInterface $repo)
  83. {
  84. $priority = array_search($repo, $this->repositories, true);
  85. if (false === $priority) {
  86. throw new \RuntimeException("Could not determine repository priority. The repository was not registered in the pool.");
  87. }
  88. return -$priority;
  89. }
  90. /**
  91. * Retrieves the package object for a given package id.
  92. *
  93. * @param int $id
  94. * @return PackageInterface
  95. */
  96. public function packageById($id)
  97. {
  98. return $this->packages[$id - 1];
  99. }
  100. /**
  101. * Retrieves the highest id assigned to a package in this pool
  102. *
  103. * @return int Highest package id
  104. */
  105. public function getMaxId()
  106. {
  107. return count($this->packages);
  108. }
  109. /**
  110. * Searches all packages providing the given package name and match the constraint
  111. *
  112. * @param string $name The package name to be searched for
  113. * @param LinkConstraintInterface $constraint A constraint that all returned
  114. * packages must match or null to return all
  115. * @return array A set of packages
  116. */
  117. public function whatProvides($name, LinkConstraintInterface $constraint = null)
  118. {
  119. if (!isset($this->packageByName[$name])) {
  120. return array();
  121. }
  122. $candidates = $this->packageByName[$name];
  123. if (null === $constraint) {
  124. return $candidates;
  125. }
  126. $result = array();
  127. foreach ($candidates as $candidate) {
  128. if ($candidate->matches($name, $constraint)) {
  129. $result[] = $candidate;
  130. }
  131. }
  132. return $result;
  133. }
  134. public function literalToPackage($literal)
  135. {
  136. $packageId = abs($literal);
  137. return $this->packageById($packageId);
  138. }
  139. public function literalToString($literal)
  140. {
  141. return ($literal > 0 ? '+' : '-') . $this->literalToPackage($literal);
  142. }
  143. }