Factory.php 10 KB

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