Config.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. 'disable-tls' => false
  39. );
  40. public static $defaultRepositories = array(
  41. 'packagist' => array(
  42. 'type' => 'composer',
  43. 'url' => 'https?://packagist.org',
  44. 'allow_ssl_downgrade' => true,
  45. )
  46. );
  47. private $config;
  48. private $repositories;
  49. private $configSource;
  50. public function __construct()
  51. {
  52. // load defaults
  53. $this->config = static::$defaultConfig;
  54. $this->repositories = static::$defaultRepositories;
  55. }
  56. public function setConfigSource(ConfigSourceInterface $source)
  57. {
  58. $this->configSource = $source;
  59. }
  60. public function getConfigSource()
  61. {
  62. return $this->configSource;
  63. }
  64. /**
  65. * Merges new config values with the existing ones (overriding)
  66. *
  67. * @param array $config
  68. */
  69. public function merge(array $config)
  70. {
  71. // override defaults with given config
  72. if (!empty($config['config']) && is_array($config['config'])) {
  73. foreach ($config['config'] as $key => $val) {
  74. if (in_array($key, array('github-oauth')) && isset($this->config[$key])) {
  75. $this->config[$key] = array_merge($this->config[$key], $val);
  76. } else {
  77. $this->config[$key] = $val;
  78. }
  79. }
  80. }
  81. if (!empty($config['repositories']) && is_array($config['repositories'])) {
  82. $this->repositories = array_reverse($this->repositories, true);
  83. $newRepos = array_reverse($config['repositories'], true);
  84. foreach ($newRepos as $name => $repository) {
  85. // disable a repository by name
  86. if (false === $repository) {
  87. unset($this->repositories[$name]);
  88. continue;
  89. }
  90. // disable a repository with an anonymous {"name": false} repo
  91. if (1 === count($repository) && false === current($repository)) {
  92. unset($this->repositories[key($repository)]);
  93. continue;
  94. }
  95. // store repo
  96. if (is_int($name)) {
  97. $this->repositories[] = $repository;
  98. } else {
  99. $this->repositories[$name] = $repository;
  100. }
  101. }
  102. $this->repositories = array_reverse($this->repositories, true);
  103. }
  104. }
  105. /**
  106. * @return array
  107. */
  108. public function getRepositories()
  109. {
  110. return $this->repositories;
  111. }
  112. /**
  113. * Returns a setting
  114. *
  115. * @param string $key
  116. * @throws \RuntimeException
  117. * @return mixed
  118. */
  119. public function get($key)
  120. {
  121. switch ($key) {
  122. case 'vendor-dir':
  123. case 'bin-dir':
  124. case 'process-timeout':
  125. case 'cache-dir':
  126. case 'cache-files-dir':
  127. case 'cache-repo-dir':
  128. case 'cache-vcs-dir':
  129. // convert foo-bar to COMPOSER_FOO_BAR and check if it exists since it overrides the local config
  130. $env = 'COMPOSER_' . strtoupper(strtr($key, '-', '_'));
  131. return rtrim($this->process(getenv($env) ?: $this->config[$key]), '/\\');
  132. case 'cache-ttl':
  133. return (int) $this->config[$key];
  134. case 'cache-files-maxsize':
  135. if (!preg_match('/^\s*([0-9.]+)\s*(?:([kmg])(?:i?b)?)?\s*$/i', $this->config[$key], $matches)) {
  136. throw new \RuntimeException(
  137. "Could not parse the value of 'cache-files-maxsize': {$this->config[$key]}"
  138. );
  139. }
  140. $size = $matches[1];
  141. if (isset($matches[2])) {
  142. switch (strtolower($matches[2])) {
  143. case 'g':
  144. $size *= 1024;
  145. // intentional fallthrough
  146. case 'm':
  147. $size *= 1024;
  148. // intentional fallthrough
  149. case 'k':
  150. $size *= 1024;
  151. break;
  152. }
  153. }
  154. return $size;
  155. case 'cache-files-ttl':
  156. if (isset($this->config[$key])) {
  157. return (int) $this->config[$key];
  158. }
  159. return (int) $this->config['cache-ttl'];
  160. case 'home':
  161. return rtrim($this->process($this->config[$key]), '/\\');
  162. case 'discard-changes':
  163. if ($env = getenv('COMPOSER_DISCARD_CHANGES')) {
  164. if (!in_array($env, array('stash', 'true', 'false', '1', '0'), true)) {
  165. throw new \RuntimeException(
  166. "Invalid value for COMPOSER_DISCARD_CHANGES: {$env}. Expected 1, 0, true, false or stash"
  167. );
  168. }
  169. if ('stash' === $env) {
  170. return 'stash';
  171. }
  172. // convert string value to bool
  173. return $env !== 'false' && (bool) $env;
  174. }
  175. if (!in_array($this->config[$key], array(true, false, 'stash'), true)) {
  176. throw new \RuntimeException(
  177. "Invalid value for 'discard-changes': {$this->config[$key]}. Expected true, false or stash"
  178. );
  179. }
  180. return $this->config[$key];
  181. case 'github-protocols':
  182. if (reset($this->config['github-protocols']) === 'http') {
  183. throw new \RuntimeException('The http protocol for github is not available anymore, update your config\'s github-protocols to use "https", "git" or "ssh"');
  184. }
  185. return $this->config[$key];
  186. case 'disable-tls':
  187. return $this->config[$key]!== 'false' && (bool) $this->config[$key];
  188. default:
  189. if (!isset($this->config[$key])) {
  190. return null;
  191. }
  192. return $this->process($this->config[$key]);
  193. }
  194. }
  195. public function all()
  196. {
  197. $all = array(
  198. 'repositories' => $this->getRepositories(),
  199. );
  200. foreach (array_keys($this->config) as $key) {
  201. $all['config'][$key] = $this->get($key);
  202. }
  203. return $all;
  204. }
  205. public function raw()
  206. {
  207. return array(
  208. 'repositories' => $this->getRepositories(),
  209. 'config' => $this->config,
  210. );
  211. }
  212. /**
  213. * Checks whether a setting exists
  214. *
  215. * @param string $key
  216. * @return bool
  217. */
  218. public function has($key)
  219. {
  220. return array_key_exists($key, $this->config);
  221. }
  222. /**
  223. * Replaces {$refs} inside a config string
  224. *
  225. * @param string $value a config string that can contain {$refs-to-other-config}
  226. * @return string
  227. */
  228. private function process($value)
  229. {
  230. $config = $this;
  231. if (!is_string($value)) {
  232. return $value;
  233. }
  234. return preg_replace_callback('#\{\$(.+)\}#', function ($match) use ($config) {
  235. return $config->get($match[1]);
  236. }, $value);
  237. }
  238. }