Factory.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. /**
  27. * @return Config
  28. */
  29. public static function createConfig()
  30. {
  31. // load main Composer configuration
  32. if (!$home = getenv('COMPOSER_HOME')) {
  33. if (defined('PHP_WINDOWS_VERSION_MAJOR')) {
  34. $home = getenv('APPDATA') . '/Composer';
  35. } else {
  36. $home = getenv('HOME') . '/.composer';
  37. }
  38. }
  39. // Protect directory against web access
  40. if (!file_exists($home . '/.htaccess')) {
  41. if (!is_dir($home)) {
  42. @mkdir($home, 0777, true);
  43. }
  44. @file_put_contents($home . '/.htaccess', 'Deny from all');
  45. }
  46. $config = new Config();
  47. $file = new JsonFile($home.'/config.json');
  48. if ($file->exists()) {
  49. $config->merge($file->read());
  50. }
  51. // add home dir to the config
  52. $config->merge(array('config' => array('home' => $home)));
  53. return $config;
  54. }
  55. public function getComposerFile()
  56. {
  57. return getenv('COMPOSER') ?: 'composer.json';
  58. }
  59. /**
  60. * Creates a Composer instance
  61. *
  62. * @param IOInterface $io IO instance
  63. * @param mixed $localConfig either a configuration array or a filename to read from, if null it will read from the default filename
  64. * @return Composer
  65. */
  66. public function createComposer(IOInterface $io, $localConfig = null)
  67. {
  68. // load Composer configuration
  69. if (null === $localConfig) {
  70. $localConfig = $this->getComposerFile();
  71. }
  72. if (is_string($localConfig)) {
  73. $composerFile = $localConfig;
  74. $file = new JsonFile($localConfig, new RemoteFilesystem($io));
  75. if (!$file->exists()) {
  76. if ($localConfig === 'composer.json') {
  77. $message = 'Composer could not find a composer.json file in '.getcwd();
  78. } else {
  79. $message = 'Composer could not find the config file: '.$localConfig;
  80. }
  81. $instructions = 'To initialize a project, please create a composer.json file as described in the http://getcomposer.org/ "Getting Started" section';
  82. throw new \InvalidArgumentException($message.PHP_EOL.$instructions);
  83. }
  84. $file->validateSchema(JsonFile::LAX_SCHEMA);
  85. $localConfig = $file->read();
  86. }
  87. // Configuration defaults
  88. $config = static::createConfig();
  89. $config->merge($localConfig);
  90. $vendorDir = $config->get('vendor-dir');
  91. $binDir = $config->get('bin-dir');
  92. // setup process timeout
  93. ProcessExecutor::setTimeout((int) $config->get('process-timeout'));
  94. // initialize repository manager
  95. $rm = $this->createRepositoryManager($io, $config);
  96. // load default repository unless it's explicitly disabled
  97. $localConfig = $this->addPackagistRepository($localConfig);
  98. // load local repository
  99. $this->addLocalRepository($rm, $vendorDir);
  100. // load package
  101. $loader = new Package\Loader\RootPackageLoader($rm);
  102. $package = $loader->load($localConfig);
  103. // initialize download manager
  104. $dm = $this->createDownloadManager($io);
  105. // initialize installation manager
  106. $im = $this->createInstallationManager($rm, $dm, $vendorDir, $binDir, $io);
  107. // purge packages if they have been deleted on the filesystem
  108. $this->purgePackages($rm, $im);
  109. // initialize composer
  110. $composer = new Composer();
  111. $composer->setConfig($config);
  112. $composer->setPackage($package);
  113. $composer->setRepositoryManager($rm);
  114. $composer->setDownloadManager($dm);
  115. $composer->setInstallationManager($im);
  116. // init locker if possible
  117. if (isset($composerFile)) {
  118. $lockFile = "json" === pathinfo($composerFile, PATHINFO_EXTENSION)
  119. ? substr($composerFile, 0, -4).'lock'
  120. : $composerFile . '.lock';
  121. $locker = new Package\Locker(new JsonFile($lockFile, new RemoteFilesystem($io)), $rm, md5_file($composerFile));
  122. $composer->setLocker($locker);
  123. }
  124. return $composer;
  125. }
  126. protected function createRepositoryManager(IOInterface $io, Config $config)
  127. {
  128. $rm = new RepositoryManager($io, $config);
  129. $rm->setRepositoryClass('composer', 'Composer\Repository\ComposerRepository');
  130. $rm->setRepositoryClass('vcs', 'Composer\Repository\VcsRepository');
  131. $rm->setRepositoryClass('package', 'Composer\Repository\PackageRepository');
  132. $rm->setRepositoryClass('pear', 'Composer\Repository\PearRepository');
  133. $rm->setRepositoryClass('git', 'Composer\Repository\VcsRepository');
  134. $rm->setRepositoryClass('svn', 'Composer\Repository\VcsRepository');
  135. $rm->setRepositoryClass('hg', 'Composer\Repository\VcsRepository');
  136. return $rm;
  137. }
  138. protected function addLocalRepository(RepositoryManager $rm, $vendorDir)
  139. {
  140. // TODO BC feature, remove after June 15th
  141. if (file_exists($vendorDir.'/.composer/installed.json')) {
  142. if (!is_dir($vendorDir.'/composer')) { mkdir($vendorDir.'/composer/', 0777, true); }
  143. rename($vendorDir.'/.composer/installed.json', $vendorDir.'/composer/installed.json');
  144. }
  145. if (file_exists($vendorDir.'/.composer/installed_dev.json')) {
  146. if (!is_dir($vendorDir.'/composer')) { mkdir($vendorDir.'/composer/', 0777, true); }
  147. rename($vendorDir.'/.composer/installed_dev.json', $vendorDir.'/composer/installed_dev.json');
  148. }
  149. if (file_exists($vendorDir.'/installed.json')) {
  150. if (!is_dir($vendorDir.'/composer')) { mkdir($vendorDir.'/composer/', 0777, true); }
  151. rename($vendorDir.'/installed.json', $vendorDir.'/composer/installed.json');
  152. }
  153. if (file_exists($vendorDir.'/installed_dev.json')) {
  154. if (!is_dir($vendorDir.'/composer')) { mkdir($vendorDir.'/composer/', 0777, true); }
  155. rename($vendorDir.'/installed_dev.json', $vendorDir.'/composer/installed_dev.json');
  156. }
  157. $rm->setLocalRepository(new Repository\InstalledFilesystemRepository(new JsonFile($vendorDir.'/composer/installed.json')));
  158. $rm->setLocalDevRepository(new Repository\InstalledFilesystemRepository(new JsonFile($vendorDir.'/composer/installed_dev.json')));
  159. }
  160. protected function addPackagistRepository(array $localConfig)
  161. {
  162. $loadPackagist = true;
  163. $packagistConfig = array(
  164. 'type' => 'composer',
  165. 'url' => 'http://packagist.org'
  166. );
  167. if (isset($localConfig['repositories'])) {
  168. foreach ($localConfig['repositories'] as $key => $repo) {
  169. if (isset($repo['packagist'])) {
  170. if (true === $repo['packagist']) {
  171. $localConfig['repositories'][$key] = $packagistConfig;
  172. }
  173. $loadPackagist = false;
  174. break;
  175. }
  176. }
  177. } else {
  178. $localConfig['repositories'] = array();
  179. }
  180. if ($loadPackagist) {
  181. $localConfig['repositories'][] = $packagistConfig;
  182. }
  183. return $localConfig;
  184. }
  185. public function createDownloadManager(IOInterface $io)
  186. {
  187. $dm = new Downloader\DownloadManager();
  188. $dm->setDownloader('git', new Downloader\GitDownloader($io));
  189. $dm->setDownloader('svn', new Downloader\SvnDownloader($io));
  190. $dm->setDownloader('hg', new Downloader\HgDownloader($io));
  191. $dm->setDownloader('pear', new Downloader\PearDownloader($io));
  192. $dm->setDownloader('zip', new Downloader\ZipDownloader($io));
  193. $dm->setDownloader('tar', new Downloader\TarDownloader($io));
  194. $dm->setDownloader('phar', new Downloader\PharDownloader($io));
  195. $dm->setDownloader('file', new Downloader\FileDownloader($io));
  196. return $dm;
  197. }
  198. protected function createInstallationManager(Repository\RepositoryManager $rm, Downloader\DownloadManager $dm, $vendorDir, $binDir, IOInterface $io)
  199. {
  200. $im = new Installer\InstallationManager($vendorDir);
  201. $im->addInstaller(new Installer\LibraryInstaller($vendorDir, $binDir, $dm, $io, null));
  202. $im->addInstaller(new Installer\InstallerInstaller($vendorDir, $binDir, $dm, $io, $im, $rm->getLocalRepositories()));
  203. $im->addInstaller(new Installer\MetapackageInstaller($io));
  204. return $im;
  205. }
  206. protected function purgePackages(Repository\RepositoryManager $rm, Installer\InstallationManager $im)
  207. {
  208. foreach ($rm->getLocalRepositories() as $repo) {
  209. foreach ($repo->getPackages() as $package) {
  210. if (!$im->isPackageInstalled($repo, $package)) {
  211. $repo->removePackage($package);
  212. }
  213. }
  214. }
  215. }
  216. /**
  217. * @param IOInterface $io IO instance
  218. * @param mixed $config either a configuration array or a filename to read from, if null it will read from the default filename
  219. * @return Composer
  220. */
  221. public static function create(IOInterface $io, $config = null)
  222. {
  223. $factory = new static();
  224. return $factory->createComposer($io, $config);
  225. }
  226. }