Locker.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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. use Composer\Package\Version\VersionParser;
  21. use Composer\Util\Git as GitUtil;
  22. use Composer\IO\IOInterface;
  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. private $lockFile;
  32. private $repositoryManager;
  33. private $installationManager;
  34. private $hash;
  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 $hash unique hash of the current composer configuration
  47. */
  48. public function __construct(IOInterface $io, JsonFile $lockFile, RepositoryManager $repositoryManager, InstallationManager $installationManager, $hash)
  49. {
  50. $this->lockFile = $lockFile;
  51. $this->repositoryManager = $repositoryManager;
  52. $this->installationManager = $installationManager;
  53. $this->hash = $hash;
  54. $this->loader = new ArrayLoader(null, true);
  55. $this->dumper = new ArrayDumper();
  56. $this->process = new ProcessExecutor($io);
  57. }
  58. /**
  59. * Checks whether locker were been locked (lockfile found).
  60. *
  61. * @return bool
  62. */
  63. public function isLocked()
  64. {
  65. if (!$this->lockFile->exists()) {
  66. return false;
  67. }
  68. $data = $this->getLockData();
  69. return isset($data['packages']);
  70. }
  71. /**
  72. * Checks whether the lock file is still up to date with the current hash
  73. *
  74. * @return bool
  75. */
  76. public function isFresh()
  77. {
  78. $lock = $this->lockFile->read();
  79. return $this->hash === $lock['hash'];
  80. }
  81. /**
  82. * Searches and returns an array of locked packages, retrieved from registered repositories.
  83. *
  84. * @param bool $withDevReqs true to retrieve the locked dev packages
  85. * @throws \RuntimeException
  86. * @return \Composer\Repository\RepositoryInterface
  87. */
  88. public function getLockedRepository($withDevReqs = false)
  89. {
  90. $lockData = $this->getLockData();
  91. $packages = new ArrayRepository();
  92. $lockedPackages = $lockData['packages'];
  93. if ($withDevReqs) {
  94. if (isset($lockData['packages-dev'])) {
  95. $lockedPackages = array_merge($lockedPackages, $lockData['packages-dev']);
  96. } else {
  97. 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.');
  98. }
  99. }
  100. if (empty($lockedPackages)) {
  101. return $packages;
  102. }
  103. if (isset($lockedPackages[0]['name'])) {
  104. foreach ($lockedPackages as $info) {
  105. $packages->addPackage($this->loader->load($info));
  106. }
  107. return $packages;
  108. }
  109. 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.');
  110. }
  111. /**
  112. * Returns the platform requirements stored in the lock file
  113. *
  114. * @param bool $withDevReqs if true, the platform requirements from the require-dev block are also returned
  115. * @return \Composer\Package\Link[]
  116. */
  117. public function getPlatformRequirements($withDevReqs = false)
  118. {
  119. $lockData = $this->getLockData();
  120. $versionParser = new VersionParser();
  121. $requirements = array();
  122. if (!empty($lockData['platform'])) {
  123. $requirements = $versionParser->parseLinks(
  124. '__ROOT__',
  125. '1.0.0',
  126. 'requires',
  127. isset($lockData['platform']) ? $lockData['platform'] : array()
  128. );
  129. }
  130. if ($withDevReqs && !empty($lockData['platform-dev'])) {
  131. $devRequirements = $versionParser->parseLinks(
  132. '__ROOT__',
  133. '1.0.0',
  134. 'requires',
  135. isset($lockData['platform-dev']) ? $lockData['platform-dev'] : array()
  136. );
  137. $requirements = array_merge($requirements, $devRequirements);
  138. }
  139. return $requirements;
  140. }
  141. public function getMinimumStability()
  142. {
  143. $lockData = $this->getLockData();
  144. return isset($lockData['minimum-stability']) ? $lockData['minimum-stability'] : 'stable';
  145. }
  146. public function getStabilityFlags()
  147. {
  148. $lockData = $this->getLockData();
  149. return isset($lockData['stability-flags']) ? $lockData['stability-flags'] : array();
  150. }
  151. public function getAliases()
  152. {
  153. $lockData = $this->getLockData();
  154. return isset($lockData['aliases']) ? $lockData['aliases'] : array();
  155. }
  156. public function getLockData()
  157. {
  158. if (null !== $this->lockDataCache) {
  159. return $this->lockDataCache;
  160. }
  161. if (!$this->lockFile->exists()) {
  162. throw new \LogicException('No lockfile found. Unable to read locked packages');
  163. }
  164. return $this->lockDataCache = $this->lockFile->read();
  165. }
  166. /**
  167. * Locks provided data into lockfile.
  168. *
  169. * @param array $packages array of packages
  170. * @param mixed $devPackages array of dev packages or null if installed without --dev
  171. * @param array $platformReqs array of package name => constraint for required platform packages
  172. * @param mixed $platformDevReqs array of package name => constraint for dev-required platform packages
  173. * @param array $aliases array of aliases
  174. * @param string $minimumStability
  175. * @param array $stabilityFlags
  176. *
  177. * @return bool
  178. */
  179. public function setLockData(array $packages, $devPackages, array $platformReqs, $platformDevReqs, array $aliases, $minimumStability, array $stabilityFlags)
  180. {
  181. $lock = array(
  182. '_readme' => array('This file locks the dependencies of your project to a known state', 'Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file'),
  183. 'hash' => $this->hash,
  184. 'packages' => null,
  185. 'packages-dev' => null,
  186. 'aliases' => array(),
  187. 'minimum-stability' => $minimumStability,
  188. 'stability-flags' => $stabilityFlags,
  189. );
  190. foreach ($aliases as $package => $versions) {
  191. foreach ($versions as $version => $alias) {
  192. $lock['aliases'][] = array(
  193. 'alias' => $alias['alias'],
  194. 'alias_normalized' => $alias['alias_normalized'],
  195. 'version' => $version,
  196. 'package' => $package,
  197. );
  198. }
  199. }
  200. $lock['packages'] = $this->lockPackages($packages);
  201. if (null !== $devPackages) {
  202. $lock['packages-dev'] = $this->lockPackages($devPackages);
  203. }
  204. if (empty($lock['packages']) && empty($lock['packages-dev'])) {
  205. if ($this->lockFile->exists()) {
  206. unlink($this->lockFile->getPath());
  207. }
  208. return false;
  209. }
  210. $lock['platform'] = $platformReqs;
  211. $lock['platform-dev'] = $platformDevReqs;
  212. if (!$this->isLocked() || $lock !== $this->getLockData()) {
  213. $this->lockFile->write($lock);
  214. $this->lockDataCache = null;
  215. return true;
  216. }
  217. return false;
  218. }
  219. private function lockPackages(array $packages)
  220. {
  221. $locked = array();
  222. foreach ($packages as $package) {
  223. if ($package instanceof AliasPackage) {
  224. continue;
  225. }
  226. $name = $package->getPrettyName();
  227. $version = $package->getPrettyVersion();
  228. if (!$name || !$version) {
  229. throw new \LogicException(sprintf(
  230. 'Package "%s" has no version or name and can not be locked', $package
  231. ));
  232. }
  233. $spec = $this->dumper->dump($package);
  234. unset($spec['version_normalized']);
  235. // always move time to the end of the package definition
  236. $time = isset($spec['time']) ? $spec['time'] : null;
  237. unset($spec['time']);
  238. if ($package->isDev() && $package->getInstallationSource() === 'source') {
  239. // use the exact commit time of the current reference if it's a dev package
  240. $time = $this->getPackageTime($package) ?: $time;
  241. }
  242. if (null !== $time) {
  243. $spec['time'] = $time;
  244. }
  245. unset($spec['installation-source']);
  246. $locked[] = $spec;
  247. }
  248. usort($locked, function ($a, $b) {
  249. $comparison = strcmp($a['name'], $b['name']);
  250. if (0 !== $comparison) {
  251. return $comparison;
  252. }
  253. // If it is the same package, compare the versions to make the order deterministic
  254. return strcmp($a['version'], $b['version']);
  255. });
  256. return $locked;
  257. }
  258. /**
  259. * Returns the packages's datetime for its source reference.
  260. *
  261. * @param PackageInterface $package The package to scan.
  262. * @return string|null The formatted datetime or null if none was found.
  263. */
  264. private function getPackageTime(PackageInterface $package)
  265. {
  266. if (!function_exists('proc_open')) {
  267. return null;
  268. }
  269. $path = realpath($this->installationManager->getInstallPath($package));
  270. $sourceType = $package->getSourceType();
  271. $datetime = null;
  272. if ($path && in_array($sourceType, array('git', 'hg'))) {
  273. $sourceRef = $package->getSourceReference() ?: $package->getDistReference();
  274. switch ($sourceType) {
  275. case 'git':
  276. GitUtil::cleanEnv();
  277. if (0 === $this->process->execute('git log -n1 --pretty=%ct '.escapeshellarg($sourceRef), $output, $path) && preg_match('{^\s*\d+\s*$}', $output)) {
  278. $datetime = new \DateTime('@'.trim($output), new \DateTimeZone('UTC'));
  279. }
  280. break;
  281. case 'hg':
  282. if (0 === $this->process->execute('hg log --template "{date|hgdate}" -r '.escapeshellarg($sourceRef), $output, $path) && preg_match('{^\s*(\d+)\s*}', $output, $match)) {
  283. $datetime = new \DateTime('@'.$match[1], new \DateTimeZone('UTC'));
  284. }
  285. break;
  286. }
  287. }
  288. return $datetime ? $datetime->format('Y-m-d H:i:s') : null;
  289. }
  290. }