1
0
Fork 0
composer/src/Composer/Package/Locker.php

243 lines
7.2 KiB
PHP
Raw Normal View History

<?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;
2012-04-02 19:46:28 +00:00
private $lockDataCache;
/**
* Initializes packages locker.
*
2012-05-22 10:07:08 +00:00
* @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).
*
2012-05-22 10:07:08 +00:00
* @param Boolean $dev true to check if dev packages are locked
2012-01-06 12:26:25 +00:00
* @return Boolean
*/
2012-04-15 17:05:50 +00:00
public function isLocked($dev = false)
{
2012-04-15 17:05:50 +00:00
if (!$this->lockFile->exists()) {
return false;
}
$data = $this->getLockData();
if ($dev) {
return isset($data['packages-dev']);
}
return isset($data['packages']);
}
2012-01-06 12:26:25 +00:00
/**
* 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.
*
2012-05-22 10:07:08 +00:00
* @param Boolean $dev true to retrieve the locked dev packages
2012-01-06 12:26:25 +00:00
* @return array
*/
2012-04-14 13:45:25 +00:00
public function getLockedPackages($dev = false)
{
$lockData = $this->getLockData();
$packages = array();
2012-04-14 13:45:25 +00:00
$lockedPackages = $dev ? $lockData['packages-dev'] : $lockData['packages'];
2012-04-14 13:45:25 +00:00
$repo = $dev ? $this->repositoryManager->getLocalDevRepository() : $this->repositoryManager->getLocalRepository();
foreach ($lockedPackages as $info) {
2012-05-09 15:31:27 +00:00
// TODO BC remove this after June 10th
if (isset($info['alias']) && empty($warned)) {
$warned = true;
echo 'BC warning: your lock file appears to be of an older format than this composer version, it is recommended to run composer update'.PHP_EOL;
}
$resolvedVersion = !empty($info['alias-version']) ? $info['alias-version'] : $info['version'];
2012-04-27 16:42:34 +00:00
// try to find the package in the local repo (best match)
2012-04-14 13:45:25 +00:00
$package = $repo->findPackage($info['package'], $resolvedVersion);
2012-04-27 16:42:34 +00:00
// try to find the package in any repo
2011-10-30 21:56:10 +00:00
if (!$package) {
2012-04-27 16:42:34 +00:00
$package = $this->repositoryManager->findPackage($info['package'], $resolvedVersion);
}
// try to find the package in any repo (second pass without alias + rebuild alias since it disappeared)
if (!$package && !empty($info['alias-version'])) {
2011-10-30 21:56:10 +00:00
$package = $this->repositoryManager->findPackage($info['package'], $info['version']);
2012-04-27 16:42:34 +00:00
if ($package) {
$alias = new AliasPackage($package, $info['alias-version'], $info['alias-pretty-version']);
$package->getRepository()->addPackage($alias);
$package = $alias;
}
2011-10-30 21:56:10 +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;
}
public function getMinimumStability()
{
$lockData = $this->getLockData();
// TODO BC change dev to stable end of june?
return isset($lockData['minimum-stability']) ? $lockData['minimum-stability'] : 'dev';
}
public function getStabilityFlags()
{
$lockData = $this->getLockData();
return isset($lockData['stability-flags']) ? $lockData['stability-flags'] : array();
}
public function getAliases()
{
$lockData = $this->getLockData();
return isset($lockData['aliases']) ? $lockData['aliases'] : array();
}
public function getLockData()
{
2012-04-02 19:46:28 +00:00
if (null !== $this->lockDataCache) {
return $this->lockDataCache;
}
if (!$this->lockFile->exists()) {
throw new \LogicException('No lockfile found. Unable to read locked packages');
}
2012-04-02 19:46:28 +00:00
return $this->lockDataCache = $this->lockFile->read();
}
/**
* Locks provided data into lockfile.
*
2012-01-06 12:26:25 +00:00
* @param array $packages array of packages
2012-04-15 17:05:50 +00:00
* @param mixed $packages array of dev packages or null if installed without --dev
2012-05-22 10:07:08 +00:00
* @param array $aliases array of aliases
*
* @return Boolean
*/
public function setLockData(array $packages, $devPackages, array $aliases, $minimumStability, array $stabilityFlags)
{
$lock = array(
'hash' => $this->hash,
2012-04-15 17:05:50 +00:00
'packages' => null,
'packages-dev' => null,
'aliases' => $aliases,
'minimum-stability' => $minimumStability,
'stability-flags' => $stabilityFlags,
);
2012-04-14 13:45:25 +00:00
$lock['packages'] = $this->lockPackages($packages);
2012-04-15 17:05:50 +00:00
if (null !== $devPackages) {
$lock['packages-dev'] = $this->lockPackages($devPackages);
}
2012-04-14 13:45:25 +00:00
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) {
$alias = null;
2012-04-15 17:12:00 +00:00
if ($package instanceof AliasPackage) {
$alias = $package;
2012-04-15 17:12:00 +00:00
$package = $package->getAliasOf();
}
$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
));
}
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
}
if ($alias) {
$spec['alias-pretty-version'] = $alias->getPrettyVersion();
$spec['alias-version'] = $alias->getVersion();
}
2012-02-18 22:48:12 +00:00
2012-04-14 13:45:25 +00:00
$locked[] = $spec;
}
2012-04-14 13:45:25 +00:00
usort($locked, function ($a, $b) {
return strcmp($a['package'], $b['package']);
});
2012-04-14 13:45:25 +00:00
return $locked;
}
}