Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. Config.php

function Config::get

Same name in this branch
  1. 11.1.x core/lib/Drupal/Core/Config/Config.php \Drupal\Core\Config\Config::get()

Returns a setting

Parameters

int $flags Options (see class constants):

Return value

mixed

Throws

\RuntimeException

4 calls to Config::get()
Config::all in vendor/composer/composer/src/Composer/Config.php
Config::getSourceOfValue in vendor/composer/composer/src/Composer/Config.php
Config::process in vendor/composer/composer/src/Composer/Config.php
Replaces {$refs} inside a config string
Config::prohibitUrlByConfig in vendor/composer/composer/src/Composer/Config.php
Validates that the passed URL is allowed to be used by current config, or throws an exception.

File

vendor/composer/composer/src/Composer/Config.php, line 293

Class

Config
@author Jordi Boggiano <j.boggiano@seld.be>

Namespace

Composer

Code

public function get(string $key, int $flags = 0) {
    switch ($key) {
        // strings/paths with env var and {$refs} support
        case 'vendor-dir':
        case 'bin-dir':
        case 'process-timeout':
        case 'data-dir':
        case 'cache-dir':
        case 'cache-files-dir':
        case 'cache-repo-dir':
        case 'cache-vcs-dir':
        case 'cafile':
        case 'capath':
            // convert foo-bar to COMPOSER_FOO_BAR and check if it exists since it overrides the local config
            $env = 'COMPOSER_' . strtoupper(strtr($key, '-', '_'));
            $val = $this->getComposerEnv($env);
            if ($val !== false) {
                $this->setSourceOfConfigValue($val, $key, $env);
            }
            if ($key === 'process-timeout') {
                return max(0, false !== $val ? (int) $val : $this->config[$key]);
            }
            $val = rtrim((string) $this->process(false !== $val ? $val : $this->config[$key], $flags), '/\\');
            $val = Platform::expandPath($val);
            if (substr($key, -4) !== '-dir') {
                return $val;
            }
            return ($flags & self::RELATIVE_PATHS) === self::RELATIVE_PATHS ? $val : $this->realpath($val);
        // booleans with env var support
        case 'cache-read-only':
        case 'htaccess-protect':
            // convert foo-bar to COMPOSER_FOO_BAR and check if it exists since it overrides the local config
            $env = 'COMPOSER_' . strtoupper(strtr($key, '-', '_'));
            $val = $this->getComposerEnv($env);
            if (false === $val) {
                $val = $this->config[$key];
            }
            else {
                $this->setSourceOfConfigValue($val, $key, $env);
            }
            return $val !== 'false' && (bool) $val;
        // booleans without env var support
        case 'disable-tls':
        case 'secure-http':
        case 'use-github-api':
        case 'lock':
            // special case for secure-http
            if ($key === 'secure-http' && $this->get('disable-tls') === true) {
                return false;
            }
            return $this->config[$key] !== 'false' && (bool) $this->config[$key];
        // ints without env var support
        case 'cache-ttl':
            return max(0, (int) $this->config[$key]);
        // numbers with kb/mb/gb support, without env var support
        case 'cache-files-maxsize':
            if (!Preg::isMatch('/^\\s*([0-9.]+)\\s*(?:([kmg])(?:i?b)?)?\\s*$/i', (string) $this->config[$key], $matches)) {
                throw new \RuntimeException("Could not parse the value of '{$key}': {$this->config[$key]}");
            }
            $size = (double) $matches[1];
            if (isset($matches[2])) {
                switch (strtolower($matches[2])) {
                    case 'g':
                        $size *= 1024;
                    // intentional fallthrough
                    // no break
                    case 'm':
                        $size *= 1024;
                    // intentional fallthrough
                    // no break
                    case 'k':
                        $size *= 1024;
                        break;
                }
            }
            return max(0, (int) $size);
        // special cases below
        case 'cache-files-ttl':
            if (isset($this->config[$key])) {
                return max(0, (int) $this->config[$key]);
            }
            return $this->get('cache-ttl');
        case 'home':
            return rtrim($this->process(Platform::expandPath($this->config[$key]), $flags), '/\\');
        case 'bin-compat':
            $value = $this->getComposerEnv('COMPOSER_BIN_COMPAT') ?: $this->config[$key];
            if (!in_array($value, [
                'auto',
                'full',
                'proxy',
                'symlink',
            ])) {
                throw new \RuntimeException("Invalid value for 'bin-compat': {$value}. Expected auto, full or proxy");
            }
            if ($value === 'symlink') {
                trigger_error('config.bin-compat "symlink" is deprecated since Composer 2.2, use auto, full (for Windows compatibility) or proxy instead.', E_USER_DEPRECATED);
            }
            return $value;
        case 'discard-changes':
            $env = $this->getComposerEnv('COMPOSER_DISCARD_CHANGES');
            if ($env !== false) {
                if (!in_array($env, [
                    'stash',
                    'true',
                    'false',
                    '1',
                    '0',
                ], true)) {
                    throw new \RuntimeException("Invalid value for COMPOSER_DISCARD_CHANGES: {$env}. Expected 1, 0, true, false or stash");
                }
                if ('stash' === $env) {
                    return 'stash';
                }
                // convert string value to bool
                return $env !== 'false' && (bool) $env;
            }
            if (!in_array($this->config[$key], [
                true,
                false,
                'stash',
            ], true)) {
                throw new \RuntimeException("Invalid value for 'discard-changes': {$this->config[$key]}. Expected true, false or stash");
            }
            return $this->config[$key];
        case 'github-protocols':
            $protos = $this->config['github-protocols'];
            if ($this->config['secure-http'] && false !== ($index = array_search('git', $protos))) {
                unset($protos[$index]);
            }
            if (reset($protos) === 'http') {
                throw new \RuntimeException('The http protocol for github is not available anymore, update your config\'s github-protocols to use "https", "git" or "ssh"');
            }
            return $protos;
        case 'autoloader-suffix':
            if ($this->config[$key] === '') {
                // we need to guarantee null or non-empty-string
                return null;
            }
            return $this->process($this->config[$key], $flags);
        case 'audit':
            $result = $this->config[$key];
            $abandonedEnv = $this->getComposerEnv('COMPOSER_AUDIT_ABANDONED');
            if (false !== $abandonedEnv) {
                if (!in_array($abandonedEnv, $validChoices = Auditor::ABANDONEDS, true)) {
                    throw new \RuntimeException("Invalid value for COMPOSER_AUDIT_ABANDONED: {$abandonedEnv}. Expected one of " . implode(', ', Auditor::ABANDONEDS) . ".");
                }
                $result['abandoned'] = $abandonedEnv;
            }
            return $result;
        default:
            if (!isset($this->config[$key])) {
                return null;
            }
            return $this->process($this->config[$key], $flags);
    }
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal