Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
87.50% covered (warning)
87.50%
7 / 8
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
GlobalVarConfig
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
4 / 4
6
100.00% covered (success)
100.00%
1 / 1
 newInstance
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 get
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 has
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2/**
3 * Copyright 2014
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 */
22
23namespace MediaWiki\Config;
24
25/**
26 * Accesses configuration settings from $GLOBALS
27 *
28 * @newable
29 * @since 1.23
30 */
31class GlobalVarConfig implements Config {
32
33    /**
34     * Prefix to use for configuration variables
35     * @var string
36     */
37    private $prefix;
38
39    /**
40     * Default builder function
41     * @return self
42     */
43    public static function newInstance() {
44        return new self();
45    }
46
47    /**
48     * @stable to call
49     *
50     * @param string $prefix
51     */
52    public function __construct( $prefix = 'wg' ) {
53        $this->prefix = $prefix;
54    }
55
56    /**
57     * @inheritDoc
58     */
59    public function get( $name ) {
60        if ( !$this->has( $name ) ) {
61            throw new ConfigException( __METHOD__ . ": undefined option: '$name'" );
62        }
63        return $GLOBALS[$this->prefix . $name];
64    }
65
66    /**
67     * @inheritDoc
68     */
69    public function has( $name ) {
70        $var = $this->prefix . $name;
71        // (T317951) Don't call array_key_exists unless we have to, as it's slow
72        // on PHP 8.1+ for $GLOBALS. When the key is set but is explicitly set
73        // to null, we still need to fall back to array_key_exists, but that's
74        // rarer.
75        return isset( $GLOBALS[$var] ) || array_key_exists( $var, $GLOBALS );
76    }
77}
78
79/** @deprecated class alias since 1.41 */
80class_alias( GlobalVarConfig::class, 'GlobalVarConfig' );