PoolBuilder.php 17 KB

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