123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235 |
- <?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;
- use Composer\IO\IOInterface;
- use Composer\Repository\RepositoryManager;
- use Composer\Util\ProcessExecutor;
- use Composer\Util\RemoteFilesystem;
- /**
- * 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
- {
- public static function createConfig()
- {
- // load main Composer configuration
- if (!$home = getenv('COMPOSER_HOME')) {
- if (defined('PHP_WINDOWS_VERSION_MAJOR')) {
- $home = getenv('APPDATA') . '/Composer';
- } else {
- $home = getenv('HOME') . '/.composer';
- }
- }
- $config = new Config();
- $file = new JsonFile($home.'/config.json');
- if ($file->exists()) {
- $config->merge($file->read());
- }
- // add home dir to the config
- $config->merge(array('config' => array('home' => $home)));
- return $config;
- }
- /**
- * Creates a Composer instance
- *
- * @param IOInterface $io IO instance
- * @param mixed $localConfig either a configuration array or a filename to read from, if null it will read from the default filename
- * @return Composer
- */
- public function createComposer(IOInterface $io, $localConfig = null)
- {
- // load Composer configuration
- if (null === $localConfig) {
- $localConfig = getenv('COMPOSER') ?: 'composer.json';
- }
- if (is_string($localConfig)) {
- $composerFile = $localConfig;
- $file = new JsonFile($localConfig, new RemoteFilesystem($io));
- if (!$file->exists()) {
- if ($localConfig === 'composer.json') {
- $message = 'Composer could not find a composer.json file in '.getcwd();
- } else {
- $message = 'Composer could not find the config file: '.$localConfig;
- }
- $instructions = 'To initialize a project, please create a composer.json file as described in the http://getcomposer.org/ "Getting Started" section';
- throw new \InvalidArgumentException($message.PHP_EOL.$instructions);
- }
- $file->validateSchema(JsonFile::LAX_SCHEMA);
- $localConfig = $file->read();
- }
- // Configuration defaults
- $config = $this->createConfig();
- $config->merge($localConfig);
- $vendorDir = $config->get('vendor-dir');
- $binDir = $config->get('bin-dir');
- // setup process timeout
- ProcessExecutor::setTimeout((int) $config->get('process-timeout'));
- // initialize repository manager
- $rm = $this->createRepositoryManager($io, $config);
- // load default repository unless it's explicitly disabled
- $localConfig = $this->addPackagistRepository($localConfig);
- // load local repository
- $this->addLocalRepository($rm, $vendorDir);
- // load package
- $loader = new Package\Loader\RootPackageLoader($rm);
- $package = $loader->load($localConfig);
- // initialize download manager
- $dm = $this->createDownloadManager($io);
- // initialize installation manager
- $im = $this->createInstallationManager($rm, $dm, $vendorDir, $binDir, $io);
- // purge packages if they have been deleted on the filesystem
- $this->purgePackages($rm, $im);
- // initialize composer
- $composer = new Composer();
- $composer->setConfig($config);
- $composer->setPackage($package);
- $composer->setRepositoryManager($rm);
- $composer->setDownloadManager($dm);
- $composer->setInstallationManager($im);
- // init locker if possible
- if (isset($composerFile)) {
- $lockFile = "json" === pathinfo($composerFile, PATHINFO_EXTENSION)
- ? substr($composerFile, 0, -4).'lock'
- : $composerFile . '.lock';
- $locker = new Package\Locker(new JsonFile($lockFile, new RemoteFilesystem($io)), $rm, md5_file($composerFile));
- $composer->setLocker($locker);
- }
- return $composer;
- }
- protected function createRepositoryManager(IOInterface $io, Config $config)
- {
- $rm = new RepositoryManager($io, $config);
- $rm->setRepositoryClass('composer', 'Composer\Repository\ComposerRepository');
- $rm->setRepositoryClass('vcs', 'Composer\Repository\VcsRepository');
- $rm->setRepositoryClass('package', 'Composer\Repository\PackageRepository');
- $rm->setRepositoryClass('pear', 'Composer\Repository\PearRepository');
- $rm->setRepositoryClass('git', 'Composer\Repository\VcsRepository');
- $rm->setRepositoryClass('svn', 'Composer\Repository\VcsRepository');
- $rm->setRepositoryClass('hg', 'Composer\Repository\VcsRepository');
- return $rm;
- }
- protected function addLocalRepository(RepositoryManager $rm, $vendorDir)
- {
- $rm->setLocalRepository(new Repository\InstalledFilesystemRepository(new JsonFile($vendorDir.'/.composer/installed.json')));
- $rm->setLocalDevRepository(new Repository\InstalledFilesystemRepository(new JsonFile($vendorDir.'/.composer/installed_dev.json')));
- }
- protected function addPackagistRepository(array $localConfig)
- {
- $loadPackagist = true;
- $packagistConfig = array(
- 'type' => 'composer',
- 'url' => 'http://packagist.org'
- );
- if (isset($localConfig['repositories'])) {
- foreach ($localConfig['repositories'] as $key => $repo) {
- if (isset($repo['packagist'])) {
- if (true === $repo['packagist']) {
- $localConfig['repositories'][$key] = $packagistConfig;
- }
- $loadPackagist = false;
- break;
- }
- }
- } else {
- $localConfig['repositories'] = array();
- }
- if ($loadPackagist) {
- $localConfig['repositories'][] = $packagistConfig;
- }
- return $localConfig;
- }
- public function createDownloadManager(IOInterface $io)
- {
- $dm = new Downloader\DownloadManager();
- $dm->setDownloader('git', new Downloader\GitDownloader($io));
- $dm->setDownloader('svn', new Downloader\SvnDownloader($io));
- $dm->setDownloader('hg', new Downloader\HgDownloader($io));
- $dm->setDownloader('pear', new Downloader\PearDownloader($io));
- $dm->setDownloader('zip', new Downloader\ZipDownloader($io));
- $dm->setDownloader('tar', new Downloader\TarDownloader($io));
- $dm->setDownloader('phar', new Downloader\PharDownloader($io));
- $dm->setDownloader('file', new Downloader\FileDownloader($io));
- return $dm;
- }
- protected function createInstallationManager(Repository\RepositoryManager $rm, Downloader\DownloadManager $dm, $vendorDir, $binDir, IOInterface $io)
- {
- $im = new Installer\InstallationManager($vendorDir);
- $im->addInstaller(new Installer\LibraryInstaller($vendorDir, $binDir, $dm, $io, null));
- $im->addInstaller(new Installer\InstallerInstaller($vendorDir, $binDir, $dm, $io, $im, $rm->getLocalRepositories()));
- $im->addInstaller(new Installer\MetapackageInstaller($io));
- return $im;
- }
- protected function purgePackages(Repository\RepositoryManager $rm, Installer\InstallationManager $im)
- {
- foreach ($rm->getLocalRepositories() as $repo) {
- foreach ($repo->getPackages() as $package) {
- if (!$im->isPackageInstalled($repo, $package)) {
- $repo->removePackage($package);
- }
- }
- }
- }
- /**
- * @param IOInterface $io IO instance
- * @param mixed $config either a configuration array or a filename to read from, if null it will read from the default filename
- * @return Composer
- */
- static public function create(IOInterface $io, $config = null)
- {
- $factory = new static();
- return $factory->createComposer($io, $config);
- }
- }
|