Config.php 11 KB

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