1
0
Fork 0

Reorg config class a little

pull/9152/head
Jordi Boggiano 2020-08-25 13:54:29 +02:00
parent 6186c7f36f
commit 875a4784ed
No known key found for this signature in database
GPG Key ID: 7BBD42C429EC80BC
1 changed files with 21 additions and 14 deletions

View File

@ -211,6 +211,7 @@ class Config
public function get($key, $flags = 0) public function get($key, $flags = 0)
{ {
switch ($key) { switch ($key) {
// strings/paths with env var and {$refs} support
case 'vendor-dir': case 'vendor-dir':
case 'bin-dir': case 'bin-dir':
case 'process-timeout': case 'process-timeout':
@ -234,20 +235,33 @@ class Config
return (($flags & self::RELATIVE_PATHS) == self::RELATIVE_PATHS) ? $val : $this->realpath($val); return (($flags & self::RELATIVE_PATHS) == self::RELATIVE_PATHS) ? $val : $this->realpath($val);
// booleans with env var support
case 'htaccess-protect': case 'htaccess-protect':
$value = $this->getComposerEnv('COMPOSER_HTACCESS_PROTECT'); // convert foo-bar to COMPOSER_FOO_BAR and check if it exists since it overrides the local config
if (false === $value) { $env = 'COMPOSER_' . strtoupper(strtr($key, '-', '_'));
$value = $this->config[$key];
}
return $value !== 'false' && (bool) $value;
$val = $this->getComposerEnv($env);
if (false === $val) {
$val = $this->config[$key];
}
return $val !== 'false' && (bool) $val;
// booleans without env var support
case 'disable-tls':
case 'secure-http':
case 'use-github-api':
case 'lock':
return $this->config[$key] !== 'false' && (bool) $this->config[$key];
// ints without env var support
case 'cache-ttl': case 'cache-ttl':
return (int) $this->config[$key]; return (int) $this->config[$key];
// numbers with kb/mb/gb support, without env var support
case 'cache-files-maxsize': case 'cache-files-maxsize':
if (!preg_match('/^\s*([0-9.]+)\s*(?:([kmg])(?:i?b)?)?\s*$/i', $this->config[$key], $matches)) { if (!preg_match('/^\s*([0-9.]+)\s*(?:([kmg])(?:i?b)?)?\s*$/i', $this->config[$key], $matches)) {
throw new \RuntimeException( throw new \RuntimeException(
"Could not parse the value of 'cache-files-maxsize': {$this->config[$key]}" "Could not parse the value of '$key': {$this->config[$key]}"
); );
} }
$size = $matches[1]; $size = $matches[1];
@ -269,6 +283,7 @@ class Config
return $size; return $size;
// special cases below
case 'cache-files-ttl': case 'cache-files-ttl':
if (isset($this->config[$key])) { if (isset($this->config[$key])) {
return (int) $this->config[$key]; return (int) $this->config[$key];
@ -326,14 +341,6 @@ class Config
return $protos; return $protos;
case 'disable-tls':
return $this->config[$key] !== 'false' && (bool) $this->config[$key];
case 'secure-http':
return $this->config[$key] !== 'false' && (bool) $this->config[$key];
case 'use-github-api':
return $this->config[$key] !== 'false' && (bool) $this->config[$key];
case 'lock':
return $this->config[$key] !== 'false' && (bool) $this->config[$key];
default: default:
if (!isset($this->config[$key])) { if (!isset($this->config[$key])) {
return null; return null;