Config.php 8.2 KB

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