Locker.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. * @param Boolean $dev true to check if dev packages are locked
  43. * @return Boolean
  44. */
  45. public function isLocked($dev = false)
  46. {
  47. if (!$this->lockFile->exists()) {
  48. return false;
  49. }
  50. $data = $this->getLockData();
  51. if ($dev) {
  52. return isset($data['packages-dev']);
  53. }
  54. return isset($data['packages']);
  55. }
  56. /**
  57. * Checks whether the lock file is still up to date with the current hash
  58. *
  59. * @return Boolean
  60. */
  61. public function isFresh()
  62. {
  63. $lock = $this->lockFile->read();
  64. return $this->hash === $lock['hash'];
  65. }
  66. /**
  67. * Searches and returns an array of locked packages, retrieved from registered repositories.
  68. *
  69. * @param Boolean $dev true to retrieve the locked dev packages
  70. * @return array
  71. */
  72. public function getLockedPackages($dev = false)
  73. {
  74. $lockList = $this->getLockData();
  75. $packages = array();
  76. $lockedPackages = $dev ? $lockList['packages-dev'] : $lockList['packages'];
  77. $repo = $dev ? $this->repositoryManager->getLocalDevRepository() : $this->repositoryManager->getLocalRepository();
  78. foreach ($lockedPackages as $info) {
  79. $resolvedVersion = !empty($info['alias-version']) ? $info['alias-version'] : $info['version'];
  80. // try to find the package in the local repo (best match)
  81. $package = $repo->findPackage($info['package'], $resolvedVersion);
  82. // try to find the package in any repo
  83. if (!$package) {
  84. $package = $this->repositoryManager->findPackage($info['package'], $resolvedVersion);
  85. }
  86. // try to find the package in any repo (second pass without alias + rebuild alias since it disappeared)
  87. if (!$package && !empty($info['alias-version'])) {
  88. $package = $this->repositoryManager->findPackage($info['package'], $info['version']);
  89. if ($package) {
  90. $alias = new AliasPackage($package, $info['alias-version'], $info['alias-pretty-version']);
  91. $package->getRepository()->addPackage($alias);
  92. $package = $alias;
  93. }
  94. }
  95. if (!$package) {
  96. throw new \LogicException(sprintf(
  97. 'Can not find "%s-%s" package in registered repositories',
  98. $info['package'], $info['version']
  99. ));
  100. }
  101. $packages[] = $package;
  102. }
  103. return $packages;
  104. }
  105. public function getAliases()
  106. {
  107. $lockList = $this->getLockData();
  108. return isset($lockList['aliases']) ? $lockList['aliases'] : array();
  109. }
  110. public function getLockData()
  111. {
  112. if (!$this->lockFile->exists()) {
  113. throw new \LogicException('No lockfile found. Unable to read locked packages');
  114. }
  115. if (null !== $this->lockDataCache) {
  116. return $this->lockDataCache;
  117. }
  118. return $this->lockDataCache = $this->lockFile->read();
  119. }
  120. /**
  121. * Locks provided data into lockfile.
  122. *
  123. * @param array $packages array of packages
  124. * @param mixed $packages array of dev packages or null if installed without --dev
  125. * @param array $aliases array of aliases
  126. *
  127. * @return Boolean
  128. */
  129. public function setLockData(array $packages, $devPackages, array $aliases)
  130. {
  131. $lock = array(
  132. 'hash' => $this->hash,
  133. 'packages' => null,
  134. 'packages-dev' => null,
  135. 'aliases' => $aliases,
  136. );
  137. $lock['packages'] = $this->lockPackages($packages);
  138. if (null !== $devPackages) {
  139. $lock['packages-dev'] = $this->lockPackages($devPackages);
  140. }
  141. if (!$this->isLocked() || $lock !== $this->getLockData()) {
  142. $this->lockFile->write($lock);
  143. $this->lockDataCache = null;
  144. return true;
  145. }
  146. return false;
  147. }
  148. private function lockPackages(array $packages)
  149. {
  150. $locked = array();
  151. foreach ($packages as $package) {
  152. $alias = null;
  153. if ($package instanceof AliasPackage) {
  154. $alias = $package;
  155. $package = $package->getAliasOf();
  156. }
  157. $name = $package->getPrettyName();
  158. $version = $package->getPrettyVersion();
  159. if (!$name || !$version) {
  160. throw new \LogicException(sprintf(
  161. 'Package "%s" has no version or name and can not be locked', $package
  162. ));
  163. }
  164. $spec = array('package' => $name, 'version' => $version);
  165. if ($package->isDev()) {
  166. $spec['source-reference'] = $package->getSourceReference();
  167. }
  168. if ($alias) {
  169. $spec['alias-pretty-version'] = $alias->getPrettyVersion();
  170. $spec['alias-version'] = $alias->getVersion();
  171. }
  172. $locked[] = $spec;
  173. }
  174. usort($locked, function ($a, $b) {
  175. return strcmp($a['package'], $b['package']);
  176. });
  177. return $locked;
  178. }
  179. }