Locker.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. // TODO BC remove this after June 10th
  80. if (isset($info['alias']) && empty($warned)) {
  81. $warned = true;
  82. echo 'BC warning: your lock file appears to be of an older format than this composer version, it is recommended to run composer update'.PHP_EOL;
  83. }
  84. $resolvedVersion = !empty($info['alias-version']) ? $info['alias-version'] : $info['version'];
  85. // try to find the package in the local repo (best match)
  86. $package = $repo->findPackage($info['package'], $resolvedVersion);
  87. // try to find the package in any repo
  88. if (!$package) {
  89. $package = $this->repositoryManager->findPackage($info['package'], $resolvedVersion);
  90. }
  91. // try to find the package in any repo (second pass without alias + rebuild alias since it disappeared)
  92. if (!$package && !empty($info['alias-version'])) {
  93. $package = $this->repositoryManager->findPackage($info['package'], $info['version']);
  94. if ($package) {
  95. $alias = new AliasPackage($package, $info['alias-version'], $info['alias-pretty-version']);
  96. $package->getRepository()->addPackage($alias);
  97. $package = $alias;
  98. }
  99. }
  100. if (!$package) {
  101. throw new \LogicException(sprintf(
  102. 'Can not find "%s-%s" package in registered repositories',
  103. $info['package'], $info['version']
  104. ));
  105. }
  106. $packages[] = $package;
  107. }
  108. return $packages;
  109. }
  110. public function getAliases()
  111. {
  112. $lockList = $this->getLockData();
  113. return isset($lockList['aliases']) ? $lockList['aliases'] : array();
  114. }
  115. public function getLockData()
  116. {
  117. if (!$this->lockFile->exists()) {
  118. throw new \LogicException('No lockfile found. Unable to read locked packages');
  119. }
  120. if (null !== $this->lockDataCache) {
  121. return $this->lockDataCache;
  122. }
  123. return $this->lockDataCache = $this->lockFile->read();
  124. }
  125. /**
  126. * Locks provided data into lockfile.
  127. *
  128. * @param array $packages array of packages
  129. * @param mixed $packages array of dev packages or null if installed without --dev
  130. * @param array $aliases array of aliases
  131. *
  132. * @return Boolean
  133. */
  134. public function setLockData(array $packages, $devPackages, array $aliases)
  135. {
  136. $lock = array(
  137. 'hash' => $this->hash,
  138. 'packages' => null,
  139. 'packages-dev' => null,
  140. 'aliases' => $aliases,
  141. );
  142. $lock['packages'] = $this->lockPackages($packages);
  143. if (null !== $devPackages) {
  144. $lock['packages-dev'] = $this->lockPackages($devPackages);
  145. }
  146. if (!$this->isLocked() || $lock !== $this->getLockData()) {
  147. $this->lockFile->write($lock);
  148. $this->lockDataCache = null;
  149. return true;
  150. }
  151. return false;
  152. }
  153. private function lockPackages(array $packages)
  154. {
  155. $locked = array();
  156. foreach ($packages as $package) {
  157. $alias = null;
  158. if ($package instanceof AliasPackage) {
  159. $alias = $package;
  160. $package = $package->getAliasOf();
  161. }
  162. $name = $package->getPrettyName();
  163. $version = $package->getPrettyVersion();
  164. if (!$name || !$version) {
  165. throw new \LogicException(sprintf(
  166. 'Package "%s" has no version or name and can not be locked', $package
  167. ));
  168. }
  169. $spec = array('package' => $name, 'version' => $version);
  170. if ($package->isDev()) {
  171. $spec['source-reference'] = $package->getSourceReference();
  172. }
  173. if ($alias) {
  174. $spec['alias-pretty-version'] = $alias->getPrettyVersion();
  175. $spec['alias-version'] = $alias->getVersion();
  176. }
  177. $locked[] = $spec;
  178. }
  179. usort($locked, function ($a, $b) {
  180. return strcmp($a['package'], $b['package']);
  181. });
  182. return $locked;
  183. }
  184. }