PoolBuilder.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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\AliasPackage;
  13. use Composer\Package\BasePackage;
  14. use Composer\Package\Package;
  15. use Composer\Package\PackageInterface;
  16. use Composer\Package\Version\StabilityFilter;
  17. use Composer\Repository\PlatformRepository;
  18. use Composer\Repository\RootPackageRepository;
  19. use Composer\Semver\Constraint\Constraint;
  20. use Composer\Semver\Constraint\MultiConstraint;
  21. use Composer\EventDispatcher\EventDispatcher;
  22. use Composer\Plugin\PrePoolCreateEvent;
  23. use Composer\Plugin\PluginEvents;
  24. /**
  25. * @author Nils Adermann <naderman@naderman.de>
  26. */
  27. class PoolBuilder
  28. {
  29. private $acceptableStabilities;
  30. private $stabilityFlags;
  31. private $rootAliases;
  32. private $rootReferences;
  33. private $eventDispatcher;
  34. private $aliasMap = array();
  35. private $nameConstraints = array();
  36. private $loadedNames = array();
  37. private $packages = array();
  38. private $unacceptableFixedPackages = array();
  39. public function __construct(array $acceptableStabilities, array $stabilityFlags, array $rootAliases, array $rootReferences, EventDispatcher $eventDispatcher = null)
  40. {
  41. $this->acceptableStabilities = $acceptableStabilities;
  42. $this->stabilityFlags = $stabilityFlags;
  43. $this->rootAliases = $rootAliases;
  44. $this->rootReferences = $rootReferences;
  45. $this->eventDispatcher = $eventDispatcher;
  46. }
  47. public function buildPool(array $repositories, Request $request)
  48. {
  49. $pool = new Pool();
  50. // TODO do we really want the request here? kind of want a root requirements thingy instead
  51. $loadNames = array();
  52. foreach ($request->getFixedPackages() as $package) {
  53. $this->nameConstraints[$package->getName()] = null;
  54. $this->loadedNames[$package->getName()] = true;
  55. unset($loadNames[$package->getName()]);
  56. if (
  57. $package->getRepository() instanceof RootPackageRepository
  58. || $package->getRepository() instanceof PlatformRepository
  59. || StabilityFilter::isPackageAcceptable($this->acceptableStabilities, $this->stabilityFlags, $package->getNames(), $package->getStability())
  60. ) {
  61. $loadNames += $this->loadPackage($request, $package);
  62. } else {
  63. $this->unacceptableFixedPackages[] = $package;
  64. }
  65. }
  66. foreach ($request->getRequires() as $packageName => $constraint) {
  67. if (isset($this->loadedNames[$packageName])) {
  68. continue;
  69. }
  70. // TODO currently lock above is always NULL if we adjust that, this needs to merge constraints
  71. // TODO does it really make sense that we can have install requests for the same package that is actively locked with non-matching constraints?
  72. // also see the solver-problems.test test case
  73. $constraint = array_key_exists($packageName, $loadNames) ? null : $constraint;
  74. $loadNames[$packageName] = $constraint;
  75. $this->nameConstraints[$packageName] = $constraint ? new MultiConstraint(array($constraint), false) : null;
  76. }
  77. while (!empty($loadNames)) {
  78. foreach ($loadNames as $name => $void) {
  79. $this->loadedNames[$name] = true;
  80. }
  81. $newLoadNames = array();
  82. foreach ($repositories as $repository) {
  83. // these repos have their packages fixed if they need to be loaded so we
  84. // never need to load anything else from them
  85. if ($repository instanceof PlatformRepository || $repository === $request->getLockedRepository()) {
  86. continue;
  87. }
  88. $result = $repository->loadPackages($loadNames, $this->acceptableStabilities, $this->stabilityFlags);
  89. foreach ($result['namesFound'] as $name) {
  90. // avoid loading the same package again from other repositories once it has been found
  91. unset($loadNames[$name]);
  92. }
  93. foreach ($result['packages'] as $package) {
  94. $newLoadNames += $this->loadPackage($request, $package);
  95. }
  96. }
  97. $loadNames = $newLoadNames;
  98. }
  99. // filter packages according to all the require statements collected for each package
  100. foreach ($this->packages as $i => $package) {
  101. // we check all alias related packages at once, so no need to check individual aliases
  102. // isset also checks non-null value
  103. if (!$package instanceof AliasPackage && isset($this->nameConstraints[$package->getName()])) {
  104. $constraint = $this->nameConstraints[$package->getName()];
  105. $aliasedPackages = array($i => $package);
  106. if (isset($this->aliasMap[spl_object_hash($package)])) {
  107. $aliasedPackages += $this->aliasMap[spl_object_hash($package)];
  108. }
  109. $found = false;
  110. foreach ($aliasedPackages as $packageOrAlias) {
  111. if ($constraint->matches(new Constraint('==', $packageOrAlias->getVersion()))) {
  112. $found = true;
  113. }
  114. }
  115. if (!$found) {
  116. foreach ($aliasedPackages as $index => $packageOrAlias) {
  117. unset($this->packages[$index]);
  118. }
  119. }
  120. }
  121. }
  122. if ($this->eventDispatcher) {
  123. $prePoolCreateEvent = new PrePoolCreateEvent(
  124. PluginEvents::PRE_POOL_CREATE,
  125. $repositories,
  126. $request,
  127. $this->acceptableStabilities,
  128. $this->stabilityFlags,
  129. $this->rootAliases,
  130. $this->rootReferences,
  131. $this->packages,
  132. $this->unacceptableFixedPackages
  133. );
  134. $this->eventDispatcher->dispatch($prePoolCreateEvent->getName(), $prePoolCreateEvent);
  135. $this->packages = $prePoolCreateEvent->getPackages();
  136. $this->unacceptableFixedPackages = $prePoolCreateEvent->getUnacceptableFixedPackages();
  137. }
  138. $pool = new Pool($this->packages, $this->unacceptableFixedPackages);
  139. $this->aliasMap = array();
  140. $this->nameConstraints = array();
  141. $this->loadedNames = array();
  142. $this->packages = array();
  143. $this->unacceptableFixedPackages = array();
  144. return $pool;
  145. }
  146. private function loadPackage(Request $request, PackageInterface $package)
  147. {
  148. $index = count($this->packages);
  149. $this->packages[] = $package;
  150. if ($package instanceof AliasPackage) {
  151. $this->aliasMap[spl_object_hash($package->getAliasOf())][$index] = $package;
  152. }
  153. $name = $package->getName();
  154. // we're simply setting the root references on all versions for a name here and rely on the solver to pick the
  155. // right version. It'd be more work to figure out which versions and which aliases of those versions this may
  156. // apply to
  157. if (isset($this->rootReferences[$name])) {
  158. // do not modify the references on already locked packages
  159. if (!$request->isFixedPackage($package)) {
  160. $package->setSourceDistReferences($this->rootReferences[$name]);
  161. }
  162. }
  163. if (isset($this->rootAliases[$name][$package->getVersion()])) {
  164. $alias = $this->rootAliases[$name][$package->getVersion()];
  165. if ($package instanceof AliasPackage) {
  166. $basePackage = $package->getAliasOf();
  167. } else {
  168. $basePackage = $package;
  169. }
  170. $aliasPackage = new AliasPackage($basePackage, $alias['alias_normalized'], $alias['alias']);
  171. $aliasPackage->setRootPackageAlias(true);
  172. $this->packages[] = $aliasPackage;
  173. $this->aliasMap[spl_object_hash($aliasPackage->getAliasOf())][$index+1] = $aliasPackage;
  174. }
  175. $loadNames = array();
  176. foreach ($package->getRequires() as $link) {
  177. $require = $link->getTarget();
  178. if (!isset($this->loadedNames[$require])) {
  179. $loadNames[$require] = null;
  180. }
  181. if ($linkConstraint = $link->getConstraint()) {
  182. if (!array_key_exists($require, $this->nameConstraints)) {
  183. $this->nameConstraints[$require] = new MultiConstraint(array($linkConstraint), false);
  184. } elseif ($this->nameConstraints[$require]) {
  185. // TODO addConstraint function?
  186. $this->nameConstraints[$require] = new MultiConstraint(array_merge(array($linkConstraint), $this->nameConstraints[$require]->getConstraints()), false);
  187. }
  188. // else it is null and should stay null
  189. } else {
  190. $this->nameConstraints[$require] = null;
  191. }
  192. }
  193. return $loadNames;
  194. }
  195. }