PageRenderTime 52ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/composer/composer/src/Composer/Package/Locker.php

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