PageRenderTime 70ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/session25/LabExam7/vendor/composer/composer/src/Composer/DependencyResolver/Pool.php

https://gitlab.com/imamul68e/137619_PHP31
PHP | 378 lines | 260 code | 58 blank | 60 comment | 55 complexity | b2c5c28ec332b08253419c212d0408a2 MD5 | raw file
  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\Semver\Constraint\ConstraintInterface;
  16. use Composer\Semver\Constraint\Constraint;
  17. use Composer\Semver\Constraint\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\PlatformRepository;
  23. use Composer\Package\PackageInterface;
  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 implements \Countable
  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. const MATCH_FILTERED = 4;
  38. protected $repositories = array();
  39. protected $providerRepos = array();
  40. protected $packages = array();
  41. protected $packageByName = array();
  42. protected $packageByExactName = 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. $this->versionParser = new VersionParser;
  53. $this->acceptableStabilities = array();
  54. foreach (BasePackage::$stabilities as $stability => $value) {
  55. if ($value <= BasePackage::$stabilities[$minimumStability]) {
  56. $this->acceptableStabilities[$stability] = $value;
  57. }
  58. }
  59. $this->stabilityFlags = $stabilityFlags;
  60. $this->filterRequires = $filterRequires;
  61. foreach ($filterRequires as $name => $constraint) {
  62. if (preg_match(PlatformRepository::PLATFORM_PACKAGE_REGEX, $name)) {
  63. unset($this->filterRequires[$name]);
  64. }
  65. }
  66. }
  67. public function setWhitelist($whitelist)
  68. {
  69. $this->whitelist = $whitelist;
  70. $this->providerCache = array();
  71. }
  72. /**
  73. * Adds a repository and its packages to this package pool
  74. *
  75. * @param RepositoryInterface $repo A package repository
  76. * @param array $rootAliases
  77. */
  78. public function addRepository(RepositoryInterface $repo, $rootAliases = array())
  79. {
  80. if ($repo instanceof CompositeRepository) {
  81. $repos = $repo->getRepositories();
  82. } else {
  83. $repos = array($repo);
  84. }
  85. foreach ($repos as $repo) {
  86. $this->repositories[] = $repo;
  87. $exempt = $repo instanceof PlatformRepository || $repo instanceof InstalledRepositoryInterface;
  88. if ($repo instanceof ComposerRepository && $repo->hasProviders()) {
  89. $this->providerRepos[] = $repo;
  90. $repo->setRootAliases($rootAliases);
  91. $repo->resetPackageIds();
  92. } else {
  93. foreach ($repo->getPackages() as $package) {
  94. $names = $package->getNames();
  95. $stability = $package->getStability();
  96. if ($exempt || $this->isPackageAcceptable($names, $stability)) {
  97. $package->setId($this->id++);
  98. $this->packages[] = $package;
  99. $this->packageByExactName[$package->getName()][$package->id] = $package;
  100. foreach ($names as $provided) {
  101. $this->packageByName[$provided][] = $package;
  102. }
  103. // handle root package aliases
  104. $name = $package->getName();
  105. if (isset($rootAliases[$name][$package->getVersion()])) {
  106. $alias = $rootAliases[$name][$package->getVersion()];
  107. if ($package instanceof AliasPackage) {
  108. $package = $package->getAliasOf();
  109. }
  110. $aliasPackage = new AliasPackage($package, $alias['alias_normalized'], $alias['alias']);
  111. $aliasPackage->setRootPackageAlias(true);
  112. $aliasPackage->setId($this->id++);
  113. $package->getRepository()->addPackage($aliasPackage);
  114. $this->packages[] = $aliasPackage;
  115. $this->packageByExactName[$aliasPackage->getName()][$aliasPackage->id] = $aliasPackage;
  116. foreach ($aliasPackage->getNames() as $name) {
  117. $this->packageByName[$name][] = $aliasPackage;
  118. }
  119. }
  120. }
  121. }
  122. }
  123. }
  124. }
  125. public function getPriority(RepositoryInterface $repo)
  126. {
  127. $priority = array_search($repo, $this->repositories, true);
  128. if (false === $priority) {
  129. throw new \RuntimeException("Could not determine repository priority. The repository was not registered in the pool.");
  130. }
  131. return -$priority;
  132. }
  133. /**
  134. * Retrieves the package object for a given package id.
  135. *
  136. * @param int $id
  137. * @return PackageInterface
  138. */
  139. public function packageById($id)
  140. {
  141. return $this->packages[$id - 1];
  142. }
  143. /**
  144. * Returns how many packages have been loaded into the pool
  145. */
  146. public function count()
  147. {
  148. return count($this->packages);
  149. }
  150. /**
  151. * Searches all packages providing the given package name and match the constraint
  152. *
  153. * @param string $name The package name to be searched for
  154. * @param ConstraintInterface $constraint A constraint that all returned
  155. * packages must match or null to return all
  156. * @param bool $mustMatchName Whether the name of returned packages
  157. * must match the given name
  158. * @param bool $bypassFilters If enabled, filterRequires and stability matching is ignored
  159. * @return PackageInterface[] A set of packages
  160. */
  161. public function whatProvides($name, ConstraintInterface $constraint = null, $mustMatchName = false, $bypassFilters = false)
  162. {
  163. if ($bypassFilters) {
  164. return $this->computeWhatProvides($name, $constraint, $mustMatchName, true);
  165. }
  166. $key = ((int) $mustMatchName).$constraint;
  167. if (isset($this->providerCache[$name][$key])) {
  168. return $this->providerCache[$name][$key];
  169. }
  170. return $this->providerCache[$name][$key] = $this->computeWhatProvides($name, $constraint, $mustMatchName, $bypassFilters);
  171. }
  172. /**
  173. * @see whatProvides
  174. */
  175. private function computeWhatProvides($name, $constraint, $mustMatchName = false, $bypassFilters = false)
  176. {
  177. $candidates = array();
  178. foreach ($this->providerRepos as $repo) {
  179. foreach ($repo->whatProvides($this, $name, $bypassFilters) as $candidate) {
  180. $candidates[] = $candidate;
  181. if ($candidate->id < 1) {
  182. $candidate->setId($this->id++);
  183. $this->packages[$this->id - 2] = $candidate;
  184. }
  185. }
  186. }
  187. if ($mustMatchName) {
  188. $candidates = array_filter($candidates, function ($candidate) use ($name) {
  189. return $candidate->getName() == $name;
  190. });
  191. if (isset($this->packageByExactName[$name])) {
  192. $candidates = array_merge($candidates, $this->packageByExactName[$name]);
  193. }
  194. } elseif (isset($this->packageByName[$name])) {
  195. $candidates = array_merge($candidates, $this->packageByName[$name]);
  196. }
  197. $matches = $provideMatches = array();
  198. $nameMatch = false;
  199. foreach ($candidates as $candidate) {
  200. $aliasOfCandidate = null;
  201. // alias packages are not white listed, make sure that the package
  202. // being aliased is white listed
  203. if ($candidate instanceof AliasPackage) {
  204. $aliasOfCandidate = $candidate->getAliasOf();
  205. }
  206. if ($this->whitelist !== null && !$bypassFilters && (
  207. (!($candidate instanceof AliasPackage) && !isset($this->whitelist[$candidate->id])) ||
  208. ($candidate instanceof AliasPackage && !isset($this->whitelist[$aliasOfCandidate->id]))
  209. )) {
  210. continue;
  211. }
  212. switch ($this->match($candidate, $name, $constraint, $bypassFilters)) {
  213. case self::MATCH_NONE:
  214. break;
  215. case self::MATCH_NAME:
  216. $nameMatch = true;
  217. break;
  218. case self::MATCH:
  219. $nameMatch = true;
  220. $matches[] = $candidate;
  221. break;
  222. case self::MATCH_PROVIDE:
  223. $provideMatches[] = $candidate;
  224. break;
  225. case self::MATCH_REPLACE:
  226. $matches[] = $candidate;
  227. break;
  228. case self::MATCH_FILTERED:
  229. break;
  230. default:
  231. throw new \UnexpectedValueException('Unexpected match type');
  232. }
  233. }
  234. // if a package with the required name exists, we ignore providers
  235. if ($nameMatch) {
  236. return $matches;
  237. }
  238. return array_merge($matches, $provideMatches);
  239. }
  240. public function literalToPackage($literal)
  241. {
  242. $packageId = abs($literal);
  243. return $this->packageById($packageId);
  244. }
  245. public function literalToPrettyString($literal, $installedMap)
  246. {
  247. $package = $this->literalToPackage($literal);
  248. if (isset($installedMap[$package->id])) {
  249. $prefix = ($literal > 0 ? 'keep' : 'remove');
  250. } else {
  251. $prefix = ($literal > 0 ? 'install' : 'don\'t install');
  252. }
  253. return $prefix.' '.$package->getPrettyString();
  254. }
  255. public function isPackageAcceptable($name, $stability)
  256. {
  257. foreach ((array) $name as $n) {
  258. // allow if package matches the global stability requirement and has no exception
  259. if (!isset($this->stabilityFlags[$n]) && isset($this->acceptableStabilities[$stability])) {
  260. return true;
  261. }
  262. // allow if package matches the package-specific stability flag
  263. if (isset($this->stabilityFlags[$n]) && BasePackage::$stabilities[$stability] <= $this->stabilityFlags[$n]) {
  264. return true;
  265. }
  266. }
  267. return false;
  268. }
  269. /**
  270. * Checks if the package matches the given constraint directly or through
  271. * provided or replaced packages
  272. *
  273. * @param array|PackageInterface $candidate
  274. * @param string $name Name of the package to be matched
  275. * @param ConstraintInterface $constraint The constraint to verify
  276. * @return int One of the MATCH* constants of this class or 0 if there is no match
  277. */
  278. private function match($candidate, $name, ConstraintInterface $constraint = null, $bypassFilters)
  279. {
  280. $candidateName = $candidate->getName();
  281. $candidateVersion = $candidate->getVersion();
  282. $isDev = $candidate->getStability() === 'dev';
  283. $isAlias = $candidate instanceof AliasPackage;
  284. if (!$bypassFilters && !$isDev && !$isAlias && isset($this->filterRequires[$name])) {
  285. $requireFilter = $this->filterRequires[$name];
  286. } else {
  287. $requireFilter = new EmptyConstraint;
  288. }
  289. if ($candidateName === $name) {
  290. $pkgConstraint = new Constraint('==', $candidateVersion);
  291. if ($constraint === null || $constraint->matches($pkgConstraint)) {
  292. return $requireFilter->matches($pkgConstraint) ? self::MATCH : self::MATCH_FILTERED;
  293. }
  294. return self::MATCH_NAME;
  295. }
  296. $provides = $candidate->getProvides();
  297. $replaces = $candidate->getReplaces();
  298. // aliases create multiple replaces/provides for one target so they can not use the shortcut below
  299. if (isset($replaces[0]) || isset($provides[0])) {
  300. foreach ($provides as $link) {
  301. if ($link->getTarget() === $name && ($constraint === null || $constraint->matches($link->getConstraint()))) {
  302. return $requireFilter->matches($link->getConstraint()) ? self::MATCH_PROVIDE : self::MATCH_FILTERED;
  303. }
  304. }
  305. foreach ($replaces as $link) {
  306. if ($link->getTarget() === $name && ($constraint === null || $constraint->matches($link->getConstraint()))) {
  307. return $requireFilter->matches($link->getConstraint()) ? self::MATCH_REPLACE : self::MATCH_FILTERED;
  308. }
  309. }
  310. return self::MATCH_NONE;
  311. }
  312. if (isset($provides[$name]) && ($constraint === null || $constraint->matches($provides[$name]->getConstraint()))) {
  313. return $requireFilter->matches($provides[$name]->getConstraint()) ? self::MATCH_PROVIDE : self::MATCH_FILTERED;
  314. }
  315. if (isset($replaces[$name]) && ($constraint === null || $constraint->matches($replaces[$name]->getConstraint()))) {
  316. return $requireFilter->matches($replaces[$name]->getConstraint()) ? self::MATCH_REPLACE : self::MATCH_FILTERED;
  317. }
  318. return self::MATCH_NONE;
  319. }
  320. }