Locker.php 15 KB

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