|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Consolidation\Cgr; |
| 4 | + |
| 5 | +/** |
| 6 | + * Determine information about the system |
| 7 | + */ |
| 8 | +class SystemInformation |
| 9 | +{ |
| 10 | + /** |
| 11 | + * @throws \RuntimeException |
| 12 | + * @return string |
| 13 | + */ |
| 14 | + public static function getHomeDir() |
| 15 | + { |
| 16 | + $home = getenv('COMPOSER_HOME'); |
| 17 | + if ($home) { |
| 18 | + return $home; |
| 19 | + } |
| 20 | + if (self::isWindows()) { |
| 21 | + if (!getenv('APPDATA')) { |
| 22 | + throw new \RuntimeException('The APPDATA or COMPOSER_HOME environment variable must be set for composer to run correctly'); |
| 23 | + } |
| 24 | + return rtrim(strtr(getenv('APPDATA'), '\\', '/'), '/') . '/Composer'; |
| 25 | + } |
| 26 | + $userDir = self::getUserDir(); |
| 27 | + if (is_dir($userDir . '/.composer')) { |
| 28 | + return $userDir . '/.composer'; |
| 29 | + } |
| 30 | + if (self::useXdg()) { |
| 31 | + // XDG Base Directory Specifications |
| 32 | + $xdgConfig = getenv('XDG_CONFIG_HOME') ?: $userDir . '/.config'; |
| 33 | + return $xdgConfig . '/composer'; |
| 34 | + } |
| 35 | + return $userDir . '/.composer'; |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * @return bool Whether the host machine is running a Windows OS |
| 40 | + */ |
| 41 | + public static function isWindows() |
| 42 | + { |
| 43 | + return defined('PHP_WINDOWS_VERSION_BUILD'); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * @return bool |
| 48 | + */ |
| 49 | + private static function useXdg() |
| 50 | + { |
| 51 | + foreach (array_keys($_SERVER) as $key) { |
| 52 | + if (substr($key, 0, 4) === 'XDG_') { |
| 53 | + return true; |
| 54 | + } |
| 55 | + } |
| 56 | + return false; |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * @throws \RuntimeException |
| 61 | + * @return string |
| 62 | + */ |
| 63 | + private static function getUserDir() |
| 64 | + { |
| 65 | + $home = getenv('HOME'); |
| 66 | + if (!$home) { |
| 67 | + throw new \RuntimeException('The HOME or COMPOSER_HOME environment variable must be set for composer to run correctly'); |
| 68 | + } |
| 69 | + return rtrim(strtr($home, '\\', '/'), '/'); |
| 70 | + } |
| 71 | +} |
0 commit comments