Config.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. /**
  13. * @author Jordi Boggiano <j.boggiano@seld.be>
  14. */
  15. class Config
  16. {
  17. public static $defaultConfig = array(
  18. 'process-timeout' => 300,
  19. 'vendor-dir' => 'vendor',
  20. 'bin-dir' => '{$vendor-dir}/bin',
  21. 'notify-on-install' => true,
  22. 'github-protocols' => array('git', 'https', 'http'),
  23. );
  24. public static $defaultRepositories = array(
  25. 'packagist' => array(
  26. 'type' => 'composer',
  27. 'url' => 'http://packagist.org',
  28. )
  29. );
  30. private $config;
  31. private $repositories;
  32. public function __construct()
  33. {
  34. // load defaults
  35. $this->config = static::$defaultConfig;
  36. $this->repositories = static::$defaultRepositories;
  37. }
  38. /**
  39. * Merges new config values with the existing ones (overriding)
  40. *
  41. * @param array $config
  42. */
  43. public function merge(array $config)
  44. {
  45. // override defaults with given config
  46. if (!empty($config['config']) && is_array($config['config'])) {
  47. $this->config = array_replace_recursive($this->config, $config['config']);
  48. }
  49. if (!empty($config['repositories']) && is_array($config['repositories'])) {
  50. $this->repositories = array_reverse($this->repositories, true);
  51. $newRepos = array_reverse($config['repositories'], true);
  52. foreach ($newRepos as $name => $repository) {
  53. // disable a repository by name
  54. if (false === $repository) {
  55. unset($this->repositories[$name]);
  56. continue;
  57. }
  58. // disable a repository with an anonymous {"name": false} repo
  59. if (1 === count($repository) && false === current($repository)) {
  60. unset($this->repositories[key($repository)]);
  61. continue;
  62. }
  63. // store repo
  64. if (is_int($name)) {
  65. $this->repositories[] = $repository;
  66. } else {
  67. $this->repositories[$name] = $repository;
  68. }
  69. }
  70. $this->repositories = array_reverse($this->repositories, true);
  71. }
  72. }
  73. /**
  74. * @return array
  75. */
  76. public function getRepositories()
  77. {
  78. return $this->repositories;
  79. }
  80. /**
  81. * Returns a setting
  82. *
  83. * @param string $key
  84. * @return mixed
  85. */
  86. public function get($key)
  87. {
  88. switch ($key) {
  89. case 'vendor-dir':
  90. case 'bin-dir':
  91. case 'process-timeout':
  92. // convert foo-bar to COMPOSER_FOO_BAR and check if it exists since it overrides the local config
  93. $env = 'COMPOSER_' . strtoupper(strtr($key, '-', '_'));
  94. return rtrim($this->process(getenv($env) ?: $this->config[$key]), '/\\');
  95. case 'home':
  96. return rtrim($this->process($this->config[$key]), '/\\');
  97. default:
  98. return $this->process($this->config[$key]);
  99. }
  100. }
  101. /**
  102. * Checks whether a setting exists
  103. *
  104. * @param string $key
  105. * @return bool
  106. */
  107. public function has($key)
  108. {
  109. return array_key_exists($key, $this->config);
  110. }
  111. /**
  112. * Replaces {$refs} inside a config string
  113. *
  114. * @param string a config string that can contain {$refs-to-other-config}
  115. * @return string
  116. */
  117. private function process($value)
  118. {
  119. $config = $this;
  120. return preg_replace_callback('#\{\$(.+)\}#', function ($match) use ($config) {
  121. return $config->get($match[1]);
  122. }, $value);
  123. }
  124. }