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