| 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 | |
| 23 | namespace MediaWiki\Config; |
| 24 | |
| 25 | use function array_key_exists; |
| 26 | |
| 27 | /** |
| 28 | * Accesses configuration settings from $GLOBALS |
| 29 | * |
| 30 | * @newable |
| 31 | * @since 1.23 |
| 32 | */ |
| 33 | class GlobalVarConfig implements Config { |
| 34 | |
| 35 | /** |
| 36 | * Prefix to use for configuration variables |
| 37 | * @var string |
| 38 | */ |
| 39 | private $prefix; |
| 40 | |
| 41 | /** |
| 42 | * Default builder function |
| 43 | * @return self |
| 44 | */ |
| 45 | public static function newInstance() { |
| 46 | return new self(); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * @stable to call |
| 51 | * |
| 52 | * @param string $prefix |
| 53 | */ |
| 54 | public function __construct( $prefix = 'wg' ) { |
| 55 | $this->prefix = $prefix; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * @inheritDoc |
| 60 | */ |
| 61 | public function get( $name ) { |
| 62 | if ( !$this->has( $name ) ) { |
| 63 | throw new ConfigException( __METHOD__ . ": undefined option: '$name'" ); |
| 64 | } |
| 65 | return $GLOBALS[$this->prefix . $name]; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * @inheritDoc |
| 70 | */ |
| 71 | public function has( $name ) { |
| 72 | $var = $this->prefix . $name; |
| 73 | // (T317951) Don't call array_key_exists unless we have to, as it's slow |
| 74 | // on PHP 8.1+ for $GLOBALS. When the key is set but is explicitly set |
| 75 | // to null, we still need to fall back to array_key_exists, but that's |
| 76 | // rarer. |
| 77 | return isset( $GLOBALS[$var] ) || array_key_exists( $var, $GLOBALS ); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | /** @deprecated class alias since 1.41 */ |
| 82 | class_alias( GlobalVarConfig::class, 'GlobalVarConfig' ); |