2012-01-17 09:00:53 +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\Json\JsonFile;
|
2012-01-17 16:56:06 +00:00
|
|
|
use Composer\IO\IOInterface;
|
2012-01-17 09:00:53 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates an configured instance of composer.
|
|
|
|
*
|
|
|
|
* @author Ryan Weaver <ryan@knplabs.com>
|
|
|
|
* @author Jordi Boggiano <j.boggiano@seld.be>
|
|
|
|
* @author Igor Wiedler <igor@wiedler.ch>
|
|
|
|
*/
|
|
|
|
class Factory
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Creates a Composer instance
|
|
|
|
*
|
|
|
|
* @return Composer
|
|
|
|
*/
|
2012-01-17 16:56:06 +00:00
|
|
|
public function createComposer(IOInterface $io, $composerFile = null)
|
2012-01-17 09:00:53 +00:00
|
|
|
{
|
|
|
|
// load Composer configuration
|
|
|
|
if (null === $composerFile) {
|
|
|
|
$composerFile = getenv('COMPOSER') ?: 'composer.json';
|
|
|
|
}
|
|
|
|
|
|
|
|
$file = new JsonFile($composerFile);
|
|
|
|
if (!$file->exists()) {
|
|
|
|
if ($composerFile === 'composer.json') {
|
|
|
|
$message = 'Composer could not find a composer.json file in '.getcwd();
|
|
|
|
} else {
|
|
|
|
$message = 'Composer could not find the config file: '.$composerFile;
|
|
|
|
}
|
|
|
|
$instructions = 'To initialize a project, please create a composer.json file as described on the http://packagist.org/ "Getting Started" section';
|
|
|
|
throw new \InvalidArgumentException($message.PHP_EOL.$instructions);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Configuration defaults
|
|
|
|
$composerConfig = array(
|
|
|
|
'vendor-dir' => 'vendor',
|
|
|
|
);
|
|
|
|
|
|
|
|
$packageConfig = $file->read();
|
|
|
|
|
|
|
|
if (isset($packageConfig['config']) && is_array($packageConfig['config'])) {
|
|
|
|
$packageConfig['config'] = array_merge($composerConfig, $packageConfig['config']);
|
|
|
|
} else {
|
|
|
|
$packageConfig['config'] = $composerConfig;
|
|
|
|
}
|
|
|
|
|
|
|
|
$vendorDir = getenv('COMPOSER_VENDOR_DIR') ?: $packageConfig['config']['vendor-dir'];
|
|
|
|
if (!isset($packageConfig['config']['bin-dir'])) {
|
|
|
|
$packageConfig['config']['bin-dir'] = $vendorDir.'/bin';
|
|
|
|
}
|
|
|
|
$binDir = getenv('COMPOSER_BIN_DIR') ?: $packageConfig['config']['bin-dir'];
|
|
|
|
|
|
|
|
// initialize repository manager
|
2012-01-17 16:56:06 +00:00
|
|
|
$rm = $this->createRepositoryManager($io, $vendorDir);
|
2012-01-17 09:00:53 +00:00
|
|
|
|
|
|
|
// initialize download manager
|
2012-01-17 16:56:06 +00:00
|
|
|
$dm = $this->createDownloadManager($io);
|
2012-01-17 09:00:53 +00:00
|
|
|
|
|
|
|
// initialize installation manager
|
2012-01-17 16:56:06 +00:00
|
|
|
$im = $this->createInstallationManager($rm, $dm, $vendorDir, $binDir, $io);
|
2012-01-17 09:00:53 +00:00
|
|
|
|
|
|
|
// load package
|
|
|
|
$loader = new Package\Loader\RootPackageLoader($rm);
|
|
|
|
$package = $loader->load($packageConfig);
|
|
|
|
|
|
|
|
// load default repository unless it's explicitly disabled
|
|
|
|
if (!isset($packageConfig['repositories']['packagist']) || $packageConfig['repositories']['packagist'] !== false) {
|
|
|
|
$rm->addRepository(new Repository\ComposerRepository(array('url' => 'http://packagist.org')));
|
|
|
|
}
|
|
|
|
|
|
|
|
// init locker
|
|
|
|
$lockFile = substr($composerFile, -5) === '.json' ? substr($composerFile, 0, -4).'lock' : $composerFile . '.lock';
|
|
|
|
$locker = new Package\Locker(new JsonFile($lockFile), $rm, md5_file($composerFile));
|
|
|
|
|
|
|
|
// initialize composer
|
|
|
|
$composer = new Composer();
|
|
|
|
$composer->setPackage($package);
|
|
|
|
$composer->setLocker($locker);
|
|
|
|
$composer->setRepositoryManager($rm);
|
|
|
|
$composer->setDownloadManager($dm);
|
|
|
|
$composer->setInstallationManager($im);
|
|
|
|
|
|
|
|
return $composer;
|
|
|
|
}
|
|
|
|
|
2012-01-17 16:56:06 +00:00
|
|
|
protected function createRepositoryManager(IOInterface $io, $vendorDir)
|
2012-01-17 09:00:53 +00:00
|
|
|
{
|
2012-01-17 16:56:06 +00:00
|
|
|
$rm = new Repository\RepositoryManager($io);
|
2012-01-17 10:54:02 +00:00
|
|
|
$rm->setLocalRepository(new Repository\FilesystemRepository(new JsonFile($vendorDir.'/.composer/installed.json')));
|
2012-01-17 09:00:53 +00:00
|
|
|
$rm->setRepositoryClass('composer', 'Composer\Repository\ComposerRepository');
|
|
|
|
$rm->setRepositoryClass('vcs', 'Composer\Repository\VcsRepository');
|
|
|
|
$rm->setRepositoryClass('pear', 'Composer\Repository\PearRepository');
|
|
|
|
$rm->setRepositoryClass('package', 'Composer\Repository\PackageRepository');
|
|
|
|
|
|
|
|
return $rm;
|
|
|
|
}
|
|
|
|
|
2012-01-17 16:56:06 +00:00
|
|
|
protected function createDownloadManager(IOInterface $io)
|
2012-01-17 09:00:53 +00:00
|
|
|
{
|
|
|
|
$dm = new Downloader\DownloadManager();
|
2012-01-22 19:43:37 +00:00
|
|
|
$dm->setDownloader('git', new Downloader\GitDownloader($io));
|
|
|
|
$dm->setDownloader('svn', new Downloader\SvnDownloader($io));
|
|
|
|
$dm->setDownloader('hg', new Downloader\HgDownloader($io));
|
2012-01-17 16:56:06 +00:00
|
|
|
$dm->setDownloader('pear', new Downloader\PearDownloader($io));
|
|
|
|
$dm->setDownloader('zip', new Downloader\ZipDownloader($io));
|
2012-01-22 19:43:37 +00:00
|
|
|
$dm->setDownloader('tar', new Downloader\TarDownloader($io));
|
|
|
|
$dm->setDownloader('phar', new Downloader\PharDownloader($io));
|
2012-01-17 09:00:53 +00:00
|
|
|
|
|
|
|
return $dm;
|
|
|
|
}
|
|
|
|
|
2012-01-17 16:56:06 +00:00
|
|
|
protected function createInstallationManager(Repository\RepositoryManager $rm, Downloader\DownloadManager $dm, $vendorDir, $binDir, IOInterface $io)
|
2012-01-17 09:00:53 +00:00
|
|
|
{
|
2012-01-17 10:54:02 +00:00
|
|
|
$im = new Installer\InstallationManager($vendorDir);
|
2012-01-17 16:56:06 +00:00
|
|
|
$im->addInstaller(new Installer\LibraryInstaller($vendorDir, $binDir, $dm, $rm->getLocalRepository(), $io, null));
|
|
|
|
$im->addInstaller(new Installer\InstallerInstaller($vendorDir, $binDir, $dm, $rm->getLocalRepository(), $io, $im));
|
2012-01-17 09:00:53 +00:00
|
|
|
|
|
|
|
return $im;
|
|
|
|
}
|
|
|
|
|
2012-01-17 16:56:06 +00:00
|
|
|
static public function create(IOInterface $io, $composerFile = null)
|
2012-01-17 09:00:53 +00:00
|
|
|
{
|
|
|
|
$factory = new static();
|
|
|
|
|
2012-01-17 16:56:06 +00:00
|
|
|
return $factory->createComposer($io, $composerFile);
|
2012-01-17 09:00:53 +00:00
|
|
|
}
|
|
|
|
}
|