From 94e277698e4c098b4a485a7278e097e23f667cd8 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Wed, 30 Nov 2011 21:30:51 +0100 Subject: [PATCH 1/2] Delay bootstrap until it's really necessary, fixes #121 --- bin/composer | 72 +-------------------- src/Composer/Console/Application.php | 96 ++++++++++++++++++++++++++-- 2 files changed, 94 insertions(+), 74 deletions(-) diff --git a/bin/composer b/bin/composer index dd23d99dc..8062757f3 100755 --- a/bin/composer +++ b/bin/composer @@ -8,76 +8,8 @@ php composer.phar install '); } -use Composer\Composer; -use Composer\Installer; -use Composer\Downloader; -use Composer\Repository; -use Composer\Package; -use Composer\Json\JsonFile; -use Composer\Console\Application as ComposerApplication; - -// load Composer configuration -$file = new JsonFile('composer.json'); -if (!$file->exists()) { - echo 'Composer could not find a composer.json file in '.getcwd().PHP_EOL; - echo 'To initialize a project, please create a composer.json file as described on the http://packagist.org/ "Getting Started" section'.PHP_EOL; - exit(1); -} - -// 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 = $packageConfig['config']['vendor-dir']; - -// initialize repository manager -$rm = new Repository\RepositoryManager(); -$rm->setLocalRepository(new Repository\FilesystemRepository(new JsonFile($vendorDir.'/.composer/installed.json'))); -$rm->setRepositoryClass('composer', 'Composer\Repository\ComposerRepository'); -$rm->setRepositoryClass('vcs', 'Composer\Repository\VcsRepository'); -$rm->setRepositoryClass('pear', 'Composer\Repository\PearRepository'); -$rm->setRepositoryClass('package', 'Composer\Repository\PackageRepository'); - -// initialize download manager -$dm = new Downloader\DownloadManager(); -$dm->setDownloader('git', new Downloader\GitDownloader()); -$dm->setDownloader('svn', new Downloader\SvnDownloader()); -$dm->setDownloader('hg', new Downloader\HgDownloader()); -$dm->setDownloader('pear', new Downloader\PearDownloader()); -$dm->setDownloader('zip', new Downloader\ZipDownloader()); - -// initialize installation manager -$im = new Installer\InstallationManager($vendorDir); -$im->addInstaller(new Installer\LibraryInstaller($vendorDir, $dm, $rm->getLocalRepository(), null)); -$im->addInstaller(new Installer\InstallerInstaller($vendorDir, $dm, $rm->getLocalRepository(), $im)); - -// load package -$loader = new Package\Loader\ArrayLoader($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('http://packagist.org')); -} - -// init locker -$locker = new Package\Locker(new JsonFile('composer.lock'), $rm); - -// initialize composer -$composer = new Composer(); -$composer->setPackage($package); -$composer->setLocker($locker); -$composer->setRepositoryManager($rm); -$composer->setDownloadManager($dm); -$composer->setInstallationManager($im); +use Composer\Console\Application; // run the command application -$application = new ComposerApplication($composer); +$application = new Application(); $application->run(); diff --git a/src/Composer/Console/Application.php b/src/Composer/Console/Application.php index b0fcb0e3b..9cf6e8811 100644 --- a/src/Composer/Console/Application.php +++ b/src/Composer/Console/Application.php @@ -18,21 +18,25 @@ use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Finder\Finder; use Composer\Command; use Composer\Composer; +use Composer\Installer; +use Composer\Downloader; +use Composer\Repository; +use Composer\Package; +use Composer\Json\JsonFile; /** * The console application that handles the commands * * @author Ryan Weaver + * @author Jordi Boggiano */ class Application extends BaseApplication { - private $composer; + protected $composer; - public function __construct(Composer $composer) + public function __construct() { parent::__construct('Composer', Composer::VERSION); - - $this->composer = $composer; } /** @@ -55,9 +59,93 @@ class Application extends BaseApplication */ public function getComposer() { + if (null === $this->composer) { + $this->composer = self::bootstrapComposer(); + } + return $this->composer; } + /** + * Bootstraps a Composer instance + * + * @return Composer + */ + public static function bootstrapComposer($composerFile = null) + { + // load Composer configuration + if (null === $composerFile) { + $composerFile = 'composer.json'; + } + + $file = new JsonFile($composerFile); + if (!$file->exists()) { + if ($composerFile === 'composer.json') { + echo 'Composer could not find a composer.json file in '.getcwd().PHP_EOL; + } else { + echo 'Composer could not find the config file: '.$composerFile.PHP_EOL; + } + echo 'To initialize a project, please create a composer.json file as described on the http://packagist.org/ "Getting Started" section'.PHP_EOL; + exit(1); + } + + // 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 = $packageConfig['config']['vendor-dir']; + + // initialize repository manager + $rm = new Repository\RepositoryManager(); + $rm->setLocalRepository(new Repository\FilesystemRepository(new JsonFile($vendorDir.'/.composer/installed.json'))); + $rm->setRepositoryClass('composer', 'Composer\Repository\ComposerRepository'); + $rm->setRepositoryClass('vcs', 'Composer\Repository\VcsRepository'); + $rm->setRepositoryClass('pear', 'Composer\Repository\PearRepository'); + $rm->setRepositoryClass('package', 'Composer\Repository\PackageRepository'); + + // initialize download manager + $dm = new Downloader\DownloadManager(); + $dm->setDownloader('git', new Downloader\GitDownloader()); + $dm->setDownloader('svn', new Downloader\SvnDownloader()); + $dm->setDownloader('hg', new Downloader\HgDownloader()); + $dm->setDownloader('pear', new Downloader\PearDownloader()); + $dm->setDownloader('zip', new Downloader\ZipDownloader()); + + // initialize installation manager + $im = new Installer\InstallationManager($vendorDir); + $im->addInstaller(new Installer\LibraryInstaller($vendorDir, $dm, $rm->getLocalRepository(), null)); + $im->addInstaller(new Installer\InstallerInstaller($vendorDir, $dm, $rm->getLocalRepository(), $im)); + + // load package + $loader = new Package\Loader\ArrayLoader($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('http://packagist.org')); + } + + // init locker + $locker = new Package\Locker(new JsonFile('composer.lock'), $rm); + + // initialize composer + $composer = new Composer(); + $composer->setPackage($package); + $composer->setLocker($locker); + $composer->setRepositoryManager($rm); + $composer->setDownloadManager($dm); + $composer->setInstallationManager($im); + + return $composer; + } + /** * Initializes all the composer commands */ From 7fa74f8f8e9734799741f4128fbcd9559020223e Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Wed, 30 Nov 2011 21:50:11 +0100 Subject: [PATCH 2/2] Add COMPOSER env var to override the composer.json file name --- src/Composer/Console/Application.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Composer/Console/Application.php b/src/Composer/Console/Application.php index 9cf6e8811..c45ebb19d 100644 --- a/src/Composer/Console/Application.php +++ b/src/Composer/Console/Application.php @@ -75,7 +75,7 @@ class Application extends BaseApplication { // load Composer configuration if (null === $composerFile) { - $composerFile = 'composer.json'; + $composerFile = getenv('COMPOSER') ?: 'composer.json'; } $file = new JsonFile($composerFile); @@ -133,7 +133,8 @@ class Application extends BaseApplication } // init locker - $locker = new Package\Locker(new JsonFile('composer.lock'), $rm); + $lockFile = substr($composerFile, -5) === '.json' ? substr($composerFile, 0, -4).'lock' : $composerFile . '.lock'; + $locker = new Package\Locker(new JsonFile($lockFile), $rm); // initialize composer $composer = new Composer();