Config.php 3.8 KB

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