Locker.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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\Repository\RepositoryManager;
  14. /**
  15. * Reads/writes project lockfile (composer.lock).
  16. *
  17. * @author Konstantin Kudryashiv <ever.zet@gmail.com>
  18. */
  19. class Locker
  20. {
  21. private $lockFile;
  22. private $repositoryManager;
  23. private $hash;
  24. /**
  25. * Initializes packages locker.
  26. *
  27. * @param JsonFile $lockFile lockfile loader
  28. * @param RepositoryManager $repositoryManager repository manager instance
  29. * @param string $hash unique hash of the current composer configuration
  30. */
  31. public function __construct(JsonFile $lockFile, RepositoryManager $repositoryManager, $hash)
  32. {
  33. $this->lockFile = $lockFile;
  34. $this->repositoryManager = $repositoryManager;
  35. $this->hash = $hash;
  36. }
  37. /**
  38. * Checks whether locker were been locked (lockfile found).
  39. *
  40. * @return Boolean
  41. */
  42. public function isLocked()
  43. {
  44. return $this->lockFile->exists();
  45. }
  46. /**
  47. * Checks whether the lock file is still up to date with the current hash
  48. *
  49. * @return Boolean
  50. */
  51. public function isFresh()
  52. {
  53. $lock = $this->lockFile->read();
  54. return $this->hash === $lock['hash'];
  55. }
  56. /**
  57. * Searches and returns an array of locked packages, retrieved from registered repositories.
  58. *
  59. * @return array
  60. */
  61. public function getLockedPackages()
  62. {
  63. if (!$this->isLocked()) {
  64. throw new \LogicException('No lockfile found. Unable to read locked packages');
  65. }
  66. $lockList = $this->lockFile->read();
  67. $packages = array();
  68. foreach ($lockList['packages'] as $info) {
  69. $package = $this->repositoryManager->getLocalRepository()->findPackage($info['package'], $info['version']);
  70. if (!$package) {
  71. $package = $this->repositoryManager->findPackage($info['package'], $info['version']);
  72. }
  73. if (!$package) {
  74. throw new \LogicException(sprintf(
  75. 'Can not find "%s-%s" package in registered repositories',
  76. $info['package'], $info['version']
  77. ));
  78. }
  79. $packages[] = $package;
  80. }
  81. return $packages;
  82. }
  83. /**
  84. * Locks provided packages into lockfile.
  85. *
  86. * @param array $packages array of packages
  87. */
  88. public function lockPackages(array $packages)
  89. {
  90. $lock = array(
  91. 'hash' => $this->hash,
  92. );
  93. foreach ($packages as $package) {
  94. $name = $package->getPrettyName();
  95. $version = $package->getPrettyVersion();
  96. if (!$name || !$version) {
  97. throw new \LogicException(sprintf(
  98. 'Package "%s" has no version or name and can not be locked', $package
  99. ));
  100. }
  101. $lock['packages'][] = array('package' => $name, 'version' => $version);
  102. }
  103. $this->lockFile->write($lock);
  104. }
  105. }