Factory.php 24 KB

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