Locker.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. use Composer\Repository\ArrayRepository;
  18. use Composer\Package\Dumper\ArrayDumper;
  19. use Composer\Package\Loader\ArrayLoader;
  20. /**
  21. * Reads/writes project lockfile (composer.lock).
  22. *
  23. * @author Konstantin Kudryashiv <ever.zet@gmail.com>
  24. * @author Jordi Boggiano <j.boggiano@seld.be>
  25. */
  26. class Locker
  27. {
  28. private $lockFile;
  29. private $repositoryManager;
  30. private $installationManager;
  31. private $hash;
  32. private $loader;
  33. private $dumper;
  34. private $lockDataCache;
  35. /**
  36. * Initializes packages locker.
  37. *
  38. * @param JsonFile $lockFile lockfile loader
  39. * @param RepositoryManager $repositoryManager repository manager instance
  40. * @param InstallationManager $installationManager installation manager instance
  41. * @param string $hash unique hash of the current composer configuration
  42. */
  43. public function __construct(JsonFile $lockFile, RepositoryManager $repositoryManager, InstallationManager $installationManager, $hash)
  44. {
  45. $this->lockFile = $lockFile;
  46. $this->repositoryManager = $repositoryManager;
  47. $this->installationManager = $installationManager;
  48. $this->hash = $hash;
  49. $this->loader = new ArrayLoader();
  50. $this->dumper = new ArrayDumper();
  51. }
  52. /**
  53. * Checks whether locker were been locked (lockfile found).
  54. *
  55. * @param bool $dev true to check if dev packages are locked
  56. * @return bool
  57. */
  58. public function isLocked($dev = false)
  59. {
  60. if (!$this->lockFile->exists()) {
  61. return false;
  62. }
  63. $data = $this->getLockData();
  64. if ($dev) {
  65. return isset($data['packages-dev']);
  66. }
  67. return isset($data['packages']);
  68. }
  69. /**
  70. * Checks whether the lock file is still up to date with the current hash
  71. *
  72. * @return bool
  73. */
  74. public function isFresh()
  75. {
  76. $lock = $this->lockFile->read();
  77. return $this->hash === $lock['hash'];
  78. }
  79. /**
  80. * Checks whether the lock file is in the new complete format or not
  81. *
  82. * @param bool $dev true to check in dev mode
  83. * @return bool
  84. */
  85. public function isCompleteFormat($dev)
  86. {
  87. $lockData = $this->getLockData();
  88. $lockedPackages = $dev ? $lockData['packages-dev'] : $lockData['packages'];
  89. if (empty($lockedPackages) || isset($lockedPackages[0]['name'])) {
  90. return true;
  91. }
  92. return false;
  93. }
  94. /**
  95. * Searches and returns an array of locked packages, retrieved from registered repositories.
  96. *
  97. * @param bool $dev true to retrieve the locked dev packages
  98. * @return \Composer\Repository\RepositoryInterface
  99. */
  100. public function getLockedRepository($dev = false)
  101. {
  102. $lockData = $this->getLockData();
  103. $packages = new ArrayRepository();
  104. $lockedPackages = $dev ? $lockData['packages-dev'] : $lockData['packages'];
  105. if (empty($lockedPackages)) {
  106. return $packages;
  107. }
  108. if (isset($lockedPackages[0]['name'])) {
  109. foreach ($lockedPackages as $info) {
  110. $packages->addPackage($this->loader->load($info));
  111. }
  112. return $packages;
  113. }
  114. // legacy lock file support
  115. $repo = $dev ? $this->repositoryManager->getLocalDevRepository() : $this->repositoryManager->getLocalRepository();
  116. foreach ($lockedPackages as $info) {
  117. $resolvedVersion = !empty($info['alias-version']) ? $info['alias-version'] : $info['version'];
  118. // try to find the package in the local repo (best match)
  119. $package = $repo->findPackage($info['package'], $resolvedVersion);
  120. // try to find the package in any repo
  121. if (!$package) {
  122. $package = $this->repositoryManager->findPackage($info['package'], $resolvedVersion);
  123. }
  124. // try to find the package in any repo (second pass without alias + rebuild alias since it disappeared)
  125. if (!$package && !empty($info['alias-version'])) {
  126. $package = $this->repositoryManager->findPackage($info['package'], $info['version']);
  127. if ($package) {
  128. $package->setAlias($info['alias-version']);
  129. $package->setPrettyAlias($info['alias-pretty-version']);
  130. }
  131. }
  132. if (!$package) {
  133. throw new \LogicException(sprintf(
  134. 'Can not find "%s-%s" package in registered repositories',
  135. $info['package'], $info['version']
  136. ));
  137. }
  138. $package = clone $package;
  139. if (!empty($info['time'])) {
  140. $package->setReleaseDate($info['time']);
  141. }
  142. if (!empty($info['source-reference'])) {
  143. $package->setSourceReference($info['source-reference']);
  144. if (is_callable($package, 'setDistReference')) {
  145. $package->setDistReference($info['source-reference']);
  146. }
  147. }
  148. $packages->addPackage($package);
  149. }
  150. return $packages;
  151. }
  152. public function getMinimumStability()
  153. {
  154. $lockData = $this->getLockData();
  155. return isset($lockData['minimum-stability']) ? $lockData['minimum-stability'] : 'stable';
  156. }
  157. public function getStabilityFlags()
  158. {
  159. $lockData = $this->getLockData();
  160. return isset($lockData['stability-flags']) ? $lockData['stability-flags'] : array();
  161. }
  162. public function getAliases()
  163. {
  164. $lockData = $this->getLockData();
  165. return isset($lockData['aliases']) ? $lockData['aliases'] : array();
  166. }
  167. public function getLockData()
  168. {
  169. if (null !== $this->lockDataCache) {
  170. return $this->lockDataCache;
  171. }
  172. if (!$this->lockFile->exists()) {
  173. throw new \LogicException('No lockfile found. Unable to read locked packages');
  174. }
  175. return $this->lockDataCache = $this->lockFile->read();
  176. }
  177. /**
  178. * Locks provided data into lockfile.
  179. *
  180. * @param array $packages array of packages
  181. * @param mixed $devPackages array of dev packages or null if installed without --dev
  182. * @param array $aliases array of aliases
  183. * @param string $minimumStability
  184. * @param array $stabilityFlags
  185. *
  186. * @return bool
  187. */
  188. public function setLockData(array $packages, $devPackages, array $aliases, $minimumStability, array $stabilityFlags)
  189. {
  190. $lock = array(
  191. 'hash' => $this->hash,
  192. 'packages' => null,
  193. 'packages-dev' => null,
  194. 'aliases' => array(),
  195. 'minimum-stability' => $minimumStability,
  196. 'stability-flags' => $stabilityFlags,
  197. );
  198. foreach ($aliases as $package => $versions) {
  199. foreach ($versions as $version => $alias) {
  200. $lock['aliases'][] = array(
  201. 'alias' => $alias['alias'],
  202. 'alias_normalized' => $alias['alias_normalized'],
  203. 'version' => $version,
  204. 'package' => $package,
  205. );
  206. }
  207. }
  208. $lock['packages'] = $this->lockPackages($packages);
  209. if (null !== $devPackages) {
  210. $lock['packages-dev'] = $this->lockPackages($devPackages);
  211. }
  212. if (empty($lock['packages']) && empty($lock['packages-dev'])) {
  213. if ($this->lockFile->exists()) {
  214. unlink($this->lockFile->getPath());
  215. }
  216. return false;
  217. }
  218. if (!$this->isLocked() || $lock !== $this->getLockData()) {
  219. $this->lockFile->write($lock);
  220. $this->lockDataCache = null;
  221. return true;
  222. }
  223. return false;
  224. }
  225. private function lockPackages(array $packages)
  226. {
  227. $locked = array();
  228. foreach ($packages as $package) {
  229. if ($package instanceof AliasPackage) {
  230. continue;
  231. }
  232. $name = $package->getPrettyName();
  233. $version = $package->getPrettyVersion();
  234. if (!$name || !$version) {
  235. throw new \LogicException(sprintf(
  236. 'Package "%s" has no version or name and can not be locked', $package
  237. ));
  238. }
  239. $spec = $this->dumper->dump($package);
  240. unset($spec['version_normalized']);
  241. if ($package->isDev()) {
  242. $time = $this->getPackageTime($package);
  243. if (null !== $time) {
  244. $spec['time'] = $time;
  245. }
  246. }
  247. unset($spec['installation-source']);
  248. $locked[] = $spec;
  249. }
  250. usort($locked, function ($a, $b) {
  251. $comparison = strcmp($a['name'], $b['name']);
  252. if (0 !== $comparison) {
  253. return $comparison;
  254. }
  255. // If it is the same package, compare the versions to make the order deterministic
  256. return strcmp($a['version'], $b['version']);
  257. });
  258. return $locked;
  259. }
  260. /**
  261. * Returns the packages's datetime for its source reference.
  262. *
  263. * @param PackageInterface $package The package to scan.
  264. * @return string|null The formatted datetime or null if none was found.
  265. */
  266. private function getPackageTime(PackageInterface $package)
  267. {
  268. if (!function_exists('proc_open')) {
  269. return null;
  270. }
  271. $path = $this->installationManager->getInstallPath($package);
  272. $sourceType = $package->getSourceType();
  273. $datetime = null;
  274. if ($path && in_array($sourceType, array('git', 'hg'))) {
  275. $sourceRef = $package->getSourceReference() ?: $package->getDistReference();
  276. $process = new ProcessExecutor();
  277. switch ($sourceType) {
  278. case 'git':
  279. if (0 === $process->execute('git log -n1 --pretty=%ct '.escapeshellarg($sourceRef), $output, $path) && preg_match('{^\s*\d+\s*$}', $output)) {
  280. $datetime = new \DateTime('@'.trim($output), new \DateTimeZone('UTC'));
  281. }
  282. break;
  283. case 'hg':
  284. if (0 === $process->execute('hg log --template "{date|hgdate}" -r '.escapeshellarg($sourceRef), $output, $path) && preg_match('{^\s*(\d+)\s*}', $output, $match)) {
  285. $datetime = new \DateTime('@'.$match[1], new \DateTimeZone('UTC'));
  286. }
  287. break;
  288. }
  289. }
  290. return $datetime ? $datetime->format('Y-m-d H:i:s') : null;
  291. }
  292. }