Config.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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\ConfigSourceInterface;
  13. /**
  14. * @author Jordi Boggiano <j.boggiano@seld.be>
  15. */
  16. class Config
  17. {
  18. const RELATIVE_PATHS = 1;
  19. public static $defaultConfig = array(
  20. 'process-timeout' => 300,
  21. 'use-include-path' => false,
  22. 'preferred-install' => 'auto',
  23. 'notify-on-install' => true,
  24. 'github-protocols' => array('git', 'https', 'ssh'),
  25. 'vendor-dir' => 'vendor',
  26. 'bin-dir' => '{$vendor-dir}/bin',
  27. 'cache-dir' => '{$home}/cache',
  28. 'cache-files-dir' => '{$cache-dir}/files',
  29. 'cache-repo-dir' => '{$cache-dir}/repo',
  30. 'cache-vcs-dir' => '{$cache-dir}/vcs',
  31. 'cache-ttl' => 15552000, // 6 months
  32. 'cache-files-ttl' => null, // fallback to cache-ttl
  33. 'cache-files-maxsize' => '300MiB',
  34. 'bin-compat' => 'auto',
  35. 'discard-changes' => false,
  36. 'autoloader-suffix' => null,
  37. 'sort-packages' => false,
  38. 'optimize-autoloader' => false,
  39. 'classmap-authoritative' => false,
  40. 'prepend-autoloader' => true,
  41. 'github-domains' => array('github.com'),
  42. 'github-expose-hostname' => true,
  43. 'gitlab-domains' => array('gitlab.com'),
  44. 'store-auths' => 'prompt',
  45. 'platform' => array(),
  46. 'archive-format' => 'tar',
  47. 'archive-dir' => '.',
  48. // valid keys without defaults (auth config stuff):
  49. // github-oauth
  50. // gitlab-oauth
  51. // http-basic
  52. );
  53. public static $defaultRepositories = array(
  54. 'packagist' => array(
  55. 'type' => 'composer',
  56. 'url' => 'https?://packagist.org',
  57. 'allow_ssl_downgrade' => true,
  58. ),
  59. );
  60. private $config;
  61. private $baseDir;
  62. private $repositories;
  63. private $configSource;
  64. private $authConfigSource;
  65. private $useEnvironment;
  66. /**
  67. * @param bool $useEnvironment Use COMPOSER_ environment variables to replace config settings
  68. */
  69. public function __construct($useEnvironment = true, $baseDir = null)
  70. {
  71. // load defaults
  72. $this->config = static::$defaultConfig;
  73. $this->repositories = static::$defaultRepositories;
  74. $this->useEnvironment = (bool) $useEnvironment;
  75. $this->baseDir = $baseDir;
  76. }
  77. public function setConfigSource(ConfigSourceInterface $source)
  78. {
  79. $this->configSource = $source;
  80. }
  81. public function getConfigSource()
  82. {
  83. return $this->configSource;
  84. }
  85. public function setAuthConfigSource(ConfigSourceInterface $source)
  86. {
  87. $this->authConfigSource = $source;
  88. }
  89. public function getAuthConfigSource()
  90. {
  91. return $this->authConfigSource;
  92. }
  93. /**
  94. * Merges new config values with the existing ones (overriding)
  95. *
  96. * @param array $config
  97. */
  98. public function merge($config)
  99. {
  100. // override defaults with given config
  101. if (!empty($config['config']) && is_array($config['config'])) {
  102. foreach ($config['config'] as $key => $val) {
  103. if (in_array($key, array('github-oauth', 'gitlab-oauth', 'http-basic')) && isset($this->config[$key])) {
  104. $this->config[$key] = array_merge($this->config[$key], $val);
  105. } else {
  106. $this->config[$key] = $val;
  107. }
  108. }
  109. }
  110. if (!empty($config['repositories']) && is_array($config['repositories'])) {
  111. $this->repositories = array_reverse($this->repositories, true);
  112. $newRepos = array_reverse($config['repositories'], true);
  113. foreach ($newRepos as $name => $repository) {
  114. // disable a repository by name
  115. if (false === $repository) {
  116. unset($this->repositories[$name]);
  117. continue;
  118. }
  119. // disable a repository with an anonymous {"name": false} repo
  120. if (is_array($repository) && 1 === count($repository) && false === current($repository)) {
  121. unset($this->repositories[key($repository)]);
  122. continue;
  123. }
  124. // store repo
  125. if (is_int($name)) {
  126. $this->repositories[] = $repository;
  127. } else {
  128. $this->repositories[$name] = $repository;
  129. }
  130. }
  131. $this->repositories = array_reverse($this->repositories, true);
  132. }
  133. }
  134. /**
  135. * @return array
  136. */
  137. public function getRepositories()
  138. {
  139. return $this->repositories;
  140. }
  141. /**
  142. * Returns a setting
  143. *
  144. * @param string $key
  145. * @param int $flags Options (see class constants)
  146. * @throws \RuntimeException
  147. * @return mixed
  148. */
  149. public function get($key, $flags = 0)
  150. {
  151. switch ($key) {
  152. case 'vendor-dir':
  153. case 'bin-dir':
  154. case 'process-timeout':
  155. case 'cache-dir':
  156. case 'cache-files-dir':
  157. case 'cache-repo-dir':
  158. case 'cache-vcs-dir':
  159. // convert foo-bar to COMPOSER_FOO_BAR and check if it exists since it overrides the local config
  160. $env = 'COMPOSER_' . strtoupper(strtr($key, '-', '_'));
  161. $val = rtrim($this->process($this->getComposerEnv($env) ?: $this->config[$key], $flags), '/\\');
  162. $val = preg_replace('#^(\$HOME|~)(/|$)#', rtrim(getenv('HOME') ?: getenv('USERPROFILE'), '/\\') . '/', $val);
  163. if (substr($key, -4) !== '-dir') {
  164. return $val;
  165. }
  166. return ($flags & self::RELATIVE_PATHS == self::RELATIVE_PATHS) ? $val : $this->realpath($val);
  167. case 'cache-ttl':
  168. return (int) $this->config[$key];
  169. case 'cache-files-maxsize':
  170. if (!preg_match('/^\s*([0-9.]+)\s*(?:([kmg])(?:i?b)?)?\s*$/i', $this->config[$key], $matches)) {
  171. throw new \RuntimeException(
  172. "Could not parse the value of 'cache-files-maxsize': {$this->config[$key]}"
  173. );
  174. }
  175. $size = $matches[1];
  176. if (isset($matches[2])) {
  177. switch (strtolower($matches[2])) {
  178. case 'g':
  179. $size *= 1024;
  180. // intentional fallthrough
  181. case 'm':
  182. $size *= 1024;
  183. // intentional fallthrough
  184. case 'k':
  185. $size *= 1024;
  186. break;
  187. }
  188. }
  189. return $size;
  190. case 'cache-files-ttl':
  191. if (isset($this->config[$key])) {
  192. return (int) $this->config[$key];
  193. }
  194. return (int) $this->config['cache-ttl'];
  195. case 'home':
  196. return rtrim($this->process($this->config[$key], $flags), '/\\');
  197. case 'bin-compat':
  198. $value = $this->getComposerEnv('COMPOSER_BIN_COMPAT') ?: $this->config[$key];
  199. if (!in_array($value, array('auto', 'full'))) {
  200. throw new \RuntimeException(
  201. "Invalid value for 'bin-compat': {$value}. Expected auto, full"
  202. );
  203. }
  204. return $value;
  205. case 'discard-changes':
  206. if ($env = $this->getComposerEnv('COMPOSER_DISCARD_CHANGES')) {
  207. if (!in_array($env, array('stash', 'true', 'false', '1', '0'), true)) {
  208. throw new \RuntimeException(
  209. "Invalid value for COMPOSER_DISCARD_CHANGES: {$env}. Expected 1, 0, true, false or stash"
  210. );
  211. }
  212. if ('stash' === $env) {
  213. return 'stash';
  214. }
  215. // convert string value to bool
  216. return $env !== 'false' && (bool) $env;
  217. }
  218. if (!in_array($this->config[$key], array(true, false, 'stash'), true)) {
  219. throw new \RuntimeException(
  220. "Invalid value for 'discard-changes': {$this->config[$key]}. Expected true, false or stash"
  221. );
  222. }
  223. return $this->config[$key];
  224. case 'github-protocols':
  225. if (reset($this->config['github-protocols']) === 'http') {
  226. throw new \RuntimeException('The http protocol for github is not available anymore, update your config\'s github-protocols to use "https", "git" or "ssh"');
  227. }
  228. return $this->config[$key];
  229. default:
  230. if (!isset($this->config[$key])) {
  231. return null;
  232. }
  233. return $this->process($this->config[$key], $flags);
  234. }
  235. }
  236. public function all($flags = 0)
  237. {
  238. $all = array(
  239. 'repositories' => $this->getRepositories(),
  240. );
  241. foreach (array_keys($this->config) as $key) {
  242. $all['config'][$key] = $this->get($key, $flags);
  243. }
  244. return $all;
  245. }
  246. public function raw()
  247. {
  248. return array(
  249. 'repositories' => $this->getRepositories(),
  250. 'config' => $this->config,
  251. );
  252. }
  253. /**
  254. * Checks whether a setting exists
  255. *
  256. * @param string $key
  257. * @return bool
  258. */
  259. public function has($key)
  260. {
  261. return array_key_exists($key, $this->config);
  262. }
  263. /**
  264. * Replaces {$refs} inside a config string
  265. *
  266. * @param string $value a config string that can contain {$refs-to-other-config}
  267. * @param int $flags Options (see class constants)
  268. * @return string
  269. */
  270. private function process($value, $flags)
  271. {
  272. $config = $this;
  273. if (!is_string($value)) {
  274. return $value;
  275. }
  276. return preg_replace_callback('#\{\$(.+)\}#', function ($match) use ($config, $flags) {
  277. return $config->get($match[1], $flags);
  278. }, $value);
  279. }
  280. /**
  281. * Turns relative paths in absolute paths without realpath()
  282. *
  283. * Since the dirs might not exist yet we can not call realpath or it will fail.
  284. *
  285. * @param string $path
  286. * @return string
  287. */
  288. private function realpath($path)
  289. {
  290. if (substr($path, 0, 1) === '/' || substr($path, 1, 1) === ':') {
  291. return $path;
  292. }
  293. return $this->baseDir . '/' . $path;
  294. }
  295. /**
  296. * Reads the value of a Composer environment variable
  297. *
  298. * This should be used to read COMPOSER_ environment variables
  299. * that overload config values.
  300. *
  301. * @param string $var
  302. * @return string|bool
  303. */
  304. private function getComposerEnv($var)
  305. {
  306. if ($this->useEnvironment) {
  307. return getenv($var);
  308. }
  309. return false;
  310. }
  311. }