Locker.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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\LockArrayRepository;
  15. use Composer\Repository\RepositoryManager;
  16. use Composer\Util\ProcessExecutor;
  17. use Composer\Package\Dumper\ArrayDumper;
  18. use Composer\Package\Loader\ArrayLoader;
  19. use Composer\Util\Git as GitUtil;
  20. use Composer\IO\IOInterface;
  21. use Seld\JsonLint\ParsingException;
  22. /**
  23. * Reads/writes project lockfile (composer.lock).
  24. *
  25. * @author Konstantin Kudryashiv <ever.zet@gmail.com>
  26. * @author Jordi Boggiano <j.boggiano@seld.be>
  27. */
  28. class Locker
  29. {
  30. private $lockFile;
  31. private $repositoryManager;
  32. private $installationManager;
  33. private $hash;
  34. private $contentHash;
  35. private $loader;
  36. private $dumper;
  37. private $process;
  38. private $lockDataCache;
  39. /**
  40. * Initializes packages locker.
  41. *
  42. * @param IOInterface $io
  43. * @param JsonFile $lockFile lockfile loader
  44. * @param RepositoryManager $repositoryManager repository manager instance
  45. * @param InstallationManager $installationManager installation manager instance
  46. * @param string $composerFileContents The contents of the composer file
  47. */
  48. public function __construct(IOInterface $io, JsonFile $lockFile, RepositoryManager $repositoryManager, InstallationManager $installationManager, $composerFileContents)
  49. {
  50. $this->lockFile = $lockFile;
  51. $this->repositoryManager = $repositoryManager;
  52. $this->installationManager = $installationManager;
  53. $this->hash = md5($composerFileContents);
  54. $this->contentHash = self::getContentHash($composerFileContents);
  55. $this->loader = new ArrayLoader(null, true);
  56. $this->dumper = new ArrayDumper();
  57. $this->process = new ProcessExecutor($io);
  58. }
  59. /**
  60. * Returns the md5 hash of the sorted content of the composer file.
  61. *
  62. * @param string $composerFileContents The contents of the composer file.
  63. *
  64. * @return string
  65. */
  66. public static function getContentHash($composerFileContents)
  67. {
  68. $content = json_decode($composerFileContents, true);
  69. $relevantKeys = array(
  70. 'name',
  71. 'version',
  72. 'require',
  73. 'require-dev',
  74. 'conflict',
  75. 'replace',
  76. 'provide',
  77. 'minimum-stability',
  78. 'prefer-stable',
  79. 'repositories',
  80. 'extra',
  81. );
  82. $relevantContent = array();
  83. foreach (array_intersect($relevantKeys, array_keys($content)) as $key) {
  84. $relevantContent[$key] = $content[$key];
  85. }
  86. if (isset($content['config']['platform'])) {
  87. $relevantContent['config']['platform'] = $content['config']['platform'];
  88. }
  89. ksort($relevantContent);
  90. return md5(json_encode($relevantContent));
  91. }
  92. /**
  93. * Checks whether locker has been locked (lockfile found).
  94. *
  95. * @return bool
  96. */
  97. public function isLocked()
  98. {
  99. if (!$this->lockFile->exists()) {
  100. return false;
  101. }
  102. $data = $this->getLockData();
  103. return isset($data['packages']);
  104. }
  105. /**
  106. * Checks whether the lock file is still up to date with the current hash
  107. *
  108. * @return bool
  109. */
  110. public function isFresh()
  111. {
  112. $lock = $this->lockFile->read();
  113. if (!empty($lock['content-hash'])) {
  114. // There is a content hash key, use that instead of the file hash
  115. return $this->contentHash === $lock['content-hash'];
  116. }
  117. // BC support for old lock files without content-hash
  118. if (!empty($lock['hash'])) {
  119. return $this->hash === $lock['hash'];
  120. }
  121. // should not be reached unless the lock file is corrupted, so assume it's out of date
  122. return false;
  123. }
  124. /**
  125. * Searches and returns an array of locked packages, retrieved from registered repositories.
  126. *
  127. * @param bool $withDevReqs true to retrieve the locked dev packages
  128. * @throws \RuntimeException
  129. * @return \Composer\Repository\RepositoryInterface
  130. */
  131. public function getLockedRepository($withDevReqs = false)
  132. {
  133. $lockData = $this->getLockData();
  134. $packages = new LockArrayRepository();
  135. $lockedPackages = $lockData['packages'];
  136. if ($withDevReqs) {
  137. if (isset($lockData['packages-dev'])) {
  138. $lockedPackages = array_merge($lockedPackages, $lockData['packages-dev']);
  139. } else {
  140. throw new \RuntimeException('The lock file does not contain require-dev information, run install with the --no-dev option or run update to install those packages.');
  141. }
  142. }
  143. if (empty($lockedPackages)) {
  144. return $packages;
  145. }
  146. if (isset($lockedPackages[0]['name'])) {
  147. foreach ($lockedPackages as $info) {
  148. $packages->addPackage($this->loader->load($info));
  149. }
  150. return $packages;
  151. }
  152. throw new \RuntimeException('Your composer.lock was created before 2012-09-15, and is not supported anymore. Run "composer update" to generate a new one.');
  153. }
  154. /**
  155. * Returns the platform requirements stored in the lock file
  156. *
  157. * @param bool $withDevReqs if true, the platform requirements from the require-dev block are also returned
  158. * @return \Composer\Package\Link[]
  159. */
  160. public function getPlatformRequirements($withDevReqs = false)
  161. {
  162. $lockData = $this->getLockData();
  163. $requirements = array();
  164. if (!empty($lockData['platform'])) {
  165. $requirements = $this->loader->parseLinks(
  166. '__ROOT__',
  167. '1.0.0',
  168. 'requires',
  169. isset($lockData['platform']) ? $lockData['platform'] : array()
  170. );
  171. }
  172. if ($withDevReqs && !empty($lockData['platform-dev'])) {
  173. $devRequirements = $this->loader->parseLinks(
  174. '__ROOT__',
  175. '1.0.0',
  176. 'requires',
  177. isset($lockData['platform-dev']) ? $lockData['platform-dev'] : array()
  178. );
  179. $requirements = array_merge($requirements, $devRequirements);
  180. }
  181. return $requirements;
  182. }
  183. public function getMinimumStability()
  184. {
  185. $lockData = $this->getLockData();
  186. return isset($lockData['minimum-stability']) ? $lockData['minimum-stability'] : 'stable';
  187. }
  188. public function getStabilityFlags()
  189. {
  190. $lockData = $this->getLockData();
  191. return isset($lockData['stability-flags']) ? $lockData['stability-flags'] : array();
  192. }
  193. public function getPreferStable()
  194. {
  195. $lockData = $this->getLockData();
  196. // return null if not set to allow caller logic to choose the
  197. // right behavior since old lock files have no prefer-stable
  198. return isset($lockData['prefer-stable']) ? $lockData['prefer-stable'] : null;
  199. }
  200. public function getPreferLowest()
  201. {
  202. $lockData = $this->getLockData();
  203. // return null if not set to allow caller logic to choose the
  204. // right behavior since old lock files have no prefer-lowest
  205. return isset($lockData['prefer-lowest']) ? $lockData['prefer-lowest'] : null;
  206. }
  207. public function getPlatformOverrides()
  208. {
  209. $lockData = $this->getLockData();
  210. return isset($lockData['platform-overrides']) ? $lockData['platform-overrides'] : array();
  211. }
  212. public function getAliases()
  213. {
  214. $lockData = $this->getLockData();
  215. return isset($lockData['aliases']) ? $lockData['aliases'] : array();
  216. }
  217. public function getLockData()
  218. {
  219. if (null !== $this->lockDataCache) {
  220. return $this->lockDataCache;
  221. }
  222. if (!$this->lockFile->exists()) {
  223. throw new \LogicException('No lockfile found. Unable to read locked packages');
  224. }
  225. return $this->lockDataCache = $this->lockFile->read();
  226. }
  227. /**
  228. * Locks provided data into lockfile.
  229. *
  230. * @param array $packages array of packages
  231. * @param mixed $devPackages array of dev packages or null if installed without --dev
  232. * @param array $platformReqs array of package name => constraint for required platform packages
  233. * @param mixed $platformDevReqs array of package name => constraint for dev-required platform packages
  234. * @param array $aliases array of aliases
  235. * @param string $minimumStability
  236. * @param array $stabilityFlags
  237. * @param bool $preferStable
  238. * @param bool $preferLowest
  239. * @param array $platformOverrides
  240. *
  241. * @return bool
  242. */
  243. public function setLockData(array $packages, $devPackages, array $platformReqs, $platformDevReqs, array $aliases, $minimumStability, array $stabilityFlags, $preferStable, $preferLowest, array $platformOverrides)
  244. {
  245. $lock = array(
  246. '_readme' => array('This file locks the dependencies of your project to a known state',
  247. 'Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies',
  248. 'This file is @gener'.'ated automatically', ),
  249. 'content-hash' => $this->contentHash,
  250. 'packages' => null,
  251. 'packages-dev' => null,
  252. 'aliases' => array(),
  253. 'minimum-stability' => $minimumStability,
  254. 'stability-flags' => $stabilityFlags,
  255. 'prefer-stable' => $preferStable,
  256. 'prefer-lowest' => $preferLowest,
  257. );
  258. foreach ($aliases as $package => $versions) {
  259. foreach ($versions as $version => $alias) {
  260. $lock['aliases'][] = array(
  261. 'alias' => $alias['alias'],
  262. 'alias_normalized' => $alias['alias_normalized'],
  263. 'version' => $version,
  264. 'package' => $package,
  265. );
  266. }
  267. }
  268. $lock['packages'] = $this->lockPackages($packages);
  269. if (null !== $devPackages) {
  270. $lock['packages-dev'] = $this->lockPackages($devPackages);
  271. }
  272. $lock['platform'] = $platformReqs;
  273. $lock['platform-dev'] = $platformDevReqs;
  274. if ($platformOverrides) {
  275. $lock['platform-overrides'] = $platformOverrides;
  276. }
  277. if (empty($lock['packages']) && empty($lock['packages-dev']) && empty($lock['platform']) && empty($lock['platform-dev'])) {
  278. if ($this->lockFile->exists()) {
  279. unlink($this->lockFile->getPath());
  280. }
  281. return false;
  282. }
  283. try {
  284. $isLocked = $this->isLocked();
  285. } catch (ParsingException $e) {
  286. $isLocked = false;
  287. }
  288. if (!$isLocked || $lock !== $this->getLockData()) {
  289. $this->lockFile->write($lock);
  290. $this->lockDataCache = null;
  291. return true;
  292. }
  293. return false;
  294. }
  295. private function lockPackages(array $packages)
  296. {
  297. $locked = array();
  298. foreach ($packages as $package) {
  299. if ($package instanceof AliasPackage) {
  300. continue;
  301. }
  302. $name = $package->getPrettyName();
  303. $version = $package->getPrettyVersion();
  304. if (!$name || !$version) {
  305. throw new \LogicException(sprintf(
  306. 'Package "%s" has no version or name and can not be locked',
  307. $package
  308. ));
  309. }
  310. $spec = $this->dumper->dump($package);
  311. unset($spec['version_normalized']);
  312. // always move time to the end of the package definition
  313. $time = isset($spec['time']) ? $spec['time'] : null;
  314. unset($spec['time']);
  315. if ($package->isDev() && $package->getInstallationSource() === 'source') {
  316. // use the exact commit time of the current reference if it's a dev package
  317. $time = $this->getPackageTime($package) ?: $time;
  318. }
  319. if (null !== $time) {
  320. $spec['time'] = $time;
  321. }
  322. unset($spec['installation-source']);
  323. $locked[] = $spec;
  324. }
  325. usort($locked, function ($a, $b) {
  326. $comparison = strcmp($a['name'], $b['name']);
  327. if (0 !== $comparison) {
  328. return $comparison;
  329. }
  330. // If it is the same package, compare the versions to make the order deterministic
  331. return strcmp($a['version'], $b['version']);
  332. });
  333. return $locked;
  334. }
  335. /**
  336. * Returns the packages's datetime for its source reference.
  337. *
  338. * @param PackageInterface $package The package to scan.
  339. * @return string|null The formatted datetime or null if none was found.
  340. */
  341. private function getPackageTime(PackageInterface $package)
  342. {
  343. if (!function_exists('proc_open')) {
  344. return null;
  345. }
  346. $path = realpath($this->installationManager->getInstallPath($package));
  347. $sourceType = $package->getSourceType();
  348. $datetime = null;
  349. if ($path && in_array($sourceType, array('git', 'hg'))) {
  350. $sourceRef = $package->getSourceReference() ?: $package->getDistReference();
  351. switch ($sourceType) {
  352. case 'git':
  353. GitUtil::cleanEnv();
  354. if (0 === $this->process->execute('git log -n1 --pretty=%ct '.ProcessExecutor::escape($sourceRef), $output, $path) && preg_match('{^\s*\d+\s*$}', $output)) {
  355. $datetime = new \DateTime('@'.trim($output), new \DateTimeZone('UTC'));
  356. }
  357. break;
  358. case 'hg':
  359. if (0 === $this->process->execute('hg log --template "{date|hgdate}" -r '.ProcessExecutor::escape($sourceRef), $output, $path) && preg_match('{^\s*(\d+)\s*}', $output, $match)) {
  360. $datetime = new \DateTime('@'.$match[1], new \DateTimeZone('UTC'));
  361. }
  362. break;
  363. }
  364. }
  365. return $datetime ? $datetime->format(DATE_RFC3339) : null;
  366. }
  367. }