PageRenderTime 43ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/composer/composer/src/Composer/Config.php

https://gitlab.com/yousafsyed/easternglamor
PHP | 342 lines | 252 code | 31 blank | 59 comment | 24 complexity | 90a6f02dcc000c6a0625792d12f7b74c MD5 | raw file
  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. const RELATIVE_PATHS = 1;
  19. public static $defaultConfig = array(
  20. 'process-timeout' => 300,
  21. 'use-include-path' => false,
  22. 'preferred-install' => 'auto',
  23. 'notify-on-install' => true,
  24. 'github-protocols' => array('git', 'https', 'ssh'),
  25. 'vendor-dir' => 'vendor',
  26. 'bin-dir' => '{$vendor-dir}/bin',
  27. 'cache-dir' => '{$home}/cache',
  28. 'cache-files-dir' => '{$cache-dir}/files',
  29. 'cache-repo-dir' => '{$cache-dir}/repo',
  30. 'cache-vcs-dir' => '{$cache-dir}/vcs',
  31. 'cache-ttl' => 15552000, // 6 months
  32. 'cache-files-ttl' => null, // fallback to cache-ttl
  33. 'cache-files-maxsize' => '300MiB',
  34. 'discard-changes' => false,
  35. 'autoloader-suffix' => null,
  36. 'optimize-autoloader' => false,
  37. 'classmap-authoritative' => false,
  38. 'prepend-autoloader' => true,
  39. 'github-domains' => array('github.com'),
  40. 'github-expose-hostname' => true,
  41. 'store-auths' => 'prompt',
  42. // valid keys without defaults (auth config stuff):
  43. // github-oauth
  44. // http-basic
  45. );
  46. public static $defaultRepositories = array(
  47. 'packagist' => array(
  48. 'type' => 'composer',
  49. 'url' => 'https?://packagist.org',
  50. 'allow_ssl_downgrade' => true,
  51. )
  52. );
  53. private $config;
  54. private $baseDir;
  55. private $repositories;
  56. private $configSource;
  57. private $authConfigSource;
  58. private $useEnvironment;
  59. /**
  60. * @param boolean $useEnvironment Use COMPOSER_ environment variables to replace config settings
  61. */
  62. public function __construct($useEnvironment = true, $baseDir = null)
  63. {
  64. // load defaults
  65. $this->config = static::$defaultConfig;
  66. $this->repositories = static::$defaultRepositories;
  67. $this->useEnvironment = (bool) $useEnvironment;
  68. $this->baseDir = $baseDir;
  69. }
  70. public function setConfigSource(ConfigSourceInterface $source)
  71. {
  72. $this->configSource = $source;
  73. }
  74. public function getConfigSource()
  75. {
  76. return $this->configSource;
  77. }
  78. public function setAuthConfigSource(ConfigSourceInterface $source)
  79. {
  80. $this->authConfigSource = $source;
  81. }
  82. public function getAuthConfigSource()
  83. {
  84. return $this->authConfigSource;
  85. }
  86. /**
  87. * Merges new config values with the existing ones (overriding)
  88. *
  89. * @param array $config
  90. */
  91. public function merge($config)
  92. {
  93. // override defaults with given config
  94. if (!empty($config['config']) && is_array($config['config'])) {
  95. foreach ($config['config'] as $key => $val) {
  96. if (in_array($key, array('github-oauth', 'http-basic')) && isset($this->config[$key])) {
  97. $this->config[$key] = array_merge($this->config[$key], $val);
  98. } else {
  99. $this->config[$key] = $val;
  100. }
  101. }
  102. }
  103. if (!empty($config['repositories']) && is_array($config['repositories'])) {
  104. $this->repositories = array_reverse($this->repositories, true);
  105. $newRepos = array_reverse($config['repositories'], true);
  106. foreach ($newRepos as $name => $repository) {
  107. // disable a repository by name
  108. if (false === $repository) {
  109. unset($this->repositories[$name]);
  110. continue;
  111. }
  112. // disable a repository with an anonymous {"name": false} repo
  113. if (is_array($repository) && 1 === count($repository) && false === current($repository)) {
  114. unset($this->repositories[key($repository)]);
  115. continue;
  116. }
  117. // store repo
  118. if (is_int($name)) {
  119. $this->repositories[] = $repository;
  120. } else {
  121. $this->repositories[$name] = $repository;
  122. }
  123. }
  124. $this->repositories = array_reverse($this->repositories, true);
  125. }
  126. }
  127. /**
  128. * @return array
  129. */
  130. public function getRepositories()
  131. {
  132. return $this->repositories;
  133. }
  134. /**
  135. * Returns a setting
  136. *
  137. * @param string $key
  138. * @param int $flags Options (see class constants)
  139. * @throws \RuntimeException
  140. * @return mixed
  141. */
  142. public function get($key, $flags = 0)
  143. {
  144. switch ($key) {
  145. case 'vendor-dir':
  146. case 'bin-dir':
  147. case 'process-timeout':
  148. case 'cache-dir':
  149. case 'cache-files-dir':
  150. case 'cache-repo-dir':
  151. case 'cache-vcs-dir':
  152. // convert foo-bar to COMPOSER_FOO_BAR and check if it exists since it overrides the local config
  153. $env = 'COMPOSER_' . strtoupper(strtr($key, '-', '_'));
  154. $val = rtrim($this->process($this->getComposerEnv($env) ?: $this->config[$key], $flags), '/\\');
  155. $val = preg_replace('#^(\$HOME|~)(/|$)#', rtrim(getenv('HOME') ?: getenv('USERPROFILE'), '/\\') . '/', $val);
  156. if (substr($key, -4) !== '-dir') {
  157. return $val;
  158. }
  159. return ($flags & self::RELATIVE_PATHS == 1) ? $val : $this->realpath($val);
  160. case 'cache-ttl':
  161. return (int) $this->config[$key];
  162. case 'cache-files-maxsize':
  163. if (!preg_match('/^\s*([0-9.]+)\s*(?:([kmg])(?:i?b)?)?\s*$/i', $this->config[$key], $matches)) {
  164. throw new \RuntimeException(
  165. "Could not parse the value of 'cache-files-maxsize': {$this->config[$key]}"
  166. );
  167. }
  168. $size = $matches[1];
  169. if (isset($matches[2])) {
  170. switch (strtolower($matches[2])) {
  171. case 'g':
  172. $size *= 1024;
  173. // intentional fallthrough
  174. case 'm':
  175. $size *= 1024;
  176. // intentional fallthrough
  177. case 'k':
  178. $size *= 1024;
  179. break;
  180. }
  181. }
  182. return $size;
  183. case 'cache-files-ttl':
  184. if (isset($this->config[$key])) {
  185. return (int) $this->config[$key];
  186. }
  187. return (int) $this->config['cache-ttl'];
  188. case 'home':
  189. return rtrim($this->process($this->config[$key], $flags), '/\\');
  190. case 'discard-changes':
  191. if ($env = $this->getComposerEnv('COMPOSER_DISCARD_CHANGES')) {
  192. if (!in_array($env, array('stash', 'true', 'false', '1', '0'), true)) {
  193. throw new \RuntimeException(
  194. "Invalid value for COMPOSER_DISCARD_CHANGES: {$env}. Expected 1, 0, true, false or stash"
  195. );
  196. }
  197. if ('stash' === $env) {
  198. return 'stash';
  199. }
  200. // convert string value to bool
  201. return $env !== 'false' && (bool) $env;
  202. }
  203. if (!in_array($this->config[$key], array(true, false, 'stash'), true)) {
  204. throw new \RuntimeException(
  205. "Invalid value for 'discard-changes': {$this->config[$key]}. Expected true, false or stash"
  206. );
  207. }
  208. return $this->config[$key];
  209. case 'github-protocols':
  210. if (reset($this->config['github-protocols']) === 'http') {
  211. throw new \RuntimeException('The http protocol for github is not available anymore, update your config\'s github-protocols to use "https", "git" or "ssh"');
  212. }
  213. return $this->config[$key];
  214. default:
  215. if (!isset($this->config[$key])) {
  216. return null;
  217. }
  218. return $this->process($this->config[$key], $flags);
  219. }
  220. }
  221. public function all($flags = 0)
  222. {
  223. $all = array(
  224. 'repositories' => $this->getRepositories(),
  225. );
  226. foreach (array_keys($this->config) as $key) {
  227. $all['config'][$key] = $this->get($key, $flags);
  228. }
  229. return $all;
  230. }
  231. public function raw()
  232. {
  233. return array(
  234. 'repositories' => $this->getRepositories(),
  235. 'config' => $this->config,
  236. );
  237. }
  238. /**
  239. * Checks whether a setting exists
  240. *
  241. * @param string $key
  242. * @return bool
  243. */
  244. public function has($key)
  245. {
  246. return array_key_exists($key, $this->config);
  247. }
  248. /**
  249. * Replaces {$refs} inside a config string
  250. *
  251. * @param string $value a config string that can contain {$refs-to-other-config}
  252. * @param int $flags Options (see class constants)
  253. * @return string
  254. */
  255. private function process($value, $flags)
  256. {
  257. $config = $this;
  258. if (!is_string($value)) {
  259. return $value;
  260. }
  261. return preg_replace_callback('#\{\$(.+)\}#', function ($match) use ($config, $flags) {
  262. return $config->get($match[1], $flags);
  263. }, $value);
  264. }
  265. /**
  266. * Turns relative paths in absolute paths without realpath()
  267. *
  268. * Since the dirs might not exist yet we can not call realpath or it will fail.
  269. *
  270. * @param string $path
  271. * @return string
  272. */
  273. private function realpath($path)
  274. {
  275. if (substr($path, 0, 1) === '/' || substr($path, 1, 1) === ':') {
  276. return $path;
  277. }
  278. return $this->baseDir . '/' . $path;
  279. }
  280. /**
  281. * Reads the value of a Composer environment variable
  282. *
  283. * This should be used to read COMPOSER_ environment variables
  284. * that overload config values.
  285. *
  286. * @param string $var
  287. * @return string|boolean
  288. */
  289. private function getComposerEnv($var)
  290. {
  291. if ($this->useEnvironment) {
  292. return getenv($var);
  293. }
  294. return false;
  295. }
  296. }