Factory.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  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\Config\JsonConfigSource;
  13. use Composer\Json\JsonFile;
  14. use Composer\IO\IOInterface;
  15. use Composer\Package\Archiver;
  16. use Composer\Package\Version\VersionGuesser;
  17. use Composer\Repository\RepositoryManager;
  18. use Composer\Repository\RepositoryFactory;
  19. use Composer\Repository\WritableRepositoryInterface;
  20. use Composer\Util\Filesystem;
  21. use Composer\Util\Platform;
  22. use Composer\Util\ProcessExecutor;
  23. use Composer\Util\RemoteFilesystem;
  24. use Composer\Util\Silencer;
  25. use Composer\Plugin\PluginEvents;
  26. use Composer\EventDispatcher\Event;
  27. use Seld\JsonLint\DuplicateKeyException;
  28. use Symfony\Component\Console\Formatter\OutputFormatter;
  29. use Symfony\Component\Console\Formatter\OutputFormatterStyle;
  30. use Symfony\Component\Console\Output\ConsoleOutput;
  31. use Composer\EventDispatcher\EventDispatcher;
  32. use Composer\Autoload\AutoloadGenerator;
  33. use Composer\Package\Version\VersionParser;
  34. use Composer\Downloader\TransportException;
  35. use Seld\JsonLint\JsonParser;
  36. /**
  37. * Creates a configured instance of composer.
  38. *
  39. * @author Ryan Weaver <ryan@knplabs.com>
  40. * @author Jordi Boggiano <j.boggiano@seld.be>
  41. * @author Igor Wiedler <igor@wiedler.ch>
  42. * @author Nils Adermann <naderman@naderman.de>
  43. */
  44. class Factory
  45. {
  46. /**
  47. * @throws \RuntimeException
  48. * @return string
  49. */
  50. protected static function getHomeDir()
  51. {
  52. $home = getenv('COMPOSER_HOME');
  53. if ($home) {
  54. return $home;
  55. }
  56. if (Platform::isWindows()) {
  57. if (!getenv('APPDATA')) {
  58. throw new \RuntimeException('The APPDATA or COMPOSER_HOME environment variable must be set for composer to run correctly');
  59. }
  60. return rtrim(strtr(getenv('APPDATA'), '\\', '/'), '/') . '/Composer';
  61. }
  62. $userDir = self::getUserDir();
  63. if (is_dir($userDir . '/.composer')) {
  64. return $userDir . '/.composer';
  65. }
  66. if (self::useXdg()) {
  67. // XDG Base Directory Specifications
  68. $xdgConfig = getenv('XDG_CONFIG_HOME') ?: $userDir . '/.config';
  69. return $xdgConfig . '/composer';
  70. }
  71. return $userDir . '/.composer';
  72. }
  73. /**
  74. * @param string $home
  75. * @return string
  76. */
  77. protected static function getCacheDir($home)
  78. {
  79. $cacheDir = getenv('COMPOSER_CACHE_DIR');
  80. if ($cacheDir) {
  81. return $cacheDir;
  82. }
  83. $homeEnv = getenv('COMPOSER_HOME');
  84. if ($homeEnv) {
  85. return $homeEnv . '/cache';
  86. }
  87. if (Platform::isWindows()) {
  88. if ($cacheDir = getenv('LOCALAPPDATA')) {
  89. $cacheDir .= '/Composer';
  90. } else {
  91. $cacheDir = $home . '/cache';
  92. }
  93. return rtrim(strtr($cacheDir, '\\', '/'), '/');
  94. }
  95. $userDir = self::getUserDir();
  96. if ($home === $userDir . '/.composer' && is_dir($home . '/cache')) {
  97. return $home . '/cache';
  98. }
  99. if (self::useXdg()) {
  100. $xdgCache = getenv('XDG_CACHE_HOME') ?: $userDir . '/.cache';
  101. return $xdgCache . '/composer';
  102. }
  103. return $home . '/cache';
  104. }
  105. /**
  106. * @param string $home
  107. * @return string
  108. */
  109. protected static function getDataDir($home)
  110. {
  111. $homeEnv = getenv('COMPOSER_HOME');
  112. if ($homeEnv) {
  113. return $homeEnv;
  114. }
  115. if (Platform::isWindows()) {
  116. return strtr($home, '\\', '/');
  117. }
  118. $userDir = self::getUserDir();
  119. if ($home !== $userDir . '/.composer' && self::useXdg()) {
  120. $xdgData = getenv('XDG_DATA_HOME') ?: $userDir . '/.local/share';
  121. return $xdgData . '/composer';
  122. }
  123. return $home;
  124. }
  125. /**
  126. * @param IOInterface|null $io
  127. * @return Config
  128. */
  129. public static function createConfig(IOInterface $io = null, $cwd = null)
  130. {
  131. $cwd = $cwd ?: getcwd();
  132. $config = new Config(true, $cwd);
  133. // determine and add main dirs to the config
  134. $home = self::getHomeDir();
  135. $config->merge(array('config' => array(
  136. 'home' => $home,
  137. 'cache-dir' => self::getCacheDir($home),
  138. 'data-dir' => self::getDataDir($home),
  139. )));
  140. // Protect directory against web access. Since HOME could be
  141. // the www-data's user home and be web-accessible it is a
  142. // potential security risk
  143. $dirs = array($config->get('home'), $config->get('cache-dir'), $config->get('data-dir'));
  144. foreach ($dirs as $dir) {
  145. if (!file_exists($dir . '/.htaccess')) {
  146. if (!is_dir($dir)) {
  147. Silencer::call('mkdir', $dir, 0777, true);
  148. }
  149. Silencer::call('file_put_contents', $dir . '/.htaccess', 'Deny from all');
  150. }
  151. }
  152. // load global config
  153. $file = new JsonFile($config->get('home').'/config.json');
  154. if ($file->exists()) {
  155. if ($io && $io->isDebug()) {
  156. $io->writeError('Loading config file ' . $file->getPath());
  157. }
  158. $config->merge($file->read());
  159. }
  160. $config->setConfigSource(new JsonConfigSource($file));
  161. // load global auth file
  162. $file = new JsonFile($config->get('home').'/auth.json');
  163. if ($file->exists()) {
  164. if ($io && $io->isDebug()) {
  165. $io->writeError('Loading config file ' . $file->getPath());
  166. }
  167. $config->merge(array('config' => $file->read()));
  168. }
  169. $config->setAuthConfigSource(new JsonConfigSource($file, true));
  170. // load COMPOSER_AUTH environment variable if set
  171. if ($composerAuthEnv = getenv('COMPOSER_AUTH')) {
  172. $authData = json_decode($composerAuthEnv, true);
  173. if (is_null($authData)) {
  174. throw new \UnexpectedValueException('COMPOSER_AUTH environment variable is malformed, should be a valid JSON object');
  175. }
  176. if ($io && $io->isDebug()) {
  177. $io->writeError('Loading auth config from COMPOSER_AUTH');
  178. }
  179. $config->merge(array('config' => $authData));
  180. }
  181. return $config;
  182. }
  183. public static function getComposerFile()
  184. {
  185. return trim(getenv('COMPOSER')) ?: './composer.json';
  186. }
  187. public static function createAdditionalStyles()
  188. {
  189. return array(
  190. 'highlight' => new OutputFormatterStyle('red'),
  191. 'warning' => new OutputFormatterStyle('black', 'yellow'),
  192. );
  193. }
  194. /**
  195. * Creates a ConsoleOutput instance
  196. *
  197. * @return ConsoleOutput
  198. */
  199. public static function createOutput()
  200. {
  201. $styles = self::createAdditionalStyles();
  202. $formatter = new OutputFormatter(null, $styles);
  203. return new ConsoleOutput(ConsoleOutput::VERBOSITY_NORMAL, null, $formatter);
  204. }
  205. /**
  206. * @deprecated Use Composer\Repository\RepositoryFactory::defaultRepos instead
  207. */
  208. public static function createDefaultRepositories(IOInterface $io = null, Config $config = null, RepositoryManager $rm = null)
  209. {
  210. return RepositoryFactory::defaultRepos($io, $config, $rm);
  211. }
  212. /**
  213. * Creates a Composer instance
  214. *
  215. * @param IOInterface $io IO instance
  216. * @param array|string|null $localConfig either a configuration array or a filename to read from, if null it will
  217. * read from the default filename
  218. * @param bool $disablePlugins Whether plugins should not be loaded
  219. * @param bool $fullLoad Whether to initialize everything or only main project stuff (used when loading the global composer)
  220. * @throws \InvalidArgumentException
  221. * @throws \UnexpectedValueException
  222. * @return Composer
  223. */
  224. public function createComposer(IOInterface $io, $localConfig = null, $disablePlugins = false, $cwd = null, $fullLoad = true)
  225. {
  226. $cwd = $cwd ?: getcwd();
  227. // load Composer configuration
  228. if (null === $localConfig) {
  229. $localConfig = static::getComposerFile();
  230. }
  231. if (is_string($localConfig)) {
  232. $composerFile = $localConfig;
  233. $file = new JsonFile($localConfig, null, $io);
  234. if (!$file->exists()) {
  235. if ($localConfig === './composer.json' || $localConfig === 'composer.json') {
  236. $message = 'Composer could not find a composer.json file in '.$cwd;
  237. } else {
  238. $message = 'Composer could not find the config file: '.$localConfig;
  239. }
  240. $instructions = 'To initialize a project, please create a composer.json file as described in the https://getcomposer.org/ "Getting Started" section';
  241. throw new \InvalidArgumentException($message.PHP_EOL.$instructions);
  242. }
  243. $file->validateSchema(JsonFile::LAX_SCHEMA);
  244. $jsonParser = new JsonParser;
  245. try {
  246. $jsonParser->parse(file_get_contents($localConfig), JsonParser::DETECT_KEY_CONFLICTS);
  247. } catch (DuplicateKeyException $e) {
  248. $details = $e->getDetails();
  249. $io->writeError('<warning>Key '.$details['key'].' is a duplicate in '.$localConfig.' at line '.$details['line'].'</warning>');
  250. }
  251. $localConfig = $file->read();
  252. }
  253. // Load config and override with local config/auth config
  254. $config = static::createConfig($io, $cwd);
  255. $config->merge($localConfig);
  256. if (isset($composerFile)) {
  257. $io->writeError('Loading config file ' . $composerFile, true, IOInterface::DEBUG);
  258. $config->setConfigSource(new JsonConfigSource(new JsonFile(realpath($composerFile), null, $io)));
  259. $localAuthFile = new JsonFile(dirname(realpath($composerFile)) . '/auth.json', null, $io);
  260. if ($localAuthFile->exists()) {
  261. $io->writeError('Loading config file ' . $localAuthFile->getPath(), true, IOInterface::DEBUG);
  262. $config->merge(array('config' => $localAuthFile->read()));
  263. $config->setAuthConfigSource(new JsonConfigSource($localAuthFile, true));
  264. }
  265. }
  266. $vendorDir = $config->get('vendor-dir');
  267. // initialize composer
  268. $composer = new Composer();
  269. $composer->setConfig($config);
  270. if ($fullLoad) {
  271. // load auth configs into the IO instance
  272. $io->loadConfiguration($config);
  273. }
  274. $rfs = self::createRemoteFilesystem($io, $config);
  275. // initialize event dispatcher
  276. $dispatcher = new EventDispatcher($composer, $io);
  277. $composer->setEventDispatcher($dispatcher);
  278. // initialize repository manager
  279. $rm = RepositoryFactory::manager($io, $config, $dispatcher, $rfs);
  280. $composer->setRepositoryManager($rm);
  281. // load local repository
  282. $this->addLocalRepository($io, $rm, $vendorDir);
  283. // force-set the version of the global package if not defined as
  284. // guessing it adds no value and only takes time
  285. if (!$fullLoad && !isset($localConfig['version'])) {
  286. $localConfig['version'] = '1.0.0';
  287. }
  288. // load package
  289. $parser = new VersionParser;
  290. $guesser = new VersionGuesser($config, new ProcessExecutor($io), $parser);
  291. $loader = new Package\Loader\RootPackageLoader($rm, $config, $parser, $guesser);
  292. $package = $loader->load($localConfig, 'Composer\Package\RootPackage', $cwd);
  293. $composer->setPackage($package);
  294. // initialize installation manager
  295. $im = $this->createInstallationManager();
  296. $composer->setInstallationManager($im);
  297. if ($fullLoad) {
  298. // initialize download manager
  299. $dm = $this->createDownloadManager($io, $config, $dispatcher, $rfs);
  300. $composer->setDownloadManager($dm);
  301. // initialize autoload generator
  302. $generator = new AutoloadGenerator($dispatcher, $io);
  303. $composer->setAutoloadGenerator($generator);
  304. }
  305. // add installers to the manager (must happen after download manager is created since they read it out of $composer)
  306. $this->createDefaultInstallers($im, $composer, $io);
  307. if ($fullLoad) {
  308. $globalComposer = null;
  309. if (realpath($config->get('home')) !== $cwd) {
  310. $globalComposer = $this->createGlobalComposer($io, $config, $disablePlugins);
  311. }
  312. $pm = $this->createPluginManager($io, $composer, $globalComposer, $disablePlugins);
  313. $composer->setPluginManager($pm);
  314. $pm->loadInstalledPlugins();
  315. }
  316. // init locker if possible
  317. if ($fullLoad && isset($composerFile)) {
  318. $lockFile = "json" === pathinfo($composerFile, PATHINFO_EXTENSION)
  319. ? substr($composerFile, 0, -4).'lock'
  320. : $composerFile . '.lock';
  321. $locker = new Package\Locker($io, new JsonFile($lockFile, null, $io), $rm, $im, file_get_contents($composerFile));
  322. $composer->setLocker($locker);
  323. }
  324. if ($fullLoad) {
  325. $initEvent = new Event(PluginEvents::INIT);
  326. $composer->getEventDispatcher()->dispatch($initEvent->getName(), $initEvent);
  327. // once everything is initialized we can
  328. // purge packages from local repos if they have been deleted on the filesystem
  329. if ($rm->getLocalRepository()) {
  330. $this->purgePackages($rm->getLocalRepository(), $im);
  331. }
  332. }
  333. return $composer;
  334. }
  335. /**
  336. * @param IOInterface $io IO instance
  337. * @param bool $disablePlugins Whether plugins should not be loaded
  338. * @return Composer
  339. */
  340. public static function createGlobal(IOInterface $io, $disablePlugins = false)
  341. {
  342. $factory = new static();
  343. return $factory->createGlobalComposer($io, static::createConfig($io), $disablePlugins, true);
  344. }
  345. /**
  346. * @param Repository\RepositoryManager $rm
  347. * @param string $vendorDir
  348. */
  349. protected function addLocalRepository(IOInterface $io, RepositoryManager $rm, $vendorDir)
  350. {
  351. $rm->setLocalRepository(new Repository\InstalledFilesystemRepository(new JsonFile($vendorDir.'/composer/installed.json', null, $io)));
  352. }
  353. /**
  354. * @param Config $config
  355. * @return Composer|null
  356. */
  357. protected function createGlobalComposer(IOInterface $io, Config $config, $disablePlugins, $fullLoad = false)
  358. {
  359. $composer = null;
  360. try {
  361. $composer = self::createComposer($io, $config->get('home') . '/composer.json', $disablePlugins, $config->get('home'), $fullLoad);
  362. } catch (\Exception $e) {
  363. $io->writeError('Failed to initialize global composer: '.$e->getMessage(), true, IOInterface::DEBUG);
  364. }
  365. return $composer;
  366. }
  367. /**
  368. * @param IO\IOInterface $io
  369. * @param Config $config
  370. * @param EventDispatcher $eventDispatcher
  371. * @return Downloader\DownloadManager
  372. */
  373. public function createDownloadManager(IOInterface $io, Config $config, EventDispatcher $eventDispatcher = null, RemoteFilesystem $rfs = null)
  374. {
  375. $cache = null;
  376. if ($config->get('cache-files-ttl') > 0) {
  377. $cache = new Cache($io, $config->get('cache-files-dir'), 'a-z0-9_./');
  378. }
  379. $dm = new Downloader\DownloadManager($io);
  380. switch ($preferred = $config->get('preferred-install')) {
  381. case 'dist':
  382. $dm->setPreferDist(true);
  383. break;
  384. case 'source':
  385. $dm->setPreferSource(true);
  386. break;
  387. case 'auto':
  388. default:
  389. // noop
  390. break;
  391. }
  392. if (is_array($preferred)) {
  393. $dm->setPreferences($preferred);
  394. }
  395. $executor = new ProcessExecutor($io);
  396. $fs = new Filesystem($executor);
  397. $dm->setDownloader('git', new Downloader\GitDownloader($io, $config, $executor, $fs));
  398. $dm->setDownloader('svn', new Downloader\SvnDownloader($io, $config, $executor, $fs));
  399. $dm->setDownloader('fossil', new Downloader\FossilDownloader($io, $config, $executor, $fs));
  400. $dm->setDownloader('hg', new Downloader\HgDownloader($io, $config, $executor, $fs));
  401. $dm->setDownloader('perforce', new Downloader\PerforceDownloader($io, $config));
  402. $dm->setDownloader('zip', new Downloader\ZipDownloader($io, $config, $eventDispatcher, $cache, $executor, $rfs));
  403. $dm->setDownloader('rar', new Downloader\RarDownloader($io, $config, $eventDispatcher, $cache, $executor, $rfs));
  404. $dm->setDownloader('tar', new Downloader\TarDownloader($io, $config, $eventDispatcher, $cache, $rfs));
  405. $dm->setDownloader('gzip', new Downloader\GzipDownloader($io, $config, $eventDispatcher, $cache, $executor, $rfs));
  406. $dm->setDownloader('xz', new Downloader\XzDownloader($io, $config, $eventDispatcher, $cache, $executor, $rfs));
  407. $dm->setDownloader('phar', new Downloader\PharDownloader($io, $config, $eventDispatcher, $cache, $rfs));
  408. $dm->setDownloader('file', new Downloader\FileDownloader($io, $config, $eventDispatcher, $cache, $rfs));
  409. $dm->setDownloader('path', new Downloader\PathDownloader($io, $config, $eventDispatcher, $cache, $rfs));
  410. return $dm;
  411. }
  412. /**
  413. * @param Config $config The configuration
  414. * @param Downloader\DownloadManager $dm Manager use to download sources
  415. * @return Archiver\ArchiveManager
  416. */
  417. public function createArchiveManager(Config $config, Downloader\DownloadManager $dm = null)
  418. {
  419. if (null === $dm) {
  420. $io = new IO\NullIO();
  421. $io->loadConfiguration($config);
  422. $dm = $this->createDownloadManager($io, $config);
  423. }
  424. $am = new Archiver\ArchiveManager($dm);
  425. $am->addArchiver(new Archiver\ZipArchiver);
  426. $am->addArchiver(new Archiver\PharArchiver);
  427. return $am;
  428. }
  429. /**
  430. * @param IOInterface $io
  431. * @param Composer $composer
  432. * @param Composer $globalComposer
  433. * @param bool $disablePlugins
  434. * @return Plugin\PluginManager
  435. */
  436. protected function createPluginManager(IOInterface $io, Composer $composer, Composer $globalComposer = null, $disablePlugins = false)
  437. {
  438. return new Plugin\PluginManager($io, $composer, $globalComposer, $disablePlugins);
  439. }
  440. /**
  441. * @return Installer\InstallationManager
  442. */
  443. protected function createInstallationManager()
  444. {
  445. return new Installer\InstallationManager();
  446. }
  447. /**
  448. * @param Installer\InstallationManager $im
  449. * @param Composer $composer
  450. * @param IO\IOInterface $io
  451. */
  452. protected function createDefaultInstallers(Installer\InstallationManager $im, Composer $composer, IOInterface $io)
  453. {
  454. $im->addInstaller(new Installer\LibraryInstaller($io, $composer, null));
  455. $im->addInstaller(new Installer\PearInstaller($io, $composer, 'pear-library'));
  456. $im->addInstaller(new Installer\PluginInstaller($io, $composer));
  457. $im->addInstaller(new Installer\MetapackageInstaller($io));
  458. }
  459. /**
  460. * @param WritableRepositoryInterface $repo repository to purge packages from
  461. * @param Installer\InstallationManager $im manager to check whether packages are still installed
  462. */
  463. protected function purgePackages(WritableRepositoryInterface $repo, Installer\InstallationManager $im)
  464. {
  465. foreach ($repo->getPackages() as $package) {
  466. if (!$im->isPackageInstalled($repo, $package)) {
  467. $repo->removePackage($package);
  468. }
  469. }
  470. }
  471. /**
  472. * @param IOInterface $io IO instance
  473. * @param mixed $config either a configuration array or a filename to read from, if null it will read from
  474. * the default filename
  475. * @param bool $disablePlugins Whether plugins should not be loaded
  476. * @return Composer
  477. */
  478. public static function create(IOInterface $io, $config = null, $disablePlugins = false)
  479. {
  480. $factory = new static();
  481. return $factory->createComposer($io, $config, $disablePlugins);
  482. }
  483. /**
  484. * @param IOInterface $io IO instance
  485. * @param Config $config Config instance
  486. * @param array $options Array of options passed directly to RemoteFilesystem constructor
  487. * @return RemoteFilesystem
  488. */
  489. public static function createRemoteFilesystem(IOInterface $io, Config $config = null, $options = array())
  490. {
  491. static $warned = false;
  492. $disableTls = false;
  493. if ($config && $config->get('disable-tls') === true) {
  494. if (!$warned) {
  495. $io->write('<warning>You are running Composer with SSL/TLS protection disabled.</warning>');
  496. }
  497. $warned = true;
  498. $disableTls = true;
  499. } elseif (!extension_loaded('openssl')) {
  500. throw new Exception\NoSslException('The openssl extension is required for SSL/TLS protection but is not available. '
  501. . 'If you can not enable the openssl extension, you can disable this error, at your own risk, by setting the \'disable-tls\' option to true.');
  502. }
  503. $remoteFilesystemOptions = array();
  504. if ($disableTls === false) {
  505. if ($config && $config->get('cafile')) {
  506. $remoteFilesystemOptions['ssl']['cafile'] = $config->get('cafile');
  507. }
  508. if ($config && $config->get('capath')) {
  509. $remoteFilesystemOptions['ssl']['capath'] = $config->get('capath');
  510. }
  511. $remoteFilesystemOptions = array_replace_recursive($remoteFilesystemOptions, $options);
  512. }
  513. try {
  514. $remoteFilesystem = new RemoteFilesystem($io, $config, $remoteFilesystemOptions, $disableTls);
  515. } catch (TransportException $e) {
  516. if (false !== strpos($e->getMessage(), 'cafile')) {
  517. $io->write('<error>Unable to locate a valid CA certificate file. You must set a valid \'cafile\' option.</error>');
  518. $io->write('<error>A valid CA certificate file is required for SSL/TLS protection.</error>');
  519. if (PHP_VERSION_ID < 50600) {
  520. $io->write('<error>It is recommended you upgrade to PHP 5.6+ which can detect your system CA file automatically.</error>');
  521. }
  522. $io->write('<error>You can disable this error, at your own risk, by setting the \'disable-tls\' option to true.</error>');
  523. }
  524. throw $e;
  525. }
  526. return $remoteFilesystem;
  527. }
  528. /**
  529. * @return bool
  530. */
  531. private static function useXdg()
  532. {
  533. foreach (array_keys($_SERVER) as $key) {
  534. if (substr($key, 0, 4) === 'XDG_') {
  535. return true;
  536. }
  537. }
  538. return false;
  539. }
  540. /**
  541. * @throws \RuntimeException
  542. * @return string
  543. */
  544. private static function getUserDir()
  545. {
  546. $home = getenv('HOME');
  547. if (!$home) {
  548. throw new \RuntimeException('The HOME or COMPOSER_HOME environment variable must be set for composer to run correctly');
  549. }
  550. return rtrim(strtr($home, '\\', '/'), '/');
  551. }
  552. }