Transaction.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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\Repository\RepositoryInterface;
  13. use Composer\Package\PackageInterface;
  14. use Composer\Package\AliasPackage;
  15. use Composer\DependencyResolver\Operation;
  16. /**
  17. * @author Nils Adermann <naderman@naderman.de>
  18. */
  19. class Transaction
  20. {
  21. protected $policy;
  22. protected $pool;
  23. protected $installedMap;
  24. protected $decisionMap;
  25. protected $decisionQueue;
  26. protected $decisionQueueWhy;
  27. public function __construct($policy, $pool, $installedMap, $decisionMap, array $decisionQueue, $decisionQueueWhy)
  28. {
  29. $this->policy = $policy;
  30. $this->pool = $pool;
  31. $this->installedMap = $installedMap;
  32. $this->decisionMap = $decisionMap;
  33. $this->decisionQueue = $decisionQueue;
  34. $this->decisionQueueWhy = $decisionQueueWhy;
  35. }
  36. public function getOperations()
  37. {
  38. $transaction = array();
  39. $installMeansUpdateMap = array();
  40. foreach ($this->decisionQueue as $i => $literal) {
  41. $package = $literal->getPackage();
  42. // !wanted & installed
  43. if (!$literal->isWanted() && isset($this->installedMap[$package->getId()])) {
  44. $updates = $this->policy->findUpdatePackages($this->pool, $this->installedMap, $package);
  45. $literals = array(new Literal($package, true));
  46. foreach ($updates as $update) {
  47. $literals[] = new Literal($update, true);
  48. }
  49. foreach ($literals as $updateLiteral) {
  50. if (!$updateLiteral->equals($literal)) {
  51. $installMeansUpdateMap[$updateLiteral->getPackageId()] = $package;
  52. }
  53. }
  54. }
  55. }
  56. foreach ($this->decisionQueue as $i => $literal) {
  57. $package = $literal->getPackage();
  58. // wanted & installed || !wanted & !installed
  59. if ($literal->isWanted() == (isset($this->installedMap[$package->getId()]))) {
  60. continue;
  61. }
  62. if ($literal->isWanted()) {
  63. if ($package instanceof AliasPackage) {
  64. $transaction[] = new Operation\MarkAliasInstalledOperation(
  65. $package, $this->decisionQueueWhy[$i]
  66. );
  67. continue;
  68. }
  69. if (isset($installMeansUpdateMap[$literal->getPackageId()])) {
  70. $source = $installMeansUpdateMap[$literal->getPackageId()];
  71. $transaction[] = new Operation\UpdateOperation(
  72. $source, $package, $this->decisionQueueWhy[$i]
  73. );
  74. // avoid updates to one package from multiple origins
  75. unset($installMeansUpdateMap[$literal->getPackageId()]);
  76. $ignoreRemove[$source->getId()] = true;
  77. } else {
  78. $transaction[] = new Operation\InstallOperation(
  79. $package, $this->decisionQueueWhy[$i]
  80. );
  81. }
  82. } else if (!isset($ignoreRemove[$package->getId()])) {
  83. if ($package instanceof AliasPackage) {
  84. $transaction[] = new Operation\MarkAliasInstalledOperation(
  85. $package, $this->decisionQueueWhy[$i]
  86. );
  87. } else {
  88. $transaction[] = new Operation\UninstallOperation(
  89. $package, $this->decisionQueueWhy[$i]
  90. );
  91. }
  92. }
  93. }
  94. $allDecidedMap = $this->decisionMap;
  95. foreach ($this->decisionMap as $packageId => $decision) {
  96. if ($decision != 0) {
  97. $package = $this->pool->packageById($packageId);
  98. if ($package instanceof AliasPackage) {
  99. $allDecidedMap[$package->getAliasOf()->getId()] = $decision;
  100. }
  101. }
  102. }
  103. foreach ($allDecidedMap as $packageId => $decision) {
  104. if ($packageId === 0) {
  105. continue;
  106. }
  107. if (0 == $decision && isset($this->installedMap[$packageId])) {
  108. $package = $this->pool->packageById($packageId);
  109. if ($package instanceof AliasPackage) {
  110. $transaction[] = new Operation\MarkAliasInstalledOperation(
  111. $package, null
  112. );
  113. } else {
  114. $transaction[] = new Operation\UninstallOperation(
  115. $package, null
  116. );
  117. }
  118. $this->decisionMap[$packageId] = -1;
  119. }
  120. }
  121. foreach ($this->decisionMap as $packageId => $decision) {
  122. if ($packageId === 0) {
  123. continue;
  124. }
  125. if (0 == $decision && isset($this->installedMap[$packageId])) {
  126. $package = $this->pool->packageById($packageId);
  127. if ($package instanceof AliasPackage) {
  128. $transaction[] = new Operation\MarkAliasInstalledOperation(
  129. $package, null
  130. );
  131. } else {
  132. $transaction[] = new Operation\UninstallOperation(
  133. $package, null
  134. );
  135. }
  136. $this->decisionMap[$packageId] = -1;
  137. }
  138. }
  139. return array_reverse($transaction);
  140. }
  141. }