Locker.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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\Package;
  12. use Composer\Json\JsonFile;
  13. use Composer\Repository\RepositoryManager;
  14. use Composer\Package\AliasPackage;
  15. /**
  16. * Reads/writes project lockfile (composer.lock).
  17. *
  18. * @author Konstantin Kudryashiv <ever.zet@gmail.com>
  19. */
  20. class Locker
  21. {
  22. private $lockFile;
  23. private $repositoryManager;
  24. private $hash;
  25. /**
  26. * Initializes packages locker.
  27. *
  28. * @param JsonFile $lockFile lockfile loader
  29. * @param RepositoryManager $repositoryManager repository manager instance
  30. * @param string $hash unique hash of the current composer configuration
  31. */
  32. public function __construct(JsonFile $lockFile, RepositoryManager $repositoryManager, $hash)
  33. {
  34. $this->lockFile = $lockFile;
  35. $this->repositoryManager = $repositoryManager;
  36. $this->hash = $hash;
  37. }
  38. /**
  39. * Checks whether locker were been locked (lockfile found).
  40. *
  41. * @return Boolean
  42. */
  43. public function isLocked()
  44. {
  45. return $this->lockFile->exists();
  46. }
  47. /**
  48. * Checks whether the lock file is still up to date with the current hash
  49. *
  50. * @return Boolean
  51. */
  52. public function isFresh()
  53. {
  54. $lock = $this->lockFile->read();
  55. return $this->hash === $lock['hash'];
  56. }
  57. /**
  58. * Searches and returns an array of locked packages, retrieved from registered repositories.
  59. *
  60. * @return array
  61. */
  62. public function getLockedPackages()
  63. {
  64. $lockList = $this->getLockData();
  65. $packages = array();
  66. foreach ($lockList['packages'] as $info) {
  67. $resolvedVersion = !empty($info['alias']) ? $info['alias'] : $info['version'];
  68. $package = $this->repositoryManager->getLocalRepository()->findPackage($info['package'], $resolvedVersion);
  69. if (!$package) {
  70. $package = $this->repositoryManager->findPackage($info['package'], $info['version']);
  71. if ($package && !empty($info['alias'])) {
  72. $package = new AliasPackage($package, $info['alias'], $info['alias']);
  73. }
  74. }
  75. if (!$package) {
  76. throw new \LogicException(sprintf(
  77. 'Can not find "%s-%s" package in registered repositories',
  78. $info['package'], $info['version']
  79. ));
  80. }
  81. $packages[] = $package;
  82. }
  83. return $packages;
  84. }
  85. public function getAliases()
  86. {
  87. $lockList = $this->getLockData();
  88. return isset($lockList['aliases']) ? $lockList['aliases'] : array();
  89. }
  90. public function getLockData()
  91. {
  92. if (!$this->isLocked()) {
  93. throw new \LogicException('No lockfile found. Unable to read locked packages');
  94. }
  95. return $this->lockFile->read();
  96. }
  97. /**
  98. * Locks provided data into lockfile.
  99. *
  100. * @param array $packages array of packages
  101. * @param array $aliases array of aliases
  102. */
  103. public function setLockData(array $packages, array $aliases)
  104. {
  105. $lock = array(
  106. 'hash' => $this->hash,
  107. 'packages' => array(),
  108. 'aliases' => $aliases,
  109. );
  110. foreach ($packages as $package) {
  111. $name = $package->getPrettyName();
  112. $version = $package->getPrettyVersion();
  113. if (!$name || !$version) {
  114. throw new \LogicException(sprintf(
  115. 'Package "%s" has no version or name and can not be locked', $package
  116. ));
  117. }
  118. $spec = array('package' => $name, 'version' => $version);
  119. if ($package->isDev()) {
  120. $spec['source-reference'] = $package->getSourceReference();
  121. }
  122. // TODO discriminate between really installed as alias and installed as real package
  123. if ($package->getAlias()) {
  124. $spec['alias'] = $package->getAlias();
  125. }
  126. $lock['packages'][] = $spec;
  127. }
  128. usort($lock['packages'], function ($a, $b) {
  129. return strcmp($a['package'], $b['package']);
  130. });
  131. $this->lockFile->write($lock);
  132. }
  133. }