LockTransaction.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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\DependencyResolver\Operation\OperationInterface;
  13. use Composer\Package\AliasPackage;
  14. use Composer\Package\RootAliasPackage;
  15. use Composer\Package\RootPackageInterface;
  16. use Composer\Repository\ArrayRepository;
  17. use Composer\Repository\RepositoryInterface;
  18. use Composer\Test\Repository\ArrayRepositoryTest;
  19. /**
  20. * @author Nils Adermann <naderman@naderman.de>
  21. */
  22. class LockTransaction extends Transaction
  23. {
  24. /**
  25. * packages in current lock file, platform repo or otherwise present
  26. * @var array
  27. */
  28. protected $presentMap;
  29. /**
  30. * Packages which cannot be mapped, platform repo, root package, other fixed repos
  31. * @var array
  32. */
  33. protected $unlockableMap;
  34. /**
  35. * @var array
  36. */
  37. protected $resultPackages;
  38. public function __construct(Pool $pool, $presentMap, $unlockableMap, $decisions)
  39. {
  40. $this->presentMap = $presentMap;
  41. $this->unlockableMap = $unlockableMap;
  42. $this->setResultPackages($pool, $decisions);
  43. parent::__construct($this->presentMap, $this->resultPackages['all']);
  44. }
  45. // TODO make this a bit prettier instead of the two text indexes?
  46. public function setResultPackages(Pool $pool, Decisions $decisions)
  47. {
  48. $this->resultPackages = array('all' => array(), 'non-dev' => array(), 'dev' => array());
  49. foreach ($decisions as $i => $decision) {
  50. $literal = $decision[Decisions::DECISION_LITERAL];
  51. if ($literal > 0) {
  52. $package = $pool->literalToPackage($literal);
  53. $this->resultPackages['all'][] = $package;
  54. if (!isset($this->unlockableMap[$package->id])) {
  55. $this->resultPackages['non-dev'][] = $package;
  56. }
  57. }
  58. }
  59. }
  60. public function setNonDevPackages(LockTransaction $extractionResult)
  61. {
  62. $packages = $extractionResult->getNewLockPackages(false);
  63. $this->resultPackages['dev'] = $this->resultPackages['non-dev'];
  64. $this->resultPackages['non-dev'] = array();
  65. foreach ($packages as $package) {
  66. foreach ($this->resultPackages['dev'] as $i => $resultPackage) {
  67. // TODO this comparison is probably insufficient, aliases, what about modified versions? I guess they aren't possible?
  68. if ($package->getName() == $resultPackage->getName()) {
  69. $this->resultPackages['non-dev'][] = $resultPackage;
  70. unset($this->resultPackages['dev'][$i]);
  71. }
  72. }
  73. }
  74. }
  75. // TODO additionalFixedRepository needs to be looked at here as well?
  76. public function getNewLockPackages($devMode, $updateMirrors = false)
  77. {
  78. $packages = array();
  79. foreach ($this->resultPackages[$devMode ? 'dev' : 'non-dev'] as $package) {
  80. if (!($package instanceof AliasPackage) && !($package instanceof RootAliasPackage)) {
  81. // if we're just updating mirrors we need to reset references to the same as currently "present" packages' references to keep the lock file as-is
  82. // we do not reset references if the currently present package didn't have any, or if the type of VCS has changed
  83. if ($updateMirrors && !isset($this->presentMap[spl_object_hash($package)])) {
  84. foreach ($this->presentMap as $presentPackage) {
  85. if ($package->getName() == $presentPackage->getName() &&
  86. $package->getVersion() == $presentPackage->getVersion() &&
  87. $presentPackage->getSourceReference() &&
  88. $presentPackage->getSourceType() === $package->getSourceType()
  89. ) {
  90. $package->setSourceDistReferences($presentPackage->getSourceReference());
  91. }
  92. }
  93. }
  94. $packages[] = $package;
  95. }
  96. }
  97. return $packages;
  98. }
  99. /**
  100. * Checks which of the given aliases from composer.json are actually in use for the lock file
  101. */
  102. public function getAliases($aliases)
  103. {
  104. $usedAliases = array();
  105. foreach ($this->resultPackages['all'] as $package) {
  106. if ($package instanceof AliasPackage) {
  107. foreach ($aliases as $alias) {
  108. if ($alias['package'] === $package->getName()) {
  109. $usedAliases[] = $alias;
  110. }
  111. }
  112. }
  113. }
  114. return $usedAliases;
  115. }
  116. }