2012-03-03 05:35:40 +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;
|
|
|
|
|
|
|
|
use Composer\Autoload\AutoloadGenerator;
|
|
|
|
use Composer\DependencyResolver\DefaultPolicy;
|
|
|
|
use Composer\DependencyResolver\Operation\UpdateOperation;
|
|
|
|
use Composer\DependencyResolver\Pool;
|
|
|
|
use Composer\DependencyResolver\Request;
|
|
|
|
use Composer\DependencyResolver\Solver;
|
2012-03-05 23:48:07 +00:00
|
|
|
use Composer\Downloader\DownloadManager;
|
|
|
|
use Composer\Installer\InstallationManager;
|
2012-03-03 05:35:40 +00:00
|
|
|
use Composer\IO\IOInterface;
|
|
|
|
use Composer\Package\AliasPackage;
|
|
|
|
use Composer\Package\Link;
|
|
|
|
use Composer\Package\LinkConstraint\VersionConstraint;
|
2012-03-05 23:48:07 +00:00
|
|
|
use Composer\Package\Locker;
|
2012-03-03 05:35:40 +00:00
|
|
|
use Composer\Package\PackageInterface;
|
|
|
|
use Composer\Repository\CompositeRepository;
|
|
|
|
use Composer\Repository\PlatformRepository;
|
|
|
|
use Composer\Repository\RepositoryInterface;
|
2012-03-05 23:48:07 +00:00
|
|
|
use Composer\Repository\RepositoryManager;
|
2012-03-03 05:35:40 +00:00
|
|
|
use Composer\Script\EventDispatcher;
|
|
|
|
use Composer\Script\ScriptEvents;
|
|
|
|
|
2012-03-10 00:16:37 +00:00
|
|
|
/**
|
|
|
|
* @author Jordi Boggiano <j.boggiano@seld.be>
|
|
|
|
* @author Beau Simensen <beau@dflydev.com>
|
|
|
|
* @author Konstantin Kudryashov <ever.zet@gmail.com>
|
|
|
|
*/
|
2012-03-06 23:30:18 +00:00
|
|
|
class Installer
|
2012-03-03 05:35:40 +00:00
|
|
|
{
|
|
|
|
/**
|
2012-03-05 23:48:07 +00:00
|
|
|
* @var IOInterface
|
|
|
|
*/
|
|
|
|
protected $io;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var PackageInterface
|
|
|
|
*/
|
|
|
|
protected $package;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var DownloadManager
|
|
|
|
*/
|
|
|
|
protected $downloadManager;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var RepositoryManager
|
|
|
|
*/
|
|
|
|
protected $repositoryManager;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var Locker
|
|
|
|
*/
|
|
|
|
protected $locker;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var InstallationManager
|
|
|
|
*/
|
|
|
|
protected $installationManager;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var EventDispatcher
|
|
|
|
*/
|
|
|
|
protected $eventDispatcher;
|
|
|
|
|
2012-03-10 18:56:15 +00:00
|
|
|
protected $preferSource = false;
|
|
|
|
protected $dryRun = false;
|
|
|
|
protected $verbose = false;
|
|
|
|
protected $installRecommends = true;
|
|
|
|
protected $installSuggests = false;
|
|
|
|
protected $update = false;
|
2012-03-10 17:08:36 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var RepositoryInterface
|
|
|
|
*/
|
|
|
|
protected $additionalInstalledRepository;
|
|
|
|
|
2012-03-05 23:48:07 +00:00
|
|
|
/**
|
|
|
|
* Constructor
|
2012-03-10 00:16:37 +00:00
|
|
|
*
|
2012-03-03 05:35:40 +00:00
|
|
|
* @param IOInterface $io
|
2012-03-05 23:48:07 +00:00
|
|
|
* @param PackageInterface $package
|
|
|
|
* @param DownloadManager $downloadManager
|
|
|
|
* @param RepositoryManager $repositoryManager
|
|
|
|
* @param Locker $locker
|
|
|
|
* @param InstallationManager $installationManager
|
2012-03-03 05:35:40 +00:00
|
|
|
* @param EventDispatcher $eventDispatcher
|
2012-03-05 23:48:07 +00:00
|
|
|
*/
|
|
|
|
public function __construct(IOInterface $io, PackageInterface $package, DownloadManager $downloadManager, RepositoryManager $repositoryManager, Locker $locker, InstallationManager $installationManager, EventDispatcher $eventDispatcher)
|
|
|
|
{
|
|
|
|
$this->io = $io;
|
|
|
|
$this->package = $package;
|
|
|
|
$this->downloadManager = $downloadManager;
|
|
|
|
$this->repositoryManager = $repositoryManager;
|
|
|
|
$this->locker = $locker;
|
|
|
|
$this->installationManager = $installationManager;
|
|
|
|
$this->eventDispatcher = $eventDispatcher;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Run installation (or update)
|
2012-03-03 05:35:40 +00:00
|
|
|
*/
|
2012-03-10 17:08:36 +00:00
|
|
|
public function run()
|
2012-03-03 05:35:40 +00:00
|
|
|
{
|
2012-03-10 17:08:36 +00:00
|
|
|
if ($this->dryRun) {
|
|
|
|
$this->verbose = true;
|
2012-03-03 05:35:40 +00:00
|
|
|
}
|
|
|
|
|
2012-03-10 17:08:36 +00:00
|
|
|
if ($this->preferSource) {
|
2012-03-05 23:48:07 +00:00
|
|
|
$this->downloadManager->setPreferSource(true);
|
2012-03-03 05:35:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// create local repo, this contains all packages that are installed in the local project
|
2012-03-06 04:28:37 +00:00
|
|
|
$localRepo = $this->repositoryManager->getLocalRepository();
|
2012-03-03 05:35:40 +00:00
|
|
|
// create installed repo, this contains all local packages + platform packages (php & extensions)
|
|
|
|
$installedRepo = new CompositeRepository(array($localRepo, new PlatformRepository()));
|
2012-03-10 17:08:36 +00:00
|
|
|
if ($this->additionalInstalledRepository) {
|
|
|
|
$installedRepo->addRepository($this->additionalInstalledRepository);
|
2012-03-03 05:35:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// prepare aliased packages
|
2012-03-10 17:08:36 +00:00
|
|
|
if (!$this->update && $this->locker->isLocked()) {
|
2012-03-05 23:48:07 +00:00
|
|
|
$aliases = $this->locker->getAliases();
|
2012-03-03 05:35:40 +00:00
|
|
|
} else {
|
2012-03-05 23:48:07 +00:00
|
|
|
$aliases = $this->package->getAliases();
|
2012-03-03 05:35:40 +00:00
|
|
|
}
|
|
|
|
foreach ($aliases as $alias) {
|
2012-03-06 04:28:37 +00:00
|
|
|
foreach ($this->repositoryManager->findPackages($alias['package'], $alias['version']) as $package) {
|
2012-03-03 05:35:40 +00:00
|
|
|
$package->getRepository()->addPackage(new AliasPackage($package, $alias['alias_normalized'], $alias['alias']));
|
|
|
|
}
|
2012-03-06 04:28:37 +00:00
|
|
|
foreach ($this->repositoryManager->getLocalRepository()->findPackages($alias['package'], $alias['version']) as $package) {
|
|
|
|
$this->repositoryManager->getLocalRepository()->addPackage(new AliasPackage($package, $alias['alias_normalized'], $alias['alias']));
|
|
|
|
$this->repositoryManager->getLocalRepository()->removePackage($package);
|
2012-03-03 05:35:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// creating repository pool
|
|
|
|
$pool = new Pool;
|
|
|
|
$pool->addRepository($installedRepo);
|
2012-03-06 04:28:37 +00:00
|
|
|
foreach ($this->repositoryManager->getRepositories() as $repository) {
|
2012-03-03 05:35:40 +00:00
|
|
|
$pool->addRepository($repository);
|
|
|
|
}
|
|
|
|
|
|
|
|
// dispatch pre event
|
2012-03-10 17:08:36 +00:00
|
|
|
if (!$this->dryRun) {
|
|
|
|
$eventName = $this->update ? ScriptEvents::PRE_UPDATE_CMD : ScriptEvents::PRE_INSTALL_CMD;
|
2012-03-05 23:48:07 +00:00
|
|
|
$this->eventDispatcher->dispatchCommandEvent($eventName);
|
2012-03-03 05:35:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// creating requirements request
|
|
|
|
$installFromLock = false;
|
|
|
|
$request = new Request($pool);
|
2012-03-10 17:08:36 +00:00
|
|
|
if ($this->update) {
|
2012-03-05 23:48:07 +00:00
|
|
|
$this->io->write('<info>Updating dependencies</info>');
|
2012-03-03 05:35:40 +00:00
|
|
|
|
|
|
|
$request->updateAll();
|
|
|
|
|
2012-03-10 17:08:36 +00:00
|
|
|
$links = $this->collectLinks();
|
2012-03-03 05:35:40 +00:00
|
|
|
|
|
|
|
foreach ($links as $link) {
|
|
|
|
$request->install($link->getTarget(), $link->getConstraint());
|
|
|
|
}
|
2012-03-05 23:48:07 +00:00
|
|
|
} elseif ($this->locker->isLocked()) {
|
2012-03-03 05:35:40 +00:00
|
|
|
$installFromLock = true;
|
2012-03-05 23:48:07 +00:00
|
|
|
$this->io->write('<info>Installing from lock file</info>');
|
2012-03-03 05:35:40 +00:00
|
|
|
|
2012-03-05 23:48:07 +00:00
|
|
|
if (!$this->locker->isFresh()) {
|
|
|
|
$this->io->write('<warning>Your lock file is out of sync with your composer.json, run "composer.phar update" to update dependencies</warning>');
|
2012-03-03 05:35:40 +00:00
|
|
|
}
|
|
|
|
|
2012-03-05 23:48:07 +00:00
|
|
|
foreach ($this->locker->getLockedPackages() as $package) {
|
2012-03-03 05:35:40 +00:00
|
|
|
$version = $package->getVersion();
|
|
|
|
foreach ($aliases as $alias) {
|
|
|
|
if ($alias['package'] === $package->getName() && $alias['version'] === $package->getVersion()) {
|
|
|
|
$version = $alias['alias'];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$constraint = new VersionConstraint('=', $version);
|
|
|
|
$request->install($package->getName(), $constraint);
|
|
|
|
}
|
|
|
|
} else {
|
2012-03-05 23:48:07 +00:00
|
|
|
$this->io->write('<info>Installing dependencies</info>');
|
2012-03-03 05:35:40 +00:00
|
|
|
|
2012-03-10 17:08:36 +00:00
|
|
|
$links = $this->collectLinks();
|
2012-03-03 05:35:40 +00:00
|
|
|
|
|
|
|
foreach ($links as $link) {
|
|
|
|
$request->install($link->getTarget(), $link->getConstraint());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// prepare solver
|
2012-03-10 00:16:37 +00:00
|
|
|
$policy = new DefaultPolicy();
|
|
|
|
$solver = new Solver($policy, $pool, $installedRepo);
|
2012-03-03 05:35:40 +00:00
|
|
|
|
|
|
|
// solve dependencies
|
|
|
|
$operations = $solver->solve($request);
|
|
|
|
|
|
|
|
// force dev packages to be updated to latest reference on update
|
2012-03-10 17:08:36 +00:00
|
|
|
if ($this->update) {
|
2012-03-03 05:35:40 +00:00
|
|
|
foreach ($localRepo->getPackages() as $package) {
|
|
|
|
if ($package instanceof AliasPackage) {
|
|
|
|
$package = $package->getAliasOf();
|
|
|
|
}
|
|
|
|
|
|
|
|
// skip non-dev packages
|
|
|
|
if (!$package->isDev()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// skip packages that will be updated/uninstalled
|
|
|
|
foreach ($operations as $operation) {
|
|
|
|
if (('update' === $operation->getJobType() && $package === $operation->getInitialPackage())
|
|
|
|
|| ('uninstall' === $operation->getJobType() && $package === $operation->getPackage())
|
|
|
|
) {
|
|
|
|
continue 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// force update
|
2012-03-06 04:28:37 +00:00
|
|
|
$newPackage = $this->repositoryManager->findPackage($package->getName(), $package->getVersion());
|
2012-03-03 05:35:40 +00:00
|
|
|
if ($newPackage && $newPackage->getSourceReference() !== $package->getSourceReference()) {
|
|
|
|
$operations[] = new UpdateOperation($package, $newPackage);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// anti-alias local repository to allow updates to work fine
|
2012-03-06 04:28:37 +00:00
|
|
|
foreach ($this->repositoryManager->getLocalRepository()->getPackages() as $package) {
|
2012-03-03 05:35:40 +00:00
|
|
|
if ($package instanceof AliasPackage) {
|
2012-03-06 04:28:37 +00:00
|
|
|
$this->repositoryManager->getLocalRepository()->addPackage(clone $package->getAliasOf());
|
|
|
|
$this->repositoryManager->getLocalRepository()->removePackage($package);
|
2012-03-03 05:35:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// execute operations
|
|
|
|
if (!$operations) {
|
2012-03-05 23:48:07 +00:00
|
|
|
$this->io->write('<info>Nothing to install/update</info>');
|
2012-03-03 05:35:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($operations as $operation) {
|
2012-03-10 17:08:36 +00:00
|
|
|
if ($this->verbose) {
|
2012-03-05 23:48:07 +00:00
|
|
|
$this->io->write((string) $operation);
|
2012-03-03 05:35:40 +00:00
|
|
|
}
|
2012-03-10 17:08:36 +00:00
|
|
|
if (!$this->dryRun) {
|
2012-03-05 23:48:07 +00:00
|
|
|
$this->eventDispatcher->dispatchPackageEvent(constant('Composer\Script\ScriptEvents::PRE_PACKAGE_'.strtoupper($operation->getJobType())), $operation);
|
2012-03-03 05:35:40 +00:00
|
|
|
|
|
|
|
// if installing from lock, restore dev packages' references to their locked state
|
|
|
|
if ($installFromLock) {
|
|
|
|
$package = null;
|
|
|
|
if ('update' === $operation->getJobType()) {
|
|
|
|
$package = $operation->getTargetPackage();
|
|
|
|
} elseif ('install' === $operation->getJobType()) {
|
|
|
|
$package = $operation->getPackage();
|
|
|
|
}
|
|
|
|
if ($package && $package->isDev()) {
|
2012-03-05 23:48:07 +00:00
|
|
|
$lockData = $this->locker->getLockData();
|
2012-03-03 05:35:40 +00:00
|
|
|
foreach ($lockData['packages'] as $lockedPackage) {
|
|
|
|
if (!empty($lockedPackage['source-reference']) && strtolower($lockedPackage['package']) === $package->getName()) {
|
|
|
|
$package->setSourceReference($lockedPackage['source-reference']);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-03-05 23:48:07 +00:00
|
|
|
$this->installationManager->execute($operation);
|
2012-03-03 05:35:40 +00:00
|
|
|
|
2012-03-05 23:48:07 +00:00
|
|
|
$this->eventDispatcher->dispatchPackageEvent(constant('Composer\Script\ScriptEvents::POST_PACKAGE_'.strtoupper($operation->getJobType())), $operation);
|
2012-03-03 05:35:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-10 17:08:36 +00:00
|
|
|
if (!$this->dryRun) {
|
|
|
|
if ($this->update || !$this->locker->isLocked()) {
|
2012-03-05 23:48:07 +00:00
|
|
|
$this->locker->setLockData($localRepo->getPackages(), $aliases);
|
|
|
|
$this->io->write('<info>Writing lock file</info>');
|
2012-03-03 05:35:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$localRepo->write();
|
|
|
|
|
2012-03-05 23:48:07 +00:00
|
|
|
$this->io->write('<info>Generating autoload files</info>');
|
2012-03-03 05:35:40 +00:00
|
|
|
$generator = new AutoloadGenerator;
|
2012-03-05 23:48:07 +00:00
|
|
|
$generator->dump($localRepo, $this->package, $this->installationManager, $this->installationManager->getVendorPath().'/.composer');
|
2012-03-03 05:35:40 +00:00
|
|
|
|
|
|
|
// dispatch post event
|
2012-03-10 17:08:36 +00:00
|
|
|
$eventName = $this->update ? ScriptEvents::POST_UPDATE_CMD : ScriptEvents::POST_INSTALL_CMD;
|
2012-03-05 23:48:07 +00:00
|
|
|
$this->eventDispatcher->dispatchCommandEvent($eventName);
|
2012-03-03 05:35:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-10 17:08:36 +00:00
|
|
|
private function collectLinks()
|
2012-03-03 05:35:40 +00:00
|
|
|
{
|
2012-03-05 23:48:07 +00:00
|
|
|
$links = $this->package->getRequires();
|
2012-03-03 05:35:40 +00:00
|
|
|
|
2012-03-10 17:08:36 +00:00
|
|
|
if ($this->installRecommends) {
|
2012-03-05 23:48:07 +00:00
|
|
|
$links = array_merge($links, $this->package->getRecommends());
|
2012-03-03 05:35:40 +00:00
|
|
|
}
|
|
|
|
|
2012-03-10 17:08:36 +00:00
|
|
|
if ($this->installSuggests) {
|
2012-03-05 23:48:07 +00:00
|
|
|
$links = array_merge($links, $this->package->getSuggests());
|
2012-03-03 05:35:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $links;
|
|
|
|
}
|
2012-03-05 23:48:07 +00:00
|
|
|
|
|
|
|
/**
|
2012-03-06 23:30:18 +00:00
|
|
|
* Create Installer
|
2012-03-10 00:16:37 +00:00
|
|
|
*
|
2012-03-05 23:48:07 +00:00
|
|
|
* @param IOInterface $io
|
|
|
|
* @param Composer $composer
|
|
|
|
* @param EventDispatcher $eventDispatcher
|
2012-03-06 23:30:18 +00:00
|
|
|
* @return Installer
|
2012-03-05 23:48:07 +00:00
|
|
|
*/
|
2012-03-10 00:16:37 +00:00
|
|
|
static public function create(IOInterface $io, Composer $composer, EventDispatcher $eventDispatcher = null)
|
2012-03-05 23:48:07 +00:00
|
|
|
{
|
2012-03-10 00:16:37 +00:00
|
|
|
$eventDispatcher = $eventDispatcher ?: new EventDispatcher($composer, $io);
|
|
|
|
|
2012-03-05 23:48:07 +00:00
|
|
|
return new static(
|
|
|
|
$io,
|
|
|
|
$composer->getPackage(),
|
|
|
|
$composer->getDownloadManager(),
|
|
|
|
$composer->getRepositoryManager(),
|
|
|
|
$composer->getLocker(),
|
|
|
|
$composer->getInstallationManager(),
|
|
|
|
$eventDispatcher
|
|
|
|
);
|
|
|
|
}
|
2012-03-10 17:08:36 +00:00
|
|
|
|
|
|
|
public function setAdditionalInstalledRepository(RepositoryInterface $additionalInstalledRepository)
|
|
|
|
{
|
|
|
|
$this->additionalInstalledRepository = $additionalInstalledRepository;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* wether to run in drymode or not
|
|
|
|
*
|
|
|
|
* @param boolean $dryRun
|
|
|
|
* @return Installer
|
|
|
|
*/
|
|
|
|
public function setDryRun($dryRun=true)
|
|
|
|
{
|
|
|
|
$this->dryRun = (boolean)$dryRun;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-03-10 18:56:15 +00:00
|
|
|
* install recommend packages
|
2012-03-10 17:08:36 +00:00
|
|
|
*
|
2012-03-10 18:56:15 +00:00
|
|
|
* @param boolean $noInstallRecommends
|
2012-03-10 17:08:36 +00:00
|
|
|
* @return Installer
|
|
|
|
*/
|
|
|
|
public function setInstallRecommends($installRecommends=true)
|
|
|
|
{
|
|
|
|
$this->installRecommends = (boolean)$installRecommends;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* also install suggested packages
|
|
|
|
*
|
|
|
|
* @param boolean $installSuggests
|
|
|
|
* @return Installer
|
|
|
|
*/
|
|
|
|
public function setInstallSuggests($installSuggests=true)
|
|
|
|
{
|
|
|
|
$this->installSuggests = (boolean)$installSuggests;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* prefer source installation
|
|
|
|
*
|
|
|
|
* @param boolean $preferSource
|
|
|
|
* @return Installer
|
|
|
|
*/
|
|
|
|
public function setPreferSource($preferSource=true)
|
|
|
|
{
|
|
|
|
$this->preferSource = (boolean)$preferSource;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* update packages
|
|
|
|
*
|
|
|
|
* @param boolean $update
|
|
|
|
* @return Installer
|
|
|
|
*/
|
|
|
|
public function setUpdate($update=true)
|
|
|
|
{
|
|
|
|
$this->update = (boolean)$update;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* run in verbose mode
|
|
|
|
*
|
|
|
|
* @param boolean $verbose
|
|
|
|
* @return Installer
|
|
|
|
*/
|
|
|
|
public function setVerbose($verbose=true)
|
|
|
|
{
|
|
|
|
$this->verbose = (boolean)$verbose;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
2012-03-03 05:35:40 +00:00
|
|
|
}
|