Pool.php 15 KB

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