Factory.php 25 KB

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