Factory.php 24 KB

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