Factory.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <?php
  2. /*
  3. * This file is part of Composer.
  4. *
  5. * (c) Nils Adermann <naderman@naderman.de>
  6. * Jordi Boggiano <j.boggiano@seld.be>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Composer;
  12. use Composer\Json\JsonFile;
  13. use Composer\IO\IOInterface;
  14. use Composer\Repository\RepositoryManager;
  15. use Composer\Util\ProcessExecutor;
  16. use Composer\Util\RemoteFilesystem;
  17. /**
  18. * Creates an configured instance of composer.
  19. *
  20. * @author Ryan Weaver <ryan@knplabs.com>
  21. * @author Jordi Boggiano <j.boggiano@seld.be>
  22. * @author Igor Wiedler <igor@wiedler.ch>
  23. */
  24. class Factory
  25. {
  26. public static function createConfig()
  27. {
  28. // load main Composer configuration
  29. if (!$home = getenv('COMPOSER_HOME')) {
  30. if (defined('PHP_WINDOWS_VERSION_MAJOR')) {
  31. $home = getenv('APPDATA') . '/Composer';
  32. } else {
  33. $home = getenv('HOME') . '/.composer';
  34. }
  35. }
  36. $config = new Config();
  37. $file = new JsonFile($home.'/config.json');
  38. if ($file->exists()) {
  39. $config->merge($file->read());
  40. }
  41. // add home dir to the config
  42. $config->merge(array('config' => array('home' => $home)));
  43. return $config;
  44. }
  45. /**
  46. * Creates a Composer instance
  47. *
  48. * @param IOInterface $io IO instance
  49. * @param mixed $localConfig either a configuration array or a filename to read from, if null it will read from the default filename
  50. * @return Composer
  51. */
  52. public function createComposer(IOInterface $io, $localConfig = null)
  53. {
  54. // load Composer configuration
  55. if (null === $localConfig) {
  56. $localConfig = getenv('COMPOSER') ?: 'composer.json';
  57. }
  58. if (is_string($localConfig)) {
  59. $composerFile = $localConfig;
  60. $file = new JsonFile($localConfig, new RemoteFilesystem($io));
  61. if (!$file->exists()) {
  62. if ($localConfig === 'composer.json') {
  63. $message = 'Composer could not find a composer.json file in '.getcwd();
  64. } else {
  65. $message = 'Composer could not find the config file: '.$localConfig;
  66. }
  67. $instructions = 'To initialize a project, please create a composer.json file as described in the http://getcomposer.org/ "Getting Started" section';
  68. throw new \InvalidArgumentException($message.PHP_EOL.$instructions);
  69. }
  70. $file->validateSchema(JsonFile::LAX_SCHEMA);
  71. $localConfig = $file->read();
  72. }
  73. // Configuration defaults
  74. $config = $this->createConfig();
  75. $config->merge($localConfig);
  76. $vendorDir = $config->get('vendor-dir');
  77. $binDir = $config->get('bin-dir');
  78. // setup process timeout
  79. ProcessExecutor::setTimeout((int) $config->get('process-timeout'));
  80. // initialize repository manager
  81. $rm = $this->createRepositoryManager($io, $config);
  82. // load default repository unless it's explicitly disabled
  83. $localConfig = $this->addPackagistRepository($localConfig);
  84. // load local repository
  85. $this->addLocalRepository($rm, $vendorDir);
  86. // load package
  87. $loader = new Package\Loader\RootPackageLoader($rm);
  88. $package = $loader->load($localConfig);
  89. // initialize download manager
  90. $dm = $this->createDownloadManager($io);
  91. // initialize installation manager
  92. $im = $this->createInstallationManager($rm, $dm, $vendorDir, $binDir, $io);
  93. // purge packages if they have been deleted on the filesystem
  94. $this->purgePackages($rm, $im);
  95. // initialize composer
  96. $composer = new Composer();
  97. $composer->setConfig($config);
  98. $composer->setPackage($package);
  99. $composer->setRepositoryManager($rm);
  100. $composer->setDownloadManager($dm);
  101. $composer->setInstallationManager($im);
  102. // init locker if possible
  103. if (isset($composerFile)) {
  104. $lockFile = "json" === pathinfo($composerFile, PATHINFO_EXTENSION)
  105. ? substr($composerFile, 0, -4).'lock'
  106. : $composerFile . '.lock';
  107. $locker = new Package\Locker(new JsonFile($lockFile, new RemoteFilesystem($io)), $rm, md5_file($composerFile));
  108. $composer->setLocker($locker);
  109. }
  110. return $composer;
  111. }
  112. protected function createRepositoryManager(IOInterface $io, Config $config)
  113. {
  114. $rm = new RepositoryManager($io, $config);
  115. $rm->setRepositoryClass('composer', 'Composer\Repository\ComposerRepository');
  116. $rm->setRepositoryClass('vcs', 'Composer\Repository\VcsRepository');
  117. $rm->setRepositoryClass('package', 'Composer\Repository\PackageRepository');
  118. $rm->setRepositoryClass('pear', 'Composer\Repository\PearRepository');
  119. $rm->setRepositoryClass('git', 'Composer\Repository\VcsRepository');
  120. $rm->setRepositoryClass('svn', 'Composer\Repository\VcsRepository');
  121. $rm->setRepositoryClass('hg', 'Composer\Repository\VcsRepository');
  122. return $rm;
  123. }
  124. protected function addLocalRepository(RepositoryManager $rm, $vendorDir)
  125. {
  126. $rm->setLocalRepository(new Repository\InstalledFilesystemRepository(new JsonFile($vendorDir.'/.composer/installed.json')));
  127. $rm->setLocalDevRepository(new Repository\InstalledFilesystemRepository(new JsonFile($vendorDir.'/.composer/installed_dev.json')));
  128. }
  129. protected function addPackagistRepository(array $localConfig)
  130. {
  131. $loadPackagist = true;
  132. $packagistConfig = array(
  133. 'type' => 'composer',
  134. 'url' => 'http://packagist.org'
  135. );
  136. if (isset($localConfig['repositories'])) {
  137. foreach ($localConfig['repositories'] as $key => $repo) {
  138. if (isset($repo['packagist'])) {
  139. if (true === $repo['packagist']) {
  140. $localConfig['repositories'][$key] = $packagistConfig;
  141. }
  142. $loadPackagist = false;
  143. break;
  144. }
  145. }
  146. } else {
  147. $localConfig['repositories'] = array();
  148. }
  149. if ($loadPackagist) {
  150. $localConfig['repositories'][] = $packagistConfig;
  151. }
  152. return $localConfig;
  153. }
  154. public function createDownloadManager(IOInterface $io)
  155. {
  156. $dm = new Downloader\DownloadManager();
  157. $dm->setDownloader('git', new Downloader\GitDownloader($io));
  158. $dm->setDownloader('svn', new Downloader\SvnDownloader($io));
  159. $dm->setDownloader('hg', new Downloader\HgDownloader($io));
  160. $dm->setDownloader('pear', new Downloader\PearDownloader($io));
  161. $dm->setDownloader('zip', new Downloader\ZipDownloader($io));
  162. $dm->setDownloader('tar', new Downloader\TarDownloader($io));
  163. $dm->setDownloader('phar', new Downloader\PharDownloader($io));
  164. $dm->setDownloader('file', new Downloader\FileDownloader($io));
  165. return $dm;
  166. }
  167. protected function createInstallationManager(Repository\RepositoryManager $rm, Downloader\DownloadManager $dm, $vendorDir, $binDir, IOInterface $io)
  168. {
  169. $im = new Installer\InstallationManager($vendorDir);
  170. $im->addInstaller(new Installer\LibraryInstaller($vendorDir, $binDir, $dm, $io, null));
  171. $im->addInstaller(new Installer\InstallerInstaller($vendorDir, $binDir, $dm, $io, $im, $rm->getLocalRepositories()));
  172. $im->addInstaller(new Installer\MetapackageInstaller($io));
  173. return $im;
  174. }
  175. protected function purgePackages(Repository\RepositoryManager $rm, Installer\InstallationManager $im)
  176. {
  177. foreach ($rm->getLocalRepositories() as $repo) {
  178. foreach ($repo->getPackages() as $package) {
  179. if (!$im->isPackageInstalled($repo, $package)) {
  180. $repo->removePackage($package);
  181. }
  182. }
  183. }
  184. }
  185. /**
  186. * @param IOInterface $io IO instance
  187. * @param mixed $config either a configuration array or a filename to read from, if null it will read from the default filename
  188. * @return Composer
  189. */
  190. static public function create(IOInterface $io, $config = null)
  191. {
  192. $factory = new static();
  193. return $factory->createComposer($io, $config);
  194. }
  195. }