Pool.php 14 KB

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