PoolBuilder.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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\IO\IOInterface;
  13. use Composer\Package\AliasPackage;
  14. use Composer\Package\BasePackage;
  15. use Composer\Package\Package;
  16. use Composer\Package\PackageInterface;
  17. use Composer\Package\Version\StabilityFilter;
  18. use Composer\Repository\PlatformRepository;
  19. use Composer\Repository\RootPackageRepository;
  20. use Composer\Semver\Constraint\Constraint;
  21. use Composer\Semver\Constraint\EmptyConstraint;
  22. use Composer\Semver\Constraint\MultiConstraint;
  23. use Composer\EventDispatcher\EventDispatcher;
  24. use Composer\Plugin\PrePoolCreateEvent;
  25. use Composer\Plugin\PluginEvents;
  26. /**
  27. * @author Nils Adermann <naderman@naderman.de>
  28. */
  29. class PoolBuilder
  30. {
  31. private $acceptableStabilities;
  32. private $stabilityFlags;
  33. private $rootAliases;
  34. private $rootReferences;
  35. private $eventDispatcher;
  36. private $io;
  37. private $aliasMap = array();
  38. private $nameConstraints = array();
  39. private $loadedNames = array();
  40. private $packages = array();
  41. private $unacceptableFixedPackages = array();
  42. private $updateAllowList = array();
  43. private $skippedLoad = array();
  44. private $updateAllowWarned = array();
  45. public function __construct(array $acceptableStabilities, array $stabilityFlags, array $rootAliases, array $rootReferences, IOInterface $io, EventDispatcher $eventDispatcher = null)
  46. {
  47. $this->acceptableStabilities = $acceptableStabilities;
  48. $this->stabilityFlags = $stabilityFlags;
  49. $this->rootAliases = $rootAliases;
  50. $this->rootReferences = $rootReferences;
  51. $this->eventDispatcher = $eventDispatcher;
  52. $this->io = $io;
  53. }
  54. public function buildPool(array $repositories, Request $request)
  55. {
  56. if ($request->getUpdateAllowList()) {
  57. $this->updateAllowList = $request->getUpdateAllowList();
  58. $this->warnAboutNonMatchingUpdateAllowList($request);
  59. foreach ($request->getLockedRepository()->getPackages() as $lockedPackage) {
  60. if (!$this->isUpdateAllowed($lockedPackage)) {
  61. $request->fixPackage($lockedPackage);
  62. // remember which packages we skipped loading remote content for in this partial update
  63. $this->skippedLoad[$lockedPackage->getName()] = true;
  64. foreach ($lockedPackage->getReplaces() as $link) {
  65. $this->skippedLoad[$link->getTarget()] = true;
  66. }
  67. }
  68. }
  69. }
  70. $loadNames = array();
  71. foreach ($request->getFixedPackages() as $package) {
  72. $this->nameConstraints[$package->getName()] = null;
  73. $this->loadedNames[$package->getName()] = true;
  74. // replace means conflict, so if a fixed package replaces a name, no need to load that one, packages would conflict anyways
  75. foreach ($package->getReplaces() as $link) {
  76. $this->nameConstraints[$package->getName()] = null;
  77. $this->loadedNames[$link->getTarget()] = true;
  78. }
  79. // TODO in how far can we do the above for conflicts? It's more tricky cause conflicts can be limited to
  80. // specific versions while replace is a conflict with all versions of the name
  81. if (
  82. $package->getRepository() instanceof RootPackageRepository
  83. || $package->getRepository() instanceof PlatformRepository
  84. || StabilityFilter::isPackageAcceptable($this->acceptableStabilities, $this->stabilityFlags, $package->getNames(), $package->getStability())
  85. ) {
  86. $loadNames += $this->loadPackage($request, $package, false);
  87. } else {
  88. $this->unacceptableFixedPackages[] = $package;
  89. }
  90. }
  91. foreach ($request->getRequires() as $packageName => $constraint) {
  92. // fixed packages have already been added, so if a root require needs one of them, no need to do anything
  93. if (isset($this->loadedNames[$packageName])) {
  94. continue;
  95. }
  96. $loadNames[$packageName] = $constraint;
  97. $this->nameConstraints[$packageName] = $constraint ? new MultiConstraint(array($constraint), false) : null;
  98. }
  99. // clean up loadNames for anything we manually marked loaded above
  100. foreach ($loadNames as $name => $void) {
  101. if (isset($this->loadedNames[$name])) {
  102. unset($loadNames[$name]);
  103. }
  104. }
  105. while (!empty($loadNames)) {
  106. foreach ($loadNames as $name => $void) {
  107. $this->loadedNames[$name] = true;
  108. }
  109. $newLoadNames = array();
  110. foreach ($repositories as $repository) {
  111. // these repos have their packages fixed if they need to be loaded so we
  112. // never need to load anything else from them
  113. if ($repository instanceof PlatformRepository || $repository === $request->getLockedRepository()) {
  114. continue;
  115. }
  116. $result = $repository->loadPackages($loadNames, $this->acceptableStabilities, $this->stabilityFlags);
  117. foreach ($result['namesFound'] as $name) {
  118. // avoid loading the same package again from other repositories once it has been found
  119. unset($loadNames[$name]);
  120. }
  121. foreach ($result['packages'] as $package) {
  122. $newLoadNames += $this->loadPackage($request, $package);
  123. }
  124. }
  125. $loadNames = $newLoadNames;
  126. }
  127. // filter packages according to all the require statements collected for each package
  128. foreach ($this->packages as $i => $package) {
  129. // we check all alias related packages at once, so no need to check individual aliases
  130. // isset also checks non-null value
  131. if (!$package instanceof AliasPackage && isset($this->nameConstraints[$package->getName()])) {
  132. $constraint = $this->nameConstraints[$package->getName()];
  133. $aliasedPackages = array($i => $package);
  134. if (isset($this->aliasMap[spl_object_hash($package)])) {
  135. $aliasedPackages += $this->aliasMap[spl_object_hash($package)];
  136. }
  137. $found = false;
  138. foreach ($aliasedPackages as $packageOrAlias) {
  139. if ($constraint->matches(new Constraint('==', $packageOrAlias->getVersion()))) {
  140. $found = true;
  141. }
  142. }
  143. if (!$found) {
  144. foreach ($aliasedPackages as $index => $packageOrAlias) {
  145. unset($this->packages[$index]);
  146. }
  147. }
  148. }
  149. }
  150. if ($this->eventDispatcher) {
  151. $prePoolCreateEvent = new PrePoolCreateEvent(
  152. PluginEvents::PRE_POOL_CREATE,
  153. $repositories,
  154. $request,
  155. $this->acceptableStabilities,
  156. $this->stabilityFlags,
  157. $this->rootAliases,
  158. $this->rootReferences,
  159. $this->packages,
  160. $this->unacceptableFixedPackages
  161. );
  162. $this->eventDispatcher->dispatch($prePoolCreateEvent->getName(), $prePoolCreateEvent);
  163. $this->packages = $prePoolCreateEvent->getPackages();
  164. $this->unacceptableFixedPackages = $prePoolCreateEvent->getUnacceptableFixedPackages();
  165. }
  166. $pool = new Pool($this->packages, $this->unacceptableFixedPackages);
  167. $this->aliasMap = array();
  168. $this->nameConstraints = array();
  169. $this->loadedNames = array();
  170. $this->packages = array();
  171. $this->unacceptableFixedPackages = array();
  172. return $pool;
  173. }
  174. private function loadPackage(Request $request, PackageInterface $package, $propagateUpdate = true)
  175. {
  176. end($this->packages);
  177. $index = key($this->packages) + 1;
  178. $this->packages[] = $package;
  179. if ($package instanceof AliasPackage) {
  180. $this->aliasMap[spl_object_hash($package->getAliasOf())][$index] = $package;
  181. }
  182. $name = $package->getName();
  183. // we're simply setting the root references on all versions for a name here and rely on the solver to pick the
  184. // right version. It'd be more work to figure out which versions and which aliases of those versions this may
  185. // apply to
  186. if (isset($this->rootReferences[$name])) {
  187. // do not modify the references on already locked packages
  188. if (!$request->isFixedPackage($package)) {
  189. $package->setSourceDistReferences($this->rootReferences[$name]);
  190. }
  191. }
  192. if (isset($this->rootAliases[$name][$package->getVersion()])) {
  193. $alias = $this->rootAliases[$name][$package->getVersion()];
  194. if ($package instanceof AliasPackage) {
  195. $basePackage = $package->getAliasOf();
  196. } else {
  197. $basePackage = $package;
  198. }
  199. $aliasPackage = new AliasPackage($basePackage, $alias['alias_normalized'], $alias['alias']);
  200. $aliasPackage->setRootPackageAlias(true);
  201. $this->packages[] = $aliasPackage;
  202. $this->aliasMap[spl_object_hash($aliasPackage->getAliasOf())][$index+1] = $aliasPackage;
  203. }
  204. $loadNames = array();
  205. foreach ($package->getRequires() as $link) {
  206. $require = $link->getTarget();
  207. if (!isset($this->loadedNames[$require])) {
  208. $loadNames[$require] = null;
  209. // if this is a partial update with transitive dependencies we need to unfix the package we now know is a
  210. // dependency of another package which we are trying to update, and then attempt to load it again
  211. } elseif ($propagateUpdate && $request->getUpdateAllowTransitiveDependencies() && isset($this->skippedLoad[$require])) {
  212. if ($request->getUpdateAllowTransitiveRootDependencies() || !$this->isRootRequire($request, $require)) {
  213. $this->unfixPackage($request, $require);
  214. $loadNames[$require] = null;
  215. } elseif (!$request->getUpdateAllowTransitiveRootDependencies() && $this->isRootRequire($request, $require) && !isset($this->updateAllowWarned[$require])) {
  216. $this->updateAllowWarned[$require] = true;
  217. $this->io->writeError('<warning>Dependency "'.$require.'" is also a root requirement. Package has not been listed as an update argument, so keeping locked at old version. Use --with-all-dependencies to include root dependencies.</warning>');
  218. }
  219. }
  220. $linkConstraint = $link->getConstraint();
  221. if ($linkConstraint && !($linkConstraint instanceof EmptyConstraint)) {
  222. if (!array_key_exists($require, $this->nameConstraints)) {
  223. $this->nameConstraints[$require] = new MultiConstraint(array($linkConstraint), false);
  224. } elseif ($this->nameConstraints[$require]) {
  225. // TODO addConstraint function?
  226. $this->nameConstraints[$require] = new MultiConstraint(array_merge(array($linkConstraint), $this->nameConstraints[$require]->getConstraints()), false);
  227. }
  228. // else it is null and should stay null
  229. } else {
  230. $this->nameConstraints[$require] = null;
  231. }
  232. }
  233. // if we're doing a partial update with deps and we're not loading an initial fixed package
  234. // we also need to trigger an update for transitive deps which are being replaced
  235. if ($propagateUpdate && $request->getUpdateAllowTransitiveDependencies()) {
  236. foreach ($package->getReplaces() as $link) {
  237. $replace = $link->getTarget();
  238. if (isset($this->loadedNames[$replace]) && isset($this->skippedLoad[$replace])) {
  239. if ($request->getUpdateAllowTransitiveRootDependencies() || !$this->isRootRequire($request, $replace)) {
  240. $this->unfixPackage($request, $replace);
  241. $loadNames[$replace] = null;
  242. // TODO should we try to merge constraints here?
  243. $this->nameConstraints[$replace] = null;
  244. } elseif (!$request->getUpdateAllowTransitiveRootDependencies() && $this->isRootRequire($request, $replace) && !isset($this->updateAllowWarned[$require])) {
  245. $this->updateAllowWarned[$replace] = true;
  246. $this->io->writeError('<warning>Dependency "'.$require.'" is also a root requirement. Package has not been listed as an update argument, so keeping locked at old version. Use --with-all-dependencies to include root dependencies.</warning>');
  247. }
  248. }
  249. }
  250. }
  251. return $loadNames;
  252. }
  253. /**
  254. * Checks if a particular name is required directly in the request
  255. *
  256. * @return bool
  257. */
  258. private function isRootRequire(Request $request, $name)
  259. {
  260. $rootRequires = $request->getRequires();
  261. return isset($rootRequires[$name]);
  262. }
  263. /**
  264. * Checks whether the update allow list allows this package in the lock file to be updated
  265. * @return bool
  266. */
  267. private function isUpdateAllowed(PackageInterface $package)
  268. {
  269. foreach ($this->updateAllowList as $pattern => $void) {
  270. $patternRegexp = BasePackage::packageNameToRegexp($pattern);
  271. if (preg_match($patternRegexp, $package->getName())) {
  272. return true;
  273. }
  274. }
  275. return false;
  276. }
  277. private function warnAboutNonMatchingUpdateAllowList(Request $request)
  278. {
  279. foreach ($this->updateAllowList as $pattern => $void) {
  280. $patternRegexp = BasePackage::packageNameToRegexp($pattern);
  281. // update pattern matches a locked package? => all good
  282. foreach ($request->getLockedRepository()->getPackages() as $package) {
  283. if (preg_match($patternRegexp, $package->getName())) {
  284. continue 2;
  285. }
  286. }
  287. // update pattern matches a root require? => all good, probably a new package
  288. foreach ($request->getRequires() as $packageName => $constraint) {
  289. if (preg_match($patternRegexp, $packageName)) {
  290. continue 2;
  291. }
  292. }
  293. if (strpos($pattern, '*') !== false) {
  294. $this->io->writeError('<warning>Pattern "' . $pattern . '" listed for update does not match any locked packages.</warning>');
  295. } else {
  296. $this->io->writeError('<warning>Package "' . $pattern . '" listed for update is not locked.</warning>');
  297. }
  298. }
  299. }
  300. /**
  301. * Reverts the decision to use a fixed package from lock file if a partial update with transitive dependencies
  302. * found that this package actually needs to be updated
  303. */
  304. private function unfixPackage(Request $request, $name)
  305. {
  306. // remove locked package by this name which was already initialized
  307. foreach ($this->packages as $i => $loadedPackage) {
  308. if ($loadedPackage->getName() === $name && $loadedPackage->getRepository() === $request->getLockedRepository()) {
  309. $request->unfixPackage($loadedPackage);
  310. unset($this->packages[$i]);
  311. unset($this->aliasMap[spl_object_hash($loadedPackage)]);
  312. }
  313. }
  314. unset($this->skippedLoad[$name]);
  315. unset($this->loadedNames[$name]);
  316. }
  317. }