Config.php 7.1 KB

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