Locker.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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\Installer\InstallationManager;
  14. use Composer\Repository\RepositoryManager;
  15. use Composer\Util\ProcessExecutor;
  16. use Composer\Package\AliasPackage;
  17. /**
  18. * Reads/writes project lockfile (composer.lock).
  19. *
  20. * @author Konstantin Kudryashiv <ever.zet@gmail.com>
  21. * @author Jordi Boggiano <j.boggiano@seld.be>
  22. */
  23. class Locker
  24. {
  25. private $lockFile;
  26. private $repositoryManager;
  27. private $installationManager;
  28. private $hash;
  29. private $lockDataCache;
  30. /**
  31. * Initializes packages locker.
  32. *
  33. * @param JsonFile $lockFile lockfile loader
  34. * @param RepositoryManager $repositoryManager repository manager instance
  35. * @param InstallationManager $installationManager installation manager instance
  36. * @param string $hash unique hash of the current composer configuration
  37. */
  38. public function __construct(JsonFile $lockFile, RepositoryManager $repositoryManager, InstallationManager $installationManager, $hash)
  39. {
  40. $this->lockFile = $lockFile;
  41. $this->repositoryManager = $repositoryManager;
  42. $this->installationManager = $installationManager;
  43. $this->hash = $hash;
  44. }
  45. /**
  46. * Checks whether locker were been locked (lockfile found).
  47. *
  48. * @param bool $dev true to check if dev packages are locked
  49. * @return bool
  50. */
  51. public function isLocked($dev = false)
  52. {
  53. if (!$this->lockFile->exists()) {
  54. return false;
  55. }
  56. $data = $this->getLockData();
  57. if ($dev) {
  58. return isset($data['packages-dev']);
  59. }
  60. return isset($data['packages']);
  61. }
  62. /**
  63. * Checks whether the lock file is still up to date with the current hash
  64. *
  65. * @return bool
  66. */
  67. public function isFresh()
  68. {
  69. $lock = $this->lockFile->read();
  70. return $this->hash === $lock['hash'];
  71. }
  72. /**
  73. * Searches and returns an array of locked packages, retrieved from registered repositories.
  74. *
  75. * @param bool $dev true to retrieve the locked dev packages
  76. * @return array
  77. */
  78. public function getLockedPackages($dev = false)
  79. {
  80. $lockData = $this->getLockData();
  81. $packages = array();
  82. $lockedPackages = $dev ? $lockData['packages-dev'] : $lockData['packages'];
  83. $repo = $dev ? $this->repositoryManager->getLocalDevRepository() : $this->repositoryManager->getLocalRepository();
  84. foreach ($lockedPackages as $info) {
  85. $resolvedVersion = !empty($info['alias-version']) ? $info['alias-version'] : $info['version'];
  86. // try to find the package in the local repo (best match)
  87. $package = $repo->findPackage($info['package'], $resolvedVersion);
  88. // try to find the package in any repo
  89. if (!$package) {
  90. $package = $this->repositoryManager->findPackage($info['package'], $resolvedVersion);
  91. }
  92. // try to find the package in any repo (second pass without alias + rebuild alias since it disappeared)
  93. if (!$package && !empty($info['alias-version'])) {
  94. $package = $this->repositoryManager->findPackage($info['package'], $info['version']);
  95. if ($package) {
  96. $alias = new AliasPackage($package, $info['alias-version'], $info['alias-pretty-version']);
  97. $package->getRepository()->addPackage($alias);
  98. $package = $alias;
  99. }
  100. }
  101. if (!$package) {
  102. throw new \LogicException(sprintf(
  103. 'Can not find "%s-%s" package in registered repositories',
  104. $info['package'], $info['version']
  105. ));
  106. }
  107. $packages[] = $package;
  108. }
  109. return $packages;
  110. }
  111. public function getMinimumStability()
  112. {
  113. $lockData = $this->getLockData();
  114. return isset($lockData['minimum-stability']) ? $lockData['minimum-stability'] : 'stable';
  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 bool
  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. if ('git' === $package->getSourceType() && $path = $this->installationManager->getInstallPath($package)) {
  186. $process = new ProcessExecutor();
  187. if (0 === $process->execute('git log -n1 --pretty=%ct '.escapeshellarg($package->getSourceReference()), $output, $path)) {
  188. $spec['commit-date'] = trim($output);
  189. }
  190. }
  191. }
  192. if ($alias) {
  193. $spec['alias-pretty-version'] = $alias->getPrettyVersion();
  194. $spec['alias-version'] = $alias->getVersion();
  195. }
  196. $locked[] = $spec;
  197. }
  198. usort($locked, function ($a, $b) {
  199. $comparison = strcmp($a['package'], $b['package']);
  200. if (0 !== $comparison) {
  201. return $comparison;
  202. }
  203. // If it is the same package, compare the versions to make the order deterministic
  204. $aVersion = isset($a['alias-version']) ? $a['alias-version'] : $a['version'];
  205. $bVersion = isset($b['alias-version']) ? $b['alias-version'] : $b['version'];
  206. return strcmp($aVersion, $bVersion);
  207. });
  208. return $locked;
  209. }
  210. }