Config.php 12 KB

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