Factory.php 23 KB

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