Factory.php 24 KB

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