Pool.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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\AliasPackage;
  14. use Composer\Package\Version\VersionParser;
  15. use Composer\Package\LinkConstraint\LinkConstraintInterface;
  16. use Composer\Package\LinkConstraint\VersionConstraint;
  17. use Composer\Package\LinkConstraint\EmptyConstraint;
  18. use Composer\Repository\RepositoryInterface;
  19. use Composer\Repository\CompositeRepository;
  20. use Composer\Repository\ComposerRepository;
  21. use Composer\Repository\InstalledRepositoryInterface;
  22. use Composer\Repository\PlatformRepository;
  23. use Composer\Package\PackageInterface;
  24. /**
  25. * A package pool contains repositories that provide packages.
  26. *
  27. * @author Nils Adermann <naderman@naderman.de>
  28. * @author Jordi Boggiano <j.boggiano@seld.be>
  29. */
  30. class Pool
  31. {
  32. const MATCH_NAME = -1;
  33. const MATCH_NONE = 0;
  34. const MATCH = 1;
  35. const MATCH_PROVIDE = 2;
  36. const MATCH_REPLACE = 3;
  37. const MATCH_FILTERED = 4;
  38. protected $repositories = array();
  39. protected $providerRepos = array();
  40. protected $packages = array();
  41. protected $packageByName = array();
  42. protected $packageByExactName = array();
  43. protected $acceptableStabilities;
  44. protected $stabilityFlags;
  45. protected $versionParser;
  46. protected $providerCache = array();
  47. protected $filterRequires;
  48. protected $whitelist = null;
  49. protected $id = 1;
  50. public function __construct($minimumStability = 'stable', array $stabilityFlags = array(), array $filterRequires = array())
  51. {
  52. $stabilities = BasePackage::$stabilities;
  53. $this->versionParser = new VersionParser;
  54. $this->acceptableStabilities = array();
  55. foreach (BasePackage::$stabilities as $stability => $value) {
  56. if ($value <= BasePackage::$stabilities[$minimumStability]) {
  57. $this->acceptableStabilities[$stability] = $value;
  58. }
  59. }
  60. $this->stabilityFlags = $stabilityFlags;
  61. $this->filterRequires = $filterRequires;
  62. }
  63. public function setWhitelist($whitelist)
  64. {
  65. $this->whitelist = $whitelist;
  66. $this->providerCache = array();
  67. }
  68. /**
  69. * Adds a repository and its packages to this package pool
  70. *
  71. * @param RepositoryInterface $repo A package repository
  72. * @param array $rootAliases
  73. */
  74. public function addRepository(RepositoryInterface $repo, $rootAliases = array())
  75. {
  76. if ($repo instanceof CompositeRepository) {
  77. $repos = $repo->getRepositories();
  78. } else {
  79. $repos = array($repo);
  80. }
  81. foreach ($repos as $repo) {
  82. $this->repositories[] = $repo;
  83. $exempt = $repo instanceof PlatformRepository || $repo instanceof InstalledRepositoryInterface;
  84. if ($repo instanceof ComposerRepository && $repo->hasProviders()) {
  85. $this->providerRepos[] = $repo;
  86. $repo->setRootAliases($rootAliases);
  87. $repo->resetPackageIds();
  88. } else {
  89. foreach ($repo->getPackages() as $package) {
  90. $names = $package->getNames();
  91. $stability = $package->getStability();
  92. if ($exempt || $this->isPackageAcceptable($names, $stability)) {
  93. $package->setId($this->id++);
  94. $this->packages[] = $package;
  95. $this->packageByExactName[$package->getName()][$package->id] = $package;
  96. foreach ($names as $provided) {
  97. $this->packageByName[$provided][] = $package;
  98. }
  99. // handle root package aliases
  100. $name = $package->getName();
  101. if (isset($rootAliases[$name][$package->getVersion()])) {
  102. $alias = $rootAliases[$name][$package->getVersion()];
  103. if ($package instanceof AliasPackage) {
  104. $package = $package->getAliasOf();
  105. }
  106. $aliasPackage = new AliasPackage($package, $alias['alias_normalized'], $alias['alias']);
  107. $aliasPackage->setRootPackageAlias(true);
  108. $aliasPackage->setId($this->id++);
  109. $package->getRepository()->addPackage($aliasPackage);
  110. $this->packages[] = $aliasPackage;
  111. $this->packageByExactName[$aliasPackage->getName()][$aliasPackage->id] = $aliasPackage;
  112. foreach ($aliasPackage->getNames() as $name) {
  113. $this->packageByName[$name][] = $aliasPackage;
  114. }
  115. }
  116. }
  117. }
  118. }
  119. }
  120. }
  121. public function getPriority(RepositoryInterface $repo)
  122. {
  123. $priority = array_search($repo, $this->repositories, true);
  124. if (false === $priority) {
  125. throw new \RuntimeException("Could not determine repository priority. The repository was not registered in the pool.");
  126. }
  127. return -$priority;
  128. }
  129. /**
  130. * Retrieves the package object for a given package id.
  131. *
  132. * @param int $id
  133. * @return PackageInterface
  134. */
  135. public function packageById($id)
  136. {
  137. return $this->packages[$id - 1];
  138. }
  139. /**
  140. * Searches all packages providing the given package name and match the constraint
  141. *
  142. * @param string $name The package name to be searched for
  143. * @param LinkConstraintInterface $constraint A constraint that all returned
  144. * packages must match or null to return all
  145. * @param bool $mustMatchName Whether the name of returned packages
  146. * must match the given name
  147. * @return PackageInterface[] A set of packages
  148. */
  149. public function whatProvides($name, LinkConstraintInterface $constraint = null, $mustMatchName = false)
  150. {
  151. $key = ((int) $mustMatchName).$constraint;
  152. if (isset($this->providerCache[$name][$key])) {
  153. return $this->providerCache[$name][$key];
  154. }
  155. return $this->providerCache[$name][$key] = $this->computeWhatProvides($name, $constraint, $mustMatchName);
  156. }
  157. /**
  158. * @see whatProvides
  159. */
  160. private function computeWhatProvides($name, $constraint, $mustMatchName = false)
  161. {
  162. $candidates = array();
  163. foreach ($this->providerRepos as $repo) {
  164. foreach ($repo->whatProvides($this, $name) as $candidate) {
  165. $candidates[] = $candidate;
  166. if ($candidate->id < 1) {
  167. $candidate->setId($this->id++);
  168. $this->packages[$this->id - 2] = $candidate;
  169. }
  170. }
  171. }
  172. if ($mustMatchName) {
  173. $candidates = array_filter($candidates, function ($candidate) use ($name) {
  174. return $candidate->getName() == $name;
  175. });
  176. if (isset($this->packageByExactName[$name])) {
  177. $candidates = array_merge($candidates, $this->packageByExactName[$name]);
  178. }
  179. } elseif (isset($this->packageByName[$name])) {
  180. $candidates = array_merge($candidates, $this->packageByName[$name]);
  181. }
  182. $matches = $provideMatches = array();
  183. $nameMatch = false;
  184. foreach ($candidates as $candidate) {
  185. $aliasOfCandidate = null;
  186. // alias packages are not white listed, make sure that the package
  187. // being aliased is white listed
  188. if ($candidate instanceof AliasPackage) {
  189. $aliasOfCandidate = $candidate->getAliasOf();
  190. }
  191. if ($this->whitelist !== null && (
  192. (!($candidate instanceof AliasPackage) && !isset($this->whitelist[$candidate->id])) ||
  193. ($candidate instanceof AliasPackage && !isset($this->whitelist[$aliasOfCandidate->id]))
  194. )) {
  195. continue;
  196. }
  197. switch ($this->match($candidate, $name, $constraint)) {
  198. case self::MATCH_NONE:
  199. break;
  200. case self::MATCH_NAME:
  201. $nameMatch = true;
  202. break;
  203. case self::MATCH:
  204. $nameMatch = true;
  205. $matches[] = $candidate;
  206. break;
  207. case self::MATCH_PROVIDE:
  208. $provideMatches[] = $candidate;
  209. break;
  210. case self::MATCH_REPLACE:
  211. $matches[] = $candidate;
  212. break;
  213. case self::MATCH_FILTERED:
  214. break;
  215. default:
  216. throw new \UnexpectedValueException('Unexpected match type');
  217. }
  218. }
  219. // if a package with the required name exists, we ignore providers
  220. if ($nameMatch) {
  221. return $matches;
  222. }
  223. return array_merge($matches, $provideMatches);
  224. }
  225. public function literalToPackage($literal)
  226. {
  227. $packageId = abs($literal);
  228. return $this->packageById($packageId);
  229. }
  230. public function literalToString($literal)
  231. {
  232. return ($literal > 0 ? '+' : '-') . $this->literalToPackage($literal);
  233. }
  234. public function literalToPrettyString($literal, $installedMap)
  235. {
  236. $package = $this->literalToPackage($literal);
  237. if (isset($installedMap[$package->id])) {
  238. $prefix = ($literal > 0 ? 'keep' : 'remove');
  239. } else {
  240. $prefix = ($literal > 0 ? 'install' : 'don\'t install');
  241. }
  242. return $prefix.' '.$package->getPrettyString();
  243. }
  244. public function isPackageAcceptable($name, $stability)
  245. {
  246. foreach ((array) $name as $n) {
  247. // allow if package matches the global stability requirement and has no exception
  248. if (!isset($this->stabilityFlags[$n]) && isset($this->acceptableStabilities[$stability])) {
  249. return true;
  250. }
  251. // allow if package matches the package-specific stability flag
  252. if (isset($this->stabilityFlags[$n]) && BasePackage::$stabilities[$stability] <= $this->stabilityFlags[$n]) {
  253. return true;
  254. }
  255. }
  256. return false;
  257. }
  258. /**
  259. * Checks if the package matches the given constraint directly or through
  260. * provided or replaced packages
  261. *
  262. * @param array|PackageInterface $candidate
  263. * @param string $name Name of the package to be matched
  264. * @param LinkConstraintInterface $constraint The constraint to verify
  265. * @return int One of the MATCH* constants of this class or 0 if there is no match
  266. */
  267. private function match($candidate, $name, LinkConstraintInterface $constraint = null)
  268. {
  269. $candidateName = $candidate->getName();
  270. $candidateVersion = $candidate->getVersion();
  271. $isDev = $candidate->getStability() === 'dev';
  272. $isAlias = $candidate instanceof AliasPackage;
  273. if (!$isDev && !$isAlias && isset($this->filterRequires[$name])) {
  274. $requireFilter = $this->filterRequires[$name];
  275. } else {
  276. $requireFilter = new EmptyConstraint;
  277. }
  278. if ($candidateName === $name) {
  279. $pkgConstraint = new VersionConstraint('==', $candidateVersion);
  280. if ($constraint === null || $constraint->matches($pkgConstraint)) {
  281. return $requireFilter->matches($pkgConstraint) ? self::MATCH : self::MATCH_FILTERED;
  282. }
  283. return self::MATCH_NAME;
  284. }
  285. $provides = $candidate->getProvides();
  286. $replaces = $candidate->getReplaces();
  287. // aliases create multiple replaces/provides for one target so they can not use the shortcut below
  288. if (isset($replaces[0]) || isset($provides[0])) {
  289. foreach ($provides as $link) {
  290. if ($link->getTarget() === $name && ($constraint === null || $constraint->matches($link->getConstraint()))) {
  291. return $requireFilter->matches($link->getConstraint()) ? self::MATCH_PROVIDE : self::MATCH_FILTERED;
  292. }
  293. }
  294. foreach ($replaces as $link) {
  295. if ($link->getTarget() === $name && ($constraint === null || $constraint->matches($link->getConstraint()))) {
  296. return $requireFilter->matches($link->getConstraint()) ? self::MATCH_REPLACE : self::MATCH_FILTERED;
  297. }
  298. }
  299. return self::MATCH_NONE;
  300. }
  301. if (isset($provides[$name]) && ($constraint === null || $constraint->matches($provides[$name]->getConstraint()))) {
  302. return $requireFilter->matches($provides[$name]->getConstraint()) ? self::MATCH_PROVIDE : self::MATCH_FILTERED;
  303. }
  304. if (isset($replaces[$name]) && ($constraint === null || $constraint->matches($replaces[$name]->getConstraint()))) {
  305. return $requireFilter->matches($replaces[$name]->getConstraint()) ? self::MATCH_REPLACE : self::MATCH_FILTERED;
  306. }
  307. return self::MATCH_NONE;
  308. }
  309. }