Pool.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  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\StreamableRepositoryInterface;
  23. use Composer\Repository\PlatformRepository;
  24. use Composer\Package\PackageInterface;
  25. /**
  26. * A package pool contains repositories that provide packages.
  27. *
  28. * @author Nils Adermann <naderman@naderman.de>
  29. * @author Jordi Boggiano <j.boggiano@seld.be>
  30. */
  31. class Pool
  32. {
  33. const MATCH_NAME = -1;
  34. const MATCH_NONE = 0;
  35. const MATCH = 1;
  36. const MATCH_PROVIDE = 2;
  37. const MATCH_REPLACE = 3;
  38. const MATCH_FILTERED = 4;
  39. protected $repositories = array();
  40. protected $providerRepos = array();
  41. protected $packages = array();
  42. protected $packageByName = array();
  43. protected $packageByExactName = array();
  44. protected $acceptableStabilities;
  45. protected $stabilityFlags;
  46. protected $versionParser;
  47. protected $providerCache = array();
  48. protected $filterRequires;
  49. protected $whitelist = null;
  50. protected $id = 1;
  51. public function __construct($minimumStability = 'stable', array $stabilityFlags = array(), array $filterRequires = array())
  52. {
  53. $stabilities = BasePackage::$stabilities;
  54. $this->versionParser = new VersionParser;
  55. $this->acceptableStabilities = array();
  56. foreach (BasePackage::$stabilities as $stability => $value) {
  57. if ($value <= BasePackage::$stabilities[$minimumStability]) {
  58. $this->acceptableStabilities[$stability] = $value;
  59. }
  60. }
  61. $this->stabilityFlags = $stabilityFlags;
  62. $this->filterRequires = $filterRequires;
  63. }
  64. public function setWhitelist($whitelist)
  65. {
  66. $this->whitelist = $whitelist;
  67. $this->providerCache = array();
  68. }
  69. /**
  70. * Adds a repository and its packages to this package pool
  71. *
  72. * @param RepositoryInterface $repo A package repository
  73. * @param array $rootAliases
  74. */
  75. public function addRepository(RepositoryInterface $repo, $rootAliases = array())
  76. {
  77. if ($repo instanceof CompositeRepository) {
  78. $repos = $repo->getRepositories();
  79. } else {
  80. $repos = array($repo);
  81. }
  82. foreach ($repos as $repo) {
  83. $this->repositories[] = $repo;
  84. $exempt = $repo instanceof PlatformRepository || $repo instanceof InstalledRepositoryInterface;
  85. if ($repo instanceof ComposerRepository && $repo->hasProviders()) {
  86. $this->providerRepos[] = $repo;
  87. $repo->setRootAliases($rootAliases);
  88. $repo->resetPackageIds();
  89. } elseif ($repo instanceof StreamableRepositoryInterface) {
  90. foreach ($repo->getMinimalPackages() as $package) {
  91. $name = $package['name'];
  92. $version = $package['version'];
  93. $stability = VersionParser::parseStability($version);
  94. // collect names
  95. $names = array(
  96. $name => true,
  97. );
  98. if (isset($package['provide'])) {
  99. foreach ($package['provide'] as $target => $constraint) {
  100. $names[$target] = true;
  101. }
  102. }
  103. if (isset($package['replace'])) {
  104. foreach ($package['replace'] as $target => $constraint) {
  105. $names[$target] = true;
  106. }
  107. }
  108. $names = array_keys($names);
  109. if ($exempt || $this->isPackageAcceptable($names, $stability)) {
  110. $package['id'] = $this->id++;
  111. $package['stability'] = $stability;
  112. $this->packages[] = $package;
  113. $this->packageByExactName[$package->getName()][$package['id']] = $this->packages[$this->id - 2];
  114. foreach ($names as $provided) {
  115. $this->packageByName[$provided][$package['id']] = $this->packages[$this->id - 2];
  116. }
  117. // handle root package aliases
  118. unset($rootAliasData);
  119. if (isset($rootAliases[$name][$version])) {
  120. $rootAliasData = $rootAliases[$name][$version];
  121. } elseif (isset($package['alias_normalized']) && isset($rootAliases[$name][$package['alias_normalized']])) {
  122. $rootAliasData = $rootAliases[$name][$package['alias_normalized']];
  123. }
  124. if (isset($rootAliasData)) {
  125. $alias = $package;
  126. unset($alias['raw']);
  127. $alias['version'] = $rootAliasData['alias_normalized'];
  128. $alias['alias'] = $rootAliasData['alias'];
  129. $alias['alias_of'] = $package['id'];
  130. $alias['id'] = $this->id++;
  131. $alias['root_alias'] = true;
  132. $this->packages[] = $alias;
  133. $this->packageByExactName[$package->getName()][$alias['id']] = $this->packages[$this->id - 2];
  134. foreach ($names as $provided) {
  135. $this->packageByName[$provided][$alias['id']] = $this->packages[$this->id - 2];
  136. }
  137. }
  138. // handle normal package aliases
  139. if (isset($package['alias'])) {
  140. $alias = $package;
  141. unset($alias['raw']);
  142. $alias['version'] = $package['alias_normalized'];
  143. $alias['alias'] = $package['alias'];
  144. $alias['alias_of'] = $package['id'];
  145. $alias['id'] = $this->id++;
  146. $this->packages[] = $alias;
  147. $this->packageByExactName[$package->getName()][$alias['id']] = $this->packages[$this->id - 2];
  148. foreach ($names as $provided) {
  149. $this->packageByName[$provided][$alias['id']] = $this->packages[$this->id - 2];
  150. }
  151. }
  152. }
  153. }
  154. } else {
  155. foreach ($repo->getPackages() as $package) {
  156. $names = $package->getNames();
  157. $stability = $package->getStability();
  158. if ($exempt || $this->isPackageAcceptable($names, $stability)) {
  159. $package->setId($this->id++);
  160. $this->packages[] = $package;
  161. $this->packageByExactName[$package->getName()][$package->getId()] = $package;
  162. foreach ($names as $provided) {
  163. $this->packageByName[$provided][] = $package;
  164. }
  165. // handle root package aliases
  166. $name = $package->getName();
  167. if (isset($rootAliases[$name][$package->getVersion()])) {
  168. $alias = $rootAliases[$name][$package->getVersion()];
  169. if ($package instanceof AliasPackage) {
  170. $package = $package->getAliasOf();
  171. }
  172. $aliasPackage = new AliasPackage($package, $alias['alias_normalized'], $alias['alias']);
  173. $aliasPackage->setRootPackageAlias(true);
  174. $aliasPackage->setId($this->id++);
  175. $package->getRepository()->addPackage($aliasPackage);
  176. $this->packages[] = $aliasPackage;
  177. $this->packageByExactName[$aliasPackage->getName()][$aliasPackage->getId()] = $aliasPackage;
  178. foreach ($aliasPackage->getNames() as $name) {
  179. $this->packageByName[$name][] = $aliasPackage;
  180. }
  181. }
  182. }
  183. }
  184. }
  185. }
  186. }
  187. public function getPriority(RepositoryInterface $repo)
  188. {
  189. $priority = array_search($repo, $this->repositories, true);
  190. if (false === $priority) {
  191. throw new \RuntimeException("Could not determine repository priority. The repository was not registered in the pool.");
  192. }
  193. return -$priority;
  194. }
  195. /**
  196. * Retrieves the package object for a given package id.
  197. *
  198. * @param int $id
  199. * @return PackageInterface
  200. */
  201. public function packageById($id)
  202. {
  203. return $this->ensurePackageIsLoaded($this->packages[$id - 1]);
  204. }
  205. /**
  206. * Searches all packages providing the given package name and match the constraint
  207. *
  208. * @param string $name The package name to be searched for
  209. * @param LinkConstraintInterface $constraint A constraint that all returned
  210. * packages must match or null to return all
  211. * @param bool $mustMatchName Whether the name of returned packages
  212. * must match the given name
  213. * @return PackageInterface[] A set of packages
  214. */
  215. public function whatProvides($name, LinkConstraintInterface $constraint = null, $mustMatchName = false)
  216. {
  217. $key = ((int) $mustMatchName).$constraint;
  218. if (isset($this->providerCache[$name][$key])) {
  219. return $this->providerCache[$name][$key];
  220. }
  221. return $this->providerCache[$name][$key] = $this->computeWhatProvides($name, $constraint, $mustMatchName);
  222. }
  223. /**
  224. * @see whatProvides
  225. */
  226. private function computeWhatProvides($name, $constraint, $mustMatchName = false)
  227. {
  228. $candidates = array();
  229. foreach ($this->providerRepos as $repo) {
  230. foreach ($repo->whatProvides($this, $name) as $candidate) {
  231. $candidates[] = $candidate;
  232. if ($candidate->getId() < 1) {
  233. $candidate->setId($this->id++);
  234. $this->packages[$this->id - 2] = $candidate;
  235. }
  236. }
  237. }
  238. if ($mustMatchName) {
  239. $candidates = array_filter($candidates, function ($candidate) use ($name) {
  240. return $candidate->getName() == $name;
  241. });
  242. if (isset($this->packageByExactName[$name])) {
  243. $candidates = array_merge($candidates, $this->packageByExactName[$name]);
  244. }
  245. } else {
  246. if (isset($this->packageByName[$name])) {
  247. $candidates = array_merge($candidates, $this->packageByName[$name]);
  248. }
  249. }
  250. $matches = $provideMatches = array();
  251. $nameMatch = false;
  252. foreach ($candidates as $candidate) {
  253. $aliasOfCandidate = null;
  254. // alias packages are not white listed, make sure that the package
  255. // being aliased is white listed
  256. if ($candidate instanceof AliasPackage) {
  257. $aliasOfCandidate = $candidate->getAliasOf();
  258. }
  259. if ($this->whitelist !== null && (
  260. (is_array($candidate) && isset($candidate['id']) && !isset($this->whitelist[$candidate['id']])) ||
  261. (is_object($candidate) && !($candidate instanceof AliasPackage) && !isset($this->whitelist[$candidate->getId()])) ||
  262. (is_object($candidate) && $candidate instanceof AliasPackage && !isset($this->whitelist[$aliasOfCandidate->getId()]))
  263. )) {
  264. continue;
  265. }
  266. switch ($this->match($candidate, $name, $constraint)) {
  267. case self::MATCH_NONE:
  268. break;
  269. case self::MATCH_NAME:
  270. $nameMatch = true;
  271. break;
  272. case self::MATCH:
  273. $nameMatch = true;
  274. $matches[] = $this->ensurePackageIsLoaded($candidate);
  275. break;
  276. case self::MATCH_PROVIDE:
  277. $provideMatches[] = $this->ensurePackageIsLoaded($candidate);
  278. break;
  279. case self::MATCH_REPLACE:
  280. $matches[] = $this->ensurePackageIsLoaded($candidate);
  281. break;
  282. case self::MATCH_FILTERED:
  283. break;
  284. default:
  285. throw new \UnexpectedValueException('Unexpected match type');
  286. }
  287. }
  288. // if a package with the required name exists, we ignore providers
  289. if ($nameMatch) {
  290. return $matches;
  291. }
  292. return array_merge($matches, $provideMatches);
  293. }
  294. public function literalToPackage($literal)
  295. {
  296. $packageId = abs($literal);
  297. return $this->packageById($packageId);
  298. }
  299. public function literalToString($literal)
  300. {
  301. return ($literal > 0 ? '+' : '-') . $this->literalToPackage($literal);
  302. }
  303. public function literalToPrettyString($literal, $installedMap)
  304. {
  305. $package = $this->literalToPackage($literal);
  306. if (isset($installedMap[$package->getId()])) {
  307. $prefix = ($literal > 0 ? 'keep' : 'remove');
  308. } else {
  309. $prefix = ($literal > 0 ? 'install' : 'don\'t install');
  310. }
  311. return $prefix.' '.$package->getPrettyString();
  312. }
  313. public function isPackageAcceptable($name, $stability)
  314. {
  315. foreach ((array) $name as $n) {
  316. // allow if package matches the global stability requirement and has no exception
  317. if (!isset($this->stabilityFlags[$n]) && isset($this->acceptableStabilities[$stability])) {
  318. return true;
  319. }
  320. // allow if package matches the package-specific stability flag
  321. if (isset($this->stabilityFlags[$n]) && BasePackage::$stabilities[$stability] <= $this->stabilityFlags[$n]) {
  322. return true;
  323. }
  324. }
  325. return false;
  326. }
  327. private function ensurePackageIsLoaded($data)
  328. {
  329. if (is_array($data)) {
  330. if (isset($data['alias_of'])) {
  331. $aliasOf = $this->packageById($data['alias_of']);
  332. $package = $this->packages[$data['id'] - 1] = $data['repo']->loadAliasPackage($data, $aliasOf);
  333. $package->setRootPackageAlias(!empty($data['root_alias']));
  334. } else {
  335. $package = $this->packages[$data['id'] - 1] = $data['repo']->loadPackage($data);
  336. }
  337. foreach ($package->getNames() as $name) {
  338. $this->packageByName[$name][$data['id']] = $package;
  339. }
  340. $package->setId($data['id']);
  341. return $package;
  342. }
  343. return $data;
  344. }
  345. /**
  346. * Checks if the package matches the given constraint directly or through
  347. * provided or replaced packages
  348. *
  349. * @param array|PackageInterface $candidate
  350. * @param string $name Name of the package to be matched
  351. * @param LinkConstraintInterface $constraint The constraint to verify
  352. * @return int One of the MATCH* constants of this class or 0 if there is no match
  353. */
  354. private function match($candidate, $name, LinkConstraintInterface $constraint = null)
  355. {
  356. // handle array packages
  357. if (is_array($candidate)) {
  358. $candidateName = $candidate['name'];
  359. $candidateVersion = $candidate['version'];
  360. $isDev = $candidate['stability'] === 'dev';
  361. $isAlias = isset($candidate['alias_of']);
  362. } else {
  363. // handle object packages
  364. $candidateName = $candidate->getName();
  365. $candidateVersion = $candidate->getVersion();
  366. $isDev = $candidate->getStability() === 'dev';
  367. $isAlias = $candidate instanceof AliasPackage;
  368. }
  369. if (!$isDev && !$isAlias && isset($this->filterRequires[$name])) {
  370. $requireFilter = $this->filterRequires[$name];
  371. } else {
  372. $requireFilter = new EmptyConstraint;
  373. }
  374. if ($candidateName === $name) {
  375. $pkgConstraint = new VersionConstraint('==', $candidateVersion);
  376. if ($constraint === null || $constraint->matches($pkgConstraint)) {
  377. return $requireFilter->matches($pkgConstraint) ? self::MATCH : self::MATCH_FILTERED;
  378. }
  379. return self::MATCH_NAME;
  380. }
  381. if (is_array($candidate)) {
  382. $provides = isset($candidate['provide'])
  383. ? $this->versionParser->parseLinks($candidateName, $candidateVersion, 'provides', $candidate['provide'])
  384. : array();
  385. $replaces = isset($candidate['replace'])
  386. ? $this->versionParser->parseLinks($candidateName, $candidateVersion, 'replaces', $candidate['replace'])
  387. : array();
  388. } else {
  389. $provides = $candidate->getProvides();
  390. $replaces = $candidate->getReplaces();
  391. }
  392. // aliases create multiple replaces/provides for one target so they can not use the shortcut below
  393. if (isset($replaces[0]) || isset($provides[0])) {
  394. foreach ($provides as $link) {
  395. if ($link->getTarget() === $name && ($constraint === null || $constraint->matches($link->getConstraint()))) {
  396. return $requireFilter->matches($link->getConstraint()) ? self::MATCH_PROVIDE : self::MATCH_FILTERED;
  397. }
  398. }
  399. foreach ($replaces as $link) {
  400. if ($link->getTarget() === $name && ($constraint === null || $constraint->matches($link->getConstraint()))) {
  401. return $requireFilter->matches($link->getConstraint()) ? self::MATCH_REPLACE : self::MATCH_FILTERED;
  402. }
  403. }
  404. return self::MATCH_NONE;
  405. }
  406. if (isset($provides[$name]) && ($constraint === null || $constraint->matches($provides[$name]->getConstraint()))) {
  407. return $requireFilter->matches($provides[$name]->getConstraint()) ? self::MATCH_PROVIDE : self::MATCH_FILTERED;
  408. }
  409. if (isset($replaces[$name]) && ($constraint === null || $constraint->matches($replaces[$name]->getConstraint()))) {
  410. return $requireFilter->matches($replaces[$name]->getConstraint()) ? self::MATCH_REPLACE : self::MATCH_FILTERED;
  411. }
  412. return self::MATCH_NONE;
  413. }
  414. }