Pool.php 15 KB

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