Config.php 9.5 KB

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