2011-10-01 12:32:56 +00:00
|
|
|
<?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;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reads/writes project lockfile (composer.lock).
|
|
|
|
*
|
|
|
|
* @author Konstantin Kudryashiv <ever.zet@gmail.com>
|
|
|
|
*/
|
|
|
|
class Locker
|
|
|
|
{
|
|
|
|
private $lockFile;
|
|
|
|
private $repositoryManager;
|
2011-12-24 14:28:12 +00:00
|
|
|
private $hash;
|
2011-10-01 12:32:56 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes packages locker.
|
|
|
|
*
|
2012-01-06 12:26:25 +00:00
|
|
|
* @param JsonFile $lockFile lockfile loader
|
|
|
|
* @param RepositoryManager $repositoryManager repository manager instance
|
|
|
|
* @param string $hash unique hash of the current composer configuration
|
2011-10-01 12:32:56 +00:00
|
|
|
*/
|
2011-12-24 14:28:12 +00:00
|
|
|
public function __construct(JsonFile $lockFile, RepositoryManager $repositoryManager, $hash)
|
2011-10-01 12:32:56 +00:00
|
|
|
{
|
|
|
|
$this->lockFile = $lockFile;
|
|
|
|
$this->repositoryManager = $repositoryManager;
|
2011-12-24 14:28:12 +00:00
|
|
|
$this->hash = $hash;
|
2011-10-01 12:32:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks whether locker were been locked (lockfile found).
|
|
|
|
*
|
2012-01-06 12:26:25 +00:00
|
|
|
* @return Boolean
|
2011-10-01 12:32:56 +00:00
|
|
|
*/
|
|
|
|
public function isLocked()
|
|
|
|
{
|
|
|
|
return $this->lockFile->exists();
|
|
|
|
}
|
|
|
|
|
2012-01-06 12:26:25 +00:00
|
|
|
/**
|
|
|
|
* Checks whether the lock file is still up to date with the current hash
|
|
|
|
*
|
|
|
|
* @return Boolean
|
|
|
|
*/
|
2011-12-24 14:28:12 +00:00
|
|
|
public function isFresh()
|
|
|
|
{
|
|
|
|
$lock = $this->lockFile->read();
|
|
|
|
|
|
|
|
return $this->hash === $lock['hash'];
|
|
|
|
}
|
|
|
|
|
2011-10-01 12:32:56 +00:00
|
|
|
/**
|
|
|
|
* Searches and returns an array of locked packages, retrieved from registered repositories.
|
|
|
|
*
|
2012-01-06 12:26:25 +00:00
|
|
|
* @return array
|
2011-10-01 12:32:56 +00:00
|
|
|
*/
|
|
|
|
public function getLockedPackages()
|
|
|
|
{
|
2012-02-19 15:34:35 +00:00
|
|
|
$lockList = $this->getLockData();
|
2011-10-01 12:32:56 +00:00
|
|
|
$packages = array();
|
2011-12-24 14:28:12 +00:00
|
|
|
foreach ($lockList['packages'] as $info) {
|
2011-10-30 19:29:52 +00:00
|
|
|
$package = $this->repositoryManager->getLocalRepository()->findPackage($info['package'], $info['version']);
|
2011-10-01 12:32:56 +00:00
|
|
|
|
2011-10-30 21:56:10 +00:00
|
|
|
if (!$package) {
|
|
|
|
$package = $this->repositoryManager->findPackage($info['package'], $info['version']);
|
|
|
|
}
|
|
|
|
|
2011-10-01 12:32:56 +00:00
|
|
|
if (!$package) {
|
|
|
|
throw new \LogicException(sprintf(
|
|
|
|
'Can not find "%s-%s" package in registered repositories',
|
|
|
|
$info['package'], $info['version']
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
$packages[] = $package;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $packages;
|
|
|
|
}
|
|
|
|
|
2012-02-19 15:34:35 +00:00
|
|
|
public function getLockData()
|
|
|
|
{
|
|
|
|
if (!$this->isLocked()) {
|
|
|
|
throw new \LogicException('No lockfile found. Unable to read locked packages');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->lockFile->read();
|
|
|
|
}
|
|
|
|
|
2011-10-01 12:32:56 +00:00
|
|
|
/**
|
|
|
|
* Locks provided packages into lockfile.
|
|
|
|
*
|
2012-01-06 12:26:25 +00:00
|
|
|
* @param array $packages array of packages
|
2011-10-01 12:32:56 +00:00
|
|
|
*/
|
|
|
|
public function lockPackages(array $packages)
|
|
|
|
{
|
2011-12-24 14:28:12 +00:00
|
|
|
$lock = array(
|
|
|
|
'hash' => $this->hash,
|
2012-01-06 12:27:16 +00:00
|
|
|
'packages' => array(),
|
2011-12-24 14:28:12 +00:00
|
|
|
);
|
2011-10-01 12:32:56 +00:00
|
|
|
foreach ($packages as $package) {
|
2011-11-20 14:42:12 +00:00
|
|
|
$name = $package->getPrettyName();
|
|
|
|
$version = $package->getPrettyVersion();
|
2011-10-01 12:32:56 +00:00
|
|
|
|
|
|
|
if (!$name || !$version) {
|
|
|
|
throw new \LogicException(sprintf(
|
|
|
|
'Package "%s" has no version or name and can not be locked', $package
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2012-02-18 22:48:12 +00:00
|
|
|
$spec = array('package' => $name, 'version' => $version);
|
|
|
|
|
|
|
|
if ($package->isDev()) {
|
2012-02-19 14:20:03 +00:00
|
|
|
$spec['source-reference'] = $package->getSourceReference();
|
2012-02-18 22:48:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$lock['packages'][] = $spec;
|
2011-10-01 12:32:56 +00:00
|
|
|
}
|
|
|
|
|
2011-12-24 14:28:12 +00:00
|
|
|
$this->lockFile->write($lock);
|
2011-10-01 12:32:56 +00:00
|
|
|
}
|
|
|
|
}
|