LocalRepoTransaction.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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\Repository\PlatformRepository;
  14. use Composer\Repository\RepositoryInterface;
  15. /**
  16. * @author Nils Adermann <naderman@naderman.de>
  17. */
  18. class LocalRepoTransaction
  19. {
  20. /** @var RepositoryInterface */
  21. protected $lockedRepository;
  22. /** @var RepositoryInterface */
  23. protected $localRepository;
  24. public function __construct($lockedRepository, $localRepository)
  25. {
  26. $this->lockedRepository = $lockedRepository;
  27. $this->localRepository = $localRepository;
  28. $this->operations = $this->calculateOperations();
  29. }
  30. public function getOperations()
  31. {
  32. return $this->operations;
  33. }
  34. protected function calculateOperations()
  35. {
  36. $operations = array();
  37. $localPackageMap = array();
  38. $removeMap = array();
  39. foreach ($this->localRepository->getPackages() as $package) {
  40. if (isset($localPackageMap[$package->getName()])) {
  41. die("Alias?");
  42. }
  43. $localPackageMap[$package->getName()] = $package;
  44. $removeMap[$package->getName()] = $package;
  45. }
  46. $lockedPackages = array();
  47. foreach ($this->lockedRepository->getPackages() as $package) {
  48. if (isset($localPackageMap[$package->getName()])) {
  49. $source = $localPackageMap[$package->getName()];
  50. // do we need to update?
  51. if ($package->getVersion() != $localPackageMap[$package->getName()]->getVersion()) {
  52. $operations[] = new Operation\UpdateOperation($source, $package);
  53. } else {
  54. // TODO do we need to update metadata? force update based on reference?
  55. }
  56. } else {
  57. $operations[] = new Operation\InstallOperation($package);
  58. unset($removeMap[$package->getName()]);
  59. }
  60. /*
  61. if (isset($lockedPackages[$package->getName()])) {
  62. die("Alias?");
  63. }
  64. $lockedPackages[$package->getName()] = $package;*/
  65. }
  66. foreach ($removeMap as $name => $package) {
  67. $operations[] = new Operation\UninstallOperation($package, null);
  68. }
  69. $operations = $this->movePluginsToFront($operations);
  70. $operations = $this->moveUninstallsToFront($operations);
  71. // TODO skip updates which don't update? is this needed? we shouldn't schedule this update in the first place?
  72. /*
  73. if ('update' === $jobType) {
  74. $targetPackage = $operation->getTargetPackage();
  75. if ($targetPackage->isDev()) {
  76. $initialPackage = $operation->getInitialPackage();
  77. if ($targetPackage->getVersion() === $initialPackage->getVersion()
  78. && (!$targetPackage->getSourceReference() || $targetPackage->getSourceReference() === $initialPackage->getSourceReference())
  79. && (!$targetPackage->getDistReference() || $targetPackage->getDistReference() === $initialPackage->getDistReference())
  80. ) {
  81. $this->io->writeError(' - Skipping update of ' . $targetPackage->getPrettyName() . ' to the same reference-locked version', true, IOInterface::DEBUG);
  82. $this->io->writeError('', true, IOInterface::DEBUG);
  83. continue;
  84. }
  85. }
  86. }*/
  87. return $operations;
  88. }
  89. /**
  90. * Workaround: if your packages depend on plugins, we must be sure
  91. * that those are installed / updated first; else it would lead to packages
  92. * being installed multiple times in different folders, when running Composer
  93. * twice.
  94. *
  95. * While this does not fix the root-causes of https://github.com/composer/composer/issues/1147,
  96. * it at least fixes the symptoms and makes usage of composer possible (again)
  97. * in such scenarios.
  98. *
  99. * @param Operation\OperationInterface[] $operations
  100. * @return Operation\OperationInterface[] reordered operation list
  101. */
  102. private function movePluginsToFront(array $operations)
  103. {
  104. $pluginsNoDeps = array();
  105. $pluginsWithDeps = array();
  106. $pluginRequires = array();
  107. foreach (array_reverse($operations, true) as $idx => $op) {
  108. if ($op instanceof Operation\InstallOperation) {
  109. $package = $op->getPackage();
  110. } elseif ($op instanceof Operation\UpdateOperation) {
  111. $package = $op->getTargetPackage();
  112. } else {
  113. continue;
  114. }
  115. // is this package a plugin?
  116. $isPlugin = $package->getType() === 'composer-plugin' || $package->getType() === 'composer-installer';
  117. // is this a plugin or a dependency of a plugin?
  118. if ($isPlugin || count(array_intersect($package->getNames(), $pluginRequires))) {
  119. // get the package's requires, but filter out any platform requirements or 'composer-plugin-api'
  120. $requires = array_filter(array_keys($package->getRequires()), function ($req) {
  121. return $req !== 'composer-plugin-api' && !preg_match(PlatformRepository::PLATFORM_PACKAGE_REGEX, $req);
  122. });
  123. // is this a plugin with no meaningful dependencies?
  124. if ($isPlugin && !count($requires)) {
  125. // plugins with no dependencies go to the very front
  126. array_unshift($pluginsNoDeps, $op);
  127. } else {
  128. // capture the requirements for this package so those packages will be moved up as well
  129. $pluginRequires = array_merge($pluginRequires, $requires);
  130. // move the operation to the front
  131. array_unshift($pluginsWithDeps, $op);
  132. }
  133. unset($operations[$idx]);
  134. }
  135. }
  136. return array_merge($pluginsNoDeps, $pluginsWithDeps, $operations);
  137. }
  138. /**
  139. * Removals of packages should be executed before installations in
  140. * case two packages resolve to the same path (due to custom installers)
  141. *
  142. * @param Operation\OperationInterface[] $operations
  143. * @return Operation\OperationInterface[] reordered operation list
  144. */
  145. private function moveUninstallsToFront(array $operations)
  146. {
  147. $uninstOps = array();
  148. foreach ($operations as $idx => $op) {
  149. if ($op instanceof UninstallOperation) {
  150. $uninstOps[] = $op;
  151. unset($operations[$idx]);
  152. }
  153. }
  154. return array_merge($uninstOps, $operations);
  155. }
  156. }