Config.php 11 KB

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