Locker.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. public function isFresh()
  47. {
  48. $lock = $this->lockFile->read();
  49. return $this->hash === $lock['hash'];
  50. }
  51. /**
  52. * Searches and returns an array of locked packages, retrieved from registered repositories.
  53. *
  54. * @return array
  55. */
  56. public function getLockedPackages()
  57. {
  58. if (!$this->isLocked()) {
  59. throw new \LogicException('No lockfile found. Unable to read locked packages');
  60. }
  61. $lockList = $this->lockFile->read();
  62. $packages = array();
  63. foreach ($lockList['packages'] as $info) {
  64. $package = $this->repositoryManager->getLocalRepository()->findPackage($info['package'], $info['version']);
  65. if (!$package) {
  66. $package = $this->repositoryManager->findPackage($info['package'], $info['version']);
  67. }
  68. if (!$package) {
  69. throw new \LogicException(sprintf(
  70. 'Can not find "%s-%s" package in registered repositories',
  71. $info['package'], $info['version']
  72. ));
  73. }
  74. $packages[] = $package;
  75. }
  76. return $packages;
  77. }
  78. /**
  79. * Locks provided packages into lockfile.
  80. *
  81. * @param array $packages array of packages
  82. */
  83. public function lockPackages(array $packages)
  84. {
  85. $lock = array(
  86. 'hash' => $this->hash,
  87. );
  88. foreach ($packages as $package) {
  89. $name = $package->getPrettyName();
  90. $version = $package->getPrettyVersion();
  91. if (!$name || !$version) {
  92. throw new \LogicException(sprintf(
  93. 'Package "%s" has no version or name and can not be locked', $package
  94. ));
  95. }
  96. $lock['packages'][] = array('package' => $name, 'version' => $version);
  97. }
  98. $this->lockFile->write($lock);
  99. }
  100. }