Config.php 11 KB

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