Config.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. use Composer\Plugin\PluginInterface;
  14. /**
  15. * @author Jordi Boggiano <j.boggiano@seld.be>
  16. */
  17. class Config
  18. {
  19. const RELATIVE_PATHS = 1;
  20. public static $defaultConfig = array(
  21. 'process-timeout' => 300,
  22. 'use-include-path' => false,
  23. 'preferred-install' => 'auto',
  24. 'notify-on-install' => true,
  25. 'github-protocols' => array('git', 'https', 'ssh'),
  26. 'vendor-dir' => 'vendor',
  27. 'bin-dir' => '{$vendor-dir}/bin',
  28. 'cache-dir' => '{$home}/cache',
  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. '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 'discard-changes':
  195. if ($env = $this->getComposerEnv('COMPOSER_DISCARD_CHANGES')) {
  196. if (!in_array($env, array('stash', 'true', 'false', '1', '0'), true)) {
  197. throw new \RuntimeException(
  198. "Invalid value for COMPOSER_DISCARD_CHANGES: {$env}. Expected 1, 0, true, false or stash"
  199. );
  200. }
  201. if ('stash' === $env) {
  202. return 'stash';
  203. }
  204. // convert string value to bool
  205. return $env !== 'false' && (bool) $env;
  206. }
  207. if (!in_array($this->config[$key], array(true, false, 'stash'), true)) {
  208. throw new \RuntimeException(
  209. "Invalid value for 'discard-changes': {$this->config[$key]}. Expected true, false or stash"
  210. );
  211. }
  212. return $this->config[$key];
  213. case 'github-protocols':
  214. if (reset($this->config['github-protocols']) === 'http') {
  215. throw new \RuntimeException('The http protocol for github is not available anymore, update your config\'s github-protocols to use "https", "git" or "ssh"');
  216. }
  217. return $this->config[$key];
  218. default:
  219. if (!isset($this->config[$key])) {
  220. return null;
  221. }
  222. return $this->process($this->config[$key], $flags);
  223. }
  224. }
  225. public function all($flags = 0)
  226. {
  227. $all = array(
  228. 'repositories' => $this->getRepositories(),
  229. );
  230. foreach (array_keys($this->config) as $key) {
  231. $all['config'][$key] = $this->get($key, $flags);
  232. }
  233. return $all;
  234. }
  235. public function raw()
  236. {
  237. return array(
  238. 'repositories' => $this->getRepositories(),
  239. 'config' => $this->config,
  240. );
  241. }
  242. /**
  243. * Checks whether a setting exists
  244. *
  245. * @param string $key
  246. * @return bool
  247. */
  248. public function has($key)
  249. {
  250. return array_key_exists($key, $this->config);
  251. }
  252. /**
  253. * Replaces {$refs} inside a config string
  254. *
  255. * @param string $value a config string that can contain {$refs-to-other-config}
  256. * @param int $flags Options (see class constants)
  257. * @return string
  258. */
  259. private function process($value, $flags)
  260. {
  261. $config = $this;
  262. if (!is_string($value)) {
  263. return $value;
  264. }
  265. return preg_replace_callback('#\{\$(.+)\}#', function ($match) use ($config, $flags) {
  266. return $config->get($match[1], $flags);
  267. }, $value);
  268. }
  269. /**
  270. * Turns relative paths in absolute paths without realpath()
  271. *
  272. * Since the dirs might not exist yet we can not call realpath or it will fail.
  273. *
  274. * @param string $path
  275. * @return string
  276. */
  277. private function realpath($path)
  278. {
  279. if (substr($path, 0, 1) === '/' || substr($path, 1, 1) === ':') {
  280. return $path;
  281. }
  282. return $this->baseDir . '/' . $path;
  283. }
  284. /**
  285. * Reads the value of a Composer environment variable
  286. *
  287. * This should be used to read COMPOSER_ environment variables
  288. * that overload config values.
  289. *
  290. * @param string $var
  291. * @return string|boolean
  292. */
  293. private function getComposerEnv($var)
  294. {
  295. if ($this->useEnvironment) {
  296. return getenv($var);
  297. }
  298. return false;
  299. }
  300. }