Config.php 7.1 KB

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