123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- <?php
- /*
- * This file is part of Composer.
- *
- * (c) Nils Adermann <naderman@naderman.de>
- * Jordi Boggiano <j.boggiano@seld.be>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Composer\Package;
- use Composer\Json\JsonFile;
- use Composer\Repository\RepositoryManager;
- use Composer\Package\AliasPackage;
- /**
- * Reads/writes project lockfile (composer.lock).
- *
- * @author Konstantin Kudryashiv <ever.zet@gmail.com>
- */
- class Locker
- {
- private $lockFile;
- private $repositoryManager;
- private $hash;
- private $lockDataCache;
- /**
- * Initializes packages locker.
- *
- * @param JsonFile $lockFile lockfile loader
- * @param RepositoryManager $repositoryManager repository manager instance
- * @param string $hash unique hash of the current composer configuration
- */
- public function __construct(JsonFile $lockFile, RepositoryManager $repositoryManager, $hash)
- {
- $this->lockFile = $lockFile;
- $this->repositoryManager = $repositoryManager;
- $this->hash = $hash;
- }
- /**
- * Checks whether locker were been locked (lockfile found).
- *
- * @return Boolean
- */
- public function isLocked()
- {
- return $this->lockFile->exists();
- }
- /**
- * Checks whether the lock file is still up to date with the current hash
- *
- * @return Boolean
- */
- public function isFresh()
- {
- $lock = $this->lockFile->read();
- return $this->hash === $lock['hash'];
- }
- /**
- * Searches and returns an array of locked packages, retrieved from registered repositories.
- *
- * @return array
- */
- public function getLockedPackages($dev = false)
- {
- $lockList = $this->getLockData();
- $packages = array();
- $lockedPackages = $dev ? $lockList['packages-dev'] : $lockList['packages'];
- $repo = $dev ? $this->repositoryManager->getLocalDevRepository() : $this->repositoryManager->getLocalRepository();
- foreach ($lockedPackages as $info) {
- $resolvedVersion = !empty($info['alias']) ? $info['alias'] : $info['version'];
- $package = $repo->findPackage($info['package'], $resolvedVersion);
- if (!$package) {
- $package = $this->repositoryManager->findPackage($info['package'], $info['version']);
- if ($package && !empty($info['alias'])) {
- $package = new AliasPackage($package, $info['alias'], $info['alias']);
- }
- }
- if (!$package) {
- throw new \LogicException(sprintf(
- 'Can not find "%s-%s" package in registered repositories',
- $info['package'], $info['version']
- ));
- }
- $packages[] = $package;
- }
- return $packages;
- }
- public function getAliases()
- {
- $lockList = $this->getLockData();
- return isset($lockList['aliases']) ? $lockList['aliases'] : array();
- }
- public function getLockData()
- {
- if (!$this->isLocked()) {
- throw new \LogicException('No lockfile found. Unable to read locked packages');
- }
- if (null !== $this->lockDataCache) {
- return $this->lockDataCache;
- }
- return $this->lockDataCache = $this->lockFile->read();
- }
- /**
- * Locks provided data into lockfile.
- *
- * @param array $packages array of packages
- * @param array $packages array of dev packages
- * @param array $aliases array of aliases
- *
- * @return Boolean
- */
- public function setLockData(array $packages, array $devPackages, array $aliases)
- {
- $lock = array(
- 'hash' => $this->hash,
- 'packages' => array(),
- 'packages-dev' => array(),
- 'aliases' => $aliases,
- );
- $lock['packages'] = $this->lockPackages($packages);
- $lock['packages-dev'] = $this->lockPackages($devPackages);
- if (!$this->isLocked() || $lock !== $this->getLockData()) {
- $this->lockFile->write($lock);
- $this->lockDataCache = null;
- return true;
- }
- return false;
- }
- private function lockPackages(array $packages)
- {
- $locked = array();
- foreach ($packages as $package) {
- $name = $package->getPrettyName();
- $version = $package->getPrettyVersion();
- if (!$name || !$version) {
- throw new \LogicException(sprintf(
- 'Package "%s" has no version or name and can not be locked', $package
- ));
- }
- $spec = array('package' => $name, 'version' => $version);
- if ($package->isDev()) {
- $spec['source-reference'] = $package->getSourceReference();
- }
- if ($package->getAlias() && $package->isInstalledAsAlias()) {
- $spec['alias'] = $package->getAlias();
- }
- $locked[] = $spec;
- }
- usort($locked, function ($a, $b) {
- return strcmp($a['package'], $b['package']);
- });
- return $locked;
- }
- }
|