Request.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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\Package;
  13. use Composer\Package\PackageInterface;
  14. use Composer\Package\RootAliasPackage;
  15. use Composer\Repository\LockArrayRepository;
  16. use Composer\Semver\Constraint\ConstraintInterface;
  17. /**
  18. * @author Nils Adermann <naderman@naderman.de>
  19. */
  20. class Request
  21. {
  22. /**
  23. * Identifies a partial update for listed packages only, all dependencies will remain at locked versions
  24. */
  25. const UPDATE_ONLY_LISTED = 0;
  26. /**
  27. * Identifies a partial update for listed packages and recursively all their dependencies, however dependencies
  28. * also directly required by the root composer.json and their dependencies will remain at the locked version.
  29. */
  30. const UPDATE_LISTED_WITH_TRANSITIVE_DEPS_NO_ROOT_REQUIRE = 1;
  31. /**
  32. * Identifies a partial update for listed packages and recursively all their dependencies, even dependencies
  33. * also directly required by the root composer.json will be updated.
  34. */
  35. const UPDATE_LISTED_WITH_TRANSITIVE_DEPS = 2;
  36. protected $lockedRepository;
  37. protected $requires = array();
  38. protected $fixedPackages = array();
  39. protected $unlockables = array();
  40. protected $updateAllowList = array();
  41. protected $updateAllowTransitiveDependencies = false;
  42. public function __construct(LockArrayRepository $lockedRepository = null)
  43. {
  44. $this->lockedRepository = $lockedRepository;
  45. }
  46. public function requireName($packageName, ConstraintInterface $constraint = null)
  47. {
  48. $packageName = strtolower($packageName);
  49. $this->requires[$packageName] = $constraint;
  50. }
  51. /**
  52. * Mark an existing package as being installed and having to remain installed
  53. *
  54. * @param bool $lockable if set to false, the package will not be written to the lock file
  55. */
  56. public function fixPackage(PackageInterface $package, $lockable = true)
  57. {
  58. $this->fixedPackages[spl_object_hash($package)] = $package;
  59. if (!$lockable) {
  60. $this->unlockables[spl_object_hash($package)] = $package;
  61. }
  62. }
  63. public function unfixPackage(PackageInterface $package)
  64. {
  65. unset($this->fixedPackages[spl_object_hash($package)]);
  66. unset($this->unlockables[spl_object_hash($package)]);
  67. }
  68. public function setUpdateAllowList($updateAllowList, $updateAllowTransitiveDependencies)
  69. {
  70. $this->updateAllowList = $updateAllowList;
  71. $this->updateAllowTransitiveDependencies = $updateAllowTransitiveDependencies;
  72. }
  73. public function getUpdateAllowList()
  74. {
  75. return $this->updateAllowList;
  76. }
  77. public function getUpdateAllowTransitiveDependencies()
  78. {
  79. return $this->updateAllowTransitiveDependencies !== self::UPDATE_ONLY_LISTED;
  80. }
  81. public function getUpdateAllowTransitiveRootDependencies()
  82. {
  83. return $this->updateAllowTransitiveDependencies === self::UPDATE_LISTED_WITH_TRANSITIVE_DEPS;
  84. }
  85. public function getRequires()
  86. {
  87. return $this->requires;
  88. }
  89. public function getFixedPackages()
  90. {
  91. return $this->fixedPackages;
  92. }
  93. public function isFixedPackage(PackageInterface $package)
  94. {
  95. return isset($this->fixedPackages[spl_object_hash($package)]);
  96. }
  97. // TODO look into removing the packageIds option, the only place true is used is for the installed map in the solver problems
  98. // some locked packages may not be in the pool so they have a package->id of -1
  99. public function getPresentMap($packageIds = false)
  100. {
  101. $presentMap = array();
  102. if ($this->lockedRepository) {
  103. foreach ($this->lockedRepository->getPackages() as $package) {
  104. $presentMap[$packageIds ? $package->id : spl_object_hash($package)] = $package;
  105. }
  106. }
  107. foreach ($this->fixedPackages as $package) {
  108. $presentMap[$packageIds ? $package->id : spl_object_hash($package)] = $package;
  109. }
  110. return $presentMap;
  111. }
  112. public function getUnlockableMap()
  113. {
  114. $unlockableMap = array();
  115. foreach ($this->unlockables as $package) {
  116. $unlockableMap[$package->id] = $package;
  117. }
  118. return $unlockableMap;
  119. }
  120. public function getLockedRepository()
  121. {
  122. return $this->lockedRepository;
  123. }
  124. }