Pool.php 18 KB

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