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 public static function newInstance() {
26 return new self();
27 }
28
34 public function __construct(
35 private readonly string $prefix = 'wg',
36 ) {
37 }
38
42 public function get( $name ) {
43 $var = $this->prefix . $name;
44
45 // Fast path combines check and retrieval.
46 $value = $GLOBALS[$var] ?? null;
47 if ( $value !== null ) {
48 return $value;
49 }
50
51 // Slow path: the value is either explicitly null or missing.
52 // We have to pay the price of array_key_exists() here to distinguish the two.
53 if ( array_key_exists( $var, $GLOBALS ) ) {
54 return null;
55 }
56
57 throw new ConfigException( __METHOD__ . ": undefined option: '$name'" );
58 }
59
63 public function has( $name ) {
64 $var = $this->prefix . $name;
65 // (T317951) Don't call array_key_exists unless we have to, as it's slow
66 // on PHP 8.1+ for $GLOBALS. When the key is set but is explicitly set
67 // to null, we still need to fall back to array_key_exists, but that's
68 // rarer.
69 return isset( $GLOBALS[$var] ) || array_key_exists( $var, $GLOBALS );
70 }
71}
72
74class_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
__construct(private readonly string $prefix='wg',)
static newInstance()
Default builder function.
Interface for configuration instances.
Definition Config.php:18