Request.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. protected $lockedRepository;
  23. protected $requires = array();
  24. protected $fixedPackages = array();
  25. protected $unlockables = array();
  26. public function __construct(LockArrayRepository $lockedRepository = null)
  27. {
  28. $this->lockedRepository = $lockedRepository;
  29. }
  30. public function require($packageName, ConstraintInterface $constraint = null)
  31. {
  32. $packageName = strtolower($packageName);
  33. $this->requires[$packageName] = $constraint;
  34. }
  35. /**
  36. * Mark an existing package as being installed and having to remain installed
  37. *
  38. * @param bool $lockable if set to false, the package will not be written to the lock file
  39. */
  40. public function fixPackage(PackageInterface $package, $lockable = true)
  41. {
  42. $this->fixedPackages[spl_object_hash($package)] = $package;
  43. if (!$lockable) {
  44. $this->unlockables[] = $package;
  45. }
  46. }
  47. public function getRequires()
  48. {
  49. return $this->requires;
  50. }
  51. public function getFixedPackages()
  52. {
  53. return $this->fixedPackages;
  54. }
  55. public function isFixedPackage(PackageInterface $package)
  56. {
  57. return isset($this->fixedPackages[spl_object_hash($package)]);
  58. }
  59. // TODO look into removing the packageIds option, the only place true is used is for the installed map in the solver problems
  60. // some locked packages may not be in the pool so they have a package->id of -1
  61. public function getPresentMap($packageIds = false)
  62. {
  63. $presentMap = array();
  64. if ($this->lockedRepository) {
  65. foreach ($this->lockedRepository->getPackages() as $package) {
  66. $presentMap[$packageIds ? $package->id : spl_object_hash($package)] = $package;
  67. }
  68. }
  69. foreach ($this->fixedPackages as $package) {
  70. $presentMap[$packageIds ? $package->id : spl_object_hash($package)] = $package;
  71. }
  72. return $presentMap;
  73. }
  74. public function getUnlockableMap()
  75. {
  76. $unlockableMap = array();
  77. foreach ($this->unlockables as $package) {
  78. $unlockableMap[$package->id] = $package;
  79. }
  80. return $unlockableMap;
  81. }
  82. public function getLockedRepository()
  83. {
  84. return $this->lockedRepository;
  85. }
  86. }