Locker.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. private $lockDataCache;
  26. /**
  27. * Initializes packages locker.
  28. *
  29. * @param JsonFile $lockFile lockfile loader
  30. * @param RepositoryManager $repositoryManager repository manager instance
  31. * @param string $hash unique hash of the current composer configuration
  32. */
  33. public function __construct(JsonFile $lockFile, RepositoryManager $repositoryManager, $hash)
  34. {
  35. $this->lockFile = $lockFile;
  36. $this->repositoryManager = $repositoryManager;
  37. $this->hash = $hash;
  38. }
  39. /**
  40. * Checks whether locker were been locked (lockfile found).
  41. *
  42. * @return Boolean
  43. */
  44. public function isLocked()
  45. {
  46. return $this->lockFile->exists();
  47. }
  48. /**
  49. * Checks whether the lock file is still up to date with the current hash
  50. *
  51. * @return Boolean
  52. */
  53. public function isFresh()
  54. {
  55. $lock = $this->lockFile->read();
  56. return $this->hash === $lock['hash'];
  57. }
  58. /**
  59. * Searches and returns an array of locked packages, retrieved from registered repositories.
  60. *
  61. * @return array
  62. */
  63. public function getLockedPackages($dev = false)
  64. {
  65. $lockList = $this->getLockData();
  66. $packages = array();
  67. $lockedPackages = $dev ? $lockList['packages-dev'] : $lockList['packages'];
  68. $repo = $dev ? $this->repositoryManager->getLocalDevRepository() : $this->repositoryManager->getLocalRepository();
  69. foreach ($lockedPackages as $info) {
  70. $resolvedVersion = !empty($info['alias']) ? $info['alias'] : $info['version'];
  71. $package = $repo->findPackage($info['package'], $resolvedVersion);
  72. if (!$package) {
  73. $package = $this->repositoryManager->findPackage($info['package'], $info['version']);
  74. if ($package && !empty($info['alias'])) {
  75. $package = new AliasPackage($package, $info['alias'], $info['alias']);
  76. }
  77. }
  78. if (!$package) {
  79. throw new \LogicException(sprintf(
  80. 'Can not find "%s-%s" package in registered repositories',
  81. $info['package'], $info['version']
  82. ));
  83. }
  84. $packages[] = $package;
  85. }
  86. return $packages;
  87. }
  88. public function getAliases()
  89. {
  90. $lockList = $this->getLockData();
  91. return isset($lockList['aliases']) ? $lockList['aliases'] : array();
  92. }
  93. public function getLockData()
  94. {
  95. if (!$this->isLocked()) {
  96. throw new \LogicException('No lockfile found. Unable to read locked packages');
  97. }
  98. if (null !== $this->lockDataCache) {
  99. return $this->lockDataCache;
  100. }
  101. return $this->lockDataCache = $this->lockFile->read();
  102. }
  103. /**
  104. * Locks provided data into lockfile.
  105. *
  106. * @param array $packages array of packages
  107. * @param array $packages array of dev packages
  108. * @param array $aliases array of aliases
  109. *
  110. * @return Boolean
  111. */
  112. public function setLockData(array $packages, array $devPackages, array $aliases)
  113. {
  114. $lock = array(
  115. 'hash' => $this->hash,
  116. 'packages' => array(),
  117. 'packages-dev' => array(),
  118. 'aliases' => $aliases,
  119. );
  120. $lock['packages'] = $this->lockPackages($packages);
  121. $lock['packages-dev'] = $this->lockPackages($devPackages);
  122. if (!$this->isLocked() || $lock !== $this->getLockData()) {
  123. $this->lockFile->write($lock);
  124. $this->lockDataCache = null;
  125. return true;
  126. }
  127. return false;
  128. }
  129. private function lockPackages(array $packages)
  130. {
  131. $locked = array();
  132. foreach ($packages as $package) {
  133. $name = $package->getPrettyName();
  134. $version = $package->getPrettyVersion();
  135. if (!$name || !$version) {
  136. throw new \LogicException(sprintf(
  137. 'Package "%s" has no version or name and can not be locked', $package
  138. ));
  139. }
  140. $spec = array('package' => $name, 'version' => $version);
  141. if ($package->isDev()) {
  142. $spec['source-reference'] = $package->getSourceReference();
  143. }
  144. if ($package->getAlias() && $package->isInstalledAsAlias()) {
  145. $spec['alias'] = $package->getAlias();
  146. }
  147. $locked[] = $spec;
  148. }
  149. usort($locked, function ($a, $b) {
  150. return strcmp($a['package'], $b['package']);
  151. });
  152. return $locked;
  153. }
  154. }