Locker.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. $lockData = $this->getLockData();
  75. $packages = array();
  76. $lockedPackages = $dev ? $lockData['packages-dev'] : $lockData['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 getMinimumStability()
  111. {
  112. $lockData = $this->getLockData();
  113. // TODO BC change dev to stable end of june?
  114. return isset($lockData['minimum-stability']) ? $lockData['minimum-stability'] : 'dev';
  115. }
  116. public function getStabilityFlags()
  117. {
  118. $lockData = $this->getLockData();
  119. return isset($lockData['stability-flags']) ? $lockData['stability-flags'] : array();
  120. }
  121. public function getAliases()
  122. {
  123. $lockData = $this->getLockData();
  124. return isset($lockData['aliases']) ? $lockData['aliases'] : array();
  125. }
  126. public function getLockData()
  127. {
  128. if (null !== $this->lockDataCache) {
  129. return $this->lockDataCache;
  130. }
  131. if (!$this->lockFile->exists()) {
  132. throw new \LogicException('No lockfile found. Unable to read locked packages');
  133. }
  134. return $this->lockDataCache = $this->lockFile->read();
  135. }
  136. /**
  137. * Locks provided data into lockfile.
  138. *
  139. * @param array $packages array of packages
  140. * @param mixed $packages array of dev packages or null if installed without --dev
  141. * @param array $aliases array of aliases
  142. *
  143. * @return Boolean
  144. */
  145. public function setLockData(array $packages, $devPackages, array $aliases, $minimumStability, array $stabilityFlags)
  146. {
  147. $lock = array(
  148. 'hash' => $this->hash,
  149. 'packages' => null,
  150. 'packages-dev' => null,
  151. 'aliases' => $aliases,
  152. 'minimum-stability' => $minimumStability,
  153. 'stability-flags' => $stabilityFlags,
  154. );
  155. $lock['packages'] = $this->lockPackages($packages);
  156. if (null !== $devPackages) {
  157. $lock['packages-dev'] = $this->lockPackages($devPackages);
  158. }
  159. if (!$this->isLocked() || $lock !== $this->getLockData()) {
  160. $this->lockFile->write($lock);
  161. $this->lockDataCache = null;
  162. return true;
  163. }
  164. return false;
  165. }
  166. private function lockPackages(array $packages)
  167. {
  168. $locked = array();
  169. foreach ($packages as $package) {
  170. $alias = null;
  171. if ($package instanceof AliasPackage) {
  172. $alias = $package;
  173. $package = $package->getAliasOf();
  174. }
  175. $name = $package->getPrettyName();
  176. $version = $package->getPrettyVersion();
  177. if (!$name || !$version) {
  178. throw new \LogicException(sprintf(
  179. 'Package "%s" has no version or name and can not be locked', $package
  180. ));
  181. }
  182. $spec = array('package' => $name, 'version' => $version);
  183. if ($package->isDev() && !$alias) {
  184. $spec['source-reference'] = $package->getSourceReference();
  185. }
  186. if ($alias) {
  187. $spec['alias-pretty-version'] = $alias->getPrettyVersion();
  188. $spec['alias-version'] = $alias->getVersion();
  189. }
  190. $locked[] = $spec;
  191. }
  192. usort($locked, function ($a, $b) {
  193. $comparison = strcmp($a['package'], $b['package']);
  194. if (0 !== $comparison) {
  195. return $comparison;
  196. }
  197. // If it is the same package, compare the versions to make the order deterministic
  198. $aVersion = isset($a['alias-version']) ? $a['alias-version'] : $a['version'];
  199. $bVersion = isset($b['alias-version']) ? $b['alias-version'] : $b['version'];
  200. return strcmp($aVersion, $bVersion);
  201. });
  202. return $locked;
  203. }
  204. }