MediaWiki master
GlobalVarConfig.php
Go to the documentation of this file.
1<?php
9namespace MediaWiki\Config;
10
11use function array_key_exists;
12
19class GlobalVarConfig implements Config {
20
25 private $prefix;
26
31 public static function newInstance() {
32 return new self();
33 }
34
40 public function __construct( $prefix = 'wg' ) {
41 $this->prefix = $prefix;
42 }
43
47 public function get( $name ) {
48 $var = $this->prefix . $name;
49
50 // Fast path combines check and retrieval.
51 $value = $GLOBALS[$var] ?? null;
52 if ( $value !== null ) {
53 return $value;
54 }
55
56 // Slow path: the value is either explicitly null or missing.
57 // We have to pay the price of array_key_exists() here to distinguish the two.
58 if ( array_key_exists( $var, $GLOBALS ) ) {
59 return null;
60 }
61
62 throw new ConfigException( __METHOD__ . ": undefined option: '$name'" );
63 }
64
68 public function has( $name ) {
69 $var = $this->prefix . $name;
70 // (T317951) Don't call array_key_exists unless we have to, as it's slow
71 // on PHP 8.1+ for $GLOBALS. When the key is set but is explicitly set
72 // to null, we still need to fall back to array_key_exists, but that's
73 // rarer.
74 return isset( $GLOBALS[$var] ) || array_key_exists( $var, $GLOBALS );
75 }
76}
77
79class_alias( GlobalVarConfig::class, 'GlobalVarConfig' );
Exceptions for config failures.
Accesses configuration settings from $GLOBALS.
has( $name)
Check whether a configuration option is set for the given name.bool 1.24
static newInstance()
Default builder function.
Interface for configuration instances.
Definition Config.php:18