Pool.php 18 KB

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